package cn.mucang.android.community.utils;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 图像工具类,用于裁剪、放大缩小、质量调整等...
* Created by Sanders on 2014/11/3.
*/
public class BitmapUtils {
/**
* 裁剪图片为当前指定的尺寸
*
* @param path
* @param newWidth 指定宽度
* @param newHeight 指定高度
* @return 返回宽度不大于指定宽度,高度不大于指定高度的Bitmap
*/
public static Bitmap tailorBitmap(String path, int newWidth, int newHeight) {
int maxSize = newWidth >= newHeight ? newWidth : newHeight;
Bitmap inBitmap = getValidationBitmap(path, maxSize);
return tailorBitmap(inBitmap, newWidth, newHeight);
}
/**
* 按最大边裁剪
*
* @param path
* @param maxSize
* @return
*/
public static Bitmap tailorBitmap(String path, int maxSize) {
Bitmap inBitmap = getValidationBitmap(path, maxSize);
return tailorBitmap(inBitmap, maxSize);
}
/**
* 参见图片为当前指定的尺寸
*
* @param inBitmap
* @param newWidth 指定宽度
* @param newHeight 指定高度
* @return 返回宽度不大于指定宽度,高度不大于指定高度的Bitmap
*/
public static Bitmap tailorBitmap(Bitmap inBitmap, int newWidth, int newHeight) {
float inWidth = inBitmap.getWidth();
float inHeight = inBitmap.getHeight();
float scale;
if (inWidth >= inHeight) {
scale = (float) newWidth / inWidth;
} else {
scale = (float) newHeight / inHeight;
}
if(scale == 1){
return inBitmap;
}
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
Bitmap newBitmap = Bitmap.createBitmap(inBitmap, 0, 0, (int) inWidth, (int) inHeight, matrix, true);
inBitmap.recycle();
return newBitmap;
}
/**
* 验证图像不要内存溢出,在操作前先缩小下
*
* @param path 文件路径
* @param inMaxSize 一条边的最大长度
* @return
*/
public static Bitmap getValidationBitmap(String path, int inMaxSize) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(path, options);
int outWidth = options.outWidth;
int outHeight = options.outHeight;
int maxSize = outWidth >= outHeight ? outWidth : outHeight;
int sampleSize = maxSize / inMaxSize;
options.inSampleSize = sampleSize > 0 ? sampleSize : 1;
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(path, options);
return bitmap;
}
/**
* 裁剪最大的边为传入边,最大边长不能大于maxSize
*
* @param inBitmap 位图
* @param maxSize 最大边长
* @return
*/
public static Bitmap tailorBitmap(Bitmap inBitmap, int maxSize) {
float inWidth = inBitmap.getWidth();
float inHeight = inBitmap.getHeight();
float scale;
if (inWidth >= inHeight) {
scale = (float) maxSize / inWidth;
} else {
scale = (float) maxSize / inHeight;
}
if(scale == 1){
return inBitmap;
}
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
Bitmap newBitmap = Bitmap.createBitmap(inBitmap, 0, 0, (int) inWidth, (int) inHeight, matrix, true);
inBitmap.recycle();
return newBitmap;
}
/**
* 保存bitmap为图片
*
* @param bitmap
* @param filePath
* @param fileName
* @return
* @throws java.io.IOException
*/
public static File saveBitmapToFile(Bitmap bitmap, String filePath, String fileName) throws IOException {
File file = new File(filePath, fileName + ".jpg");
FileOutputStream outputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, outputStream);
outputStream.close();
bitmap.recycle();
return file;
}
/**
* 将bitmap转换为Byte数组并压缩指定大小
*
* @param bitmap
* @param maxSize 指定大小
* @return
* @throws java.io.IOException
*/
public static byte[] getBitmapToBytes(Bitmap bitmap, long maxSize) throws IOException {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
int options = 80;
bitmap.compress(Bitmap.CompressFormat.JPEG, options, outStream);
byte[] buffer = outStream.toByteArray();
while (buffer.length > maxSize) {
outStream.reset();
options -= 1;
bitmap.compress(Bitmap.CompressFormat.JPEG, options, outStream);
buffer = outStream.toByteArray();
}
outStream.close();
bitmap.recycle();
return buffer;
}
}
Android图片处理工具类(持续积累补充)
点赞
收藏