import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class ImageTool {
/**
* 缩放图片
* @author silent
* @param data 数据
* @param desWidth 目标宽
* @param desHeight 目标高
*
* */
public static Bitmap decodeBitmap(byte[] data, int desWidth, int desHeight) {
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;// 设置成了true,不占用内存,只获取bitmap宽高
//BitmapFactory.decodeFile(filePath, opts);
BitmapFactory.decodeByteArray(data, 0, data.length, opts);
//如,insamplesize = = 4返回一个图像是1 / 4的宽度/高度 ,和1 / 16像素数。
//opts.inSampleSize = computeSampleSize(opts, -1, 1024 * 800);
opts.inSampleSize = computeSampleSize(opts, -1, desWidth * desHeight);
opts.inJustDecodeBounds = false;// 这里一定要将其设置回false,因为之前我们将其设置成了true
Bitmap bitmap = null;
try {
// bitmap = BitmapFactory.decodeStream(is, null, opts);
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opts);
} catch (Exception e) {
// TODO: handle exception
}
return bitmap;
}
/* //控制图片内存溢出
public static Bitmap decodeBitmap(String filePath) {
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;// 设置成了true,不占用内存,只获取bitmap宽高
BitmapFactory.decodeFile(filePath, opts);
//如,insamplesize = = 4返回一个图像是1 / 4的宽度/高度 ,和1 / 16像素数。
opts.inSampleSize = computeSampleSize(opts, -1, 1024 * 800);
opts.inJustDecodeBounds = false;// 这里一定要将其设置回false,因为之前我们将其设置成了true
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeFile(filePath, opts);
} catch (Exception e) {
// TODO: handle exception
}
return bitmap;
}*/
/**
* 计算图片缩放多少倍
* @param minSideLength 最小边长
* @param maxNumOfPixels 最大像素
* **/
public static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);
int roundedSize;
if (initialSize <= 8) {
roundedSize = 1;
//比如 计算出 initialSize 为4 name 则 roundedSize 结果为4
//第一次 1<4 》》 roundedSize = 2 第二次 2<4 》》 roundedSize = 4 然后跳出循环
//这样写的目的应该是解码器是基于 2的 倍数解析的,如果 initialSize = 5 则 roundedSize = 6;
while (roundedSize < initialSize) {
roundedSize <<= 1;
}
} else {
//如果大于8 就基于 8 向上取整。 如 initialSize = 9 取整后roundedSize 为 16
roundedSize = (initialSize + 7) / 8 * 8;
}
return roundedSize;
}
/**
* 计算图片缩放多少倍
* @param minSideLength 最小边长
* @param maxNumOfPixels 最大像素
* **/
private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
double w = options.outWidth;
double h = options.outHeight;
//下限 : Math.ceil() 向上取整 如 Math.ceil(12.2)//返回13 这里计算 原图片是 目标最大图片的几倍
int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
//上限: Math.floor() 向下取整. 如 floor(9.999999) = 9.0 floor(-3.14) = -4.0 原图最小边 是目标最小边的几倍
int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));
if (upperBound < lowerBound) {
return lowerBound;
}
if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
return 1;
} else if (minSideLength == -1) {
return lowerBound;
} else {
return upperBound;
}
}
}
调用方式:
Bitmap bitmap = ImageTool.decodeBitmap(data, desWidth, desHeight);