图片压缩有两种方式,一种质量压缩:即损失图片的质量为代价,大小不变;像素压缩:通过inSampleSize减少长宽方向的像素数量。
BitmapFactory 在解码图片时,可以带一个Options,有一些比较有用的功能,比如:
-
inTargetDensity 表示要被画出来时的目标像素密度
-
inSampleSize 这个值是一个int,当它小于1的时候,将会被当做1处理,如果大于1,那么就会按照比例(1 / inSampleSize)缩小bitmap的宽和高、降低分辨率,大于1时这个值将会被处置为2的倍数。例如,width=100,height=100,inSampleSize=2,那么就会将bitmap处理为,width=50,height=50,宽高降为1 / 2,像素数降为1 / 4。注意:inSampleSize只能是2的指数,1,2,4,8,16等,如果输入的数值不是2的指数,系统会自动取一个小于该数值的最大的2的指数来代替,比如输入3会按2来压缩,输入7会按4来压缩
-
inJustDecodeBounds 字面意思就可以理解就是只解析图片的边界,有时如果只是为了获取图片的大小就可以用这个,而不必直接加载整张图片。
-
inPreferredConfig 默认会使用ARGB_8888,在这个模式下一个像素点将会占用4个byte,而对一些没有透明度要求或者图片质量要求不高的图片,可以使用RGB_565,一个像素只会占用2个byte,一下可以省下50%内存。
-
inPurgeable和inInputShareable 这两个需要一起使用,BitmapFactory.java的源码里面有注释,大致意思是表示在系统内存不足时是否可以回收这个bitmap,有点类似软引用,但是实际在5.0以后这两个属性已经被忽略,因为系统认为回收后再解码实际会反而可能导致性能问题
-
inBitmap 官方推荐使用的参数,表示重复利用图片内存,减少内存分配,在4.4以前只有相同大小的图片内存区域可以复用,4.4以后只要原有的图片比将要解码的图片大既可以复用了。glide使用来该参数
/**
* 多线程压缩图片的质量
*
* @param bitmap 内存中的图片
* @param imgPath 图片的保存路径
* @author JPH
* @date 2014-12-5下午11:30:43
*/
private void compressImageByQuality(final Bitmap bitmap, final String imgPath, final CompressListener listener) {
if (bitmap == null) {
sendMsg(false, imgPath, "像素压缩失败,bitmap is null", listener);
return;
}
new Thread(new Runnable() {//开启多线程进行压缩处理
@Override
public void run() {
// TODO Auto-generated method stub
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int options = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);//质量压缩方法,把压缩后的数据存放到baos中 (100表示不压缩,0表示压缩到最小)
while (baos.toByteArray().length > config.getMaxSize()) {//循环判断如果压缩后图片是否大于指定大小,大于继续压缩
baos.reset();//重置baos即让下一次的写入覆盖之前的内容
options -= 5;//图片质量每次减少5
if (options <= 5) options = 5;//如果图片质量小于5,为保证压缩后的图片质量,图片最底压缩质量为5
bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);//将压缩后的图片保存到baos中
if (options == 5) break;//如果图片的质量已降到最低则,不再进行压缩
}
// if(bitmap!=null&&!bitmap.isRecycled()){
// bitmap.recycle();//回收内存中的图片
// }
try {
File thumbnailFile = getThumbnailFile(new File(imgPath));
FileOutputStream fos = new FileOutputStream(thumbnailFile);//将压缩后的图片保存的本地上指定路径中
fos.write(baos.toByteArray());
fos.flush();
fos.close();
sendMsg(true, thumbnailFile.getPath(), null, listener);
} catch (Exception e) {
sendMsg(false, imgPath, "质量压缩失败", listener);
e.printStackTrace();
}
}
}).start();
}
/**
* 按比例缩小图片的像素以达到压缩的目的
*
* @param imgPath
* @return
* @author JPH
* @date 2014-12-5下午11:30:59
*/
private void compressImageByPixel(String imgPath, CompressListener listener) throws FileNotFoundException {
if (imgPath == null) {
sendMsg(false, imgPath, "要压缩的文件不存在", listener);
return;
}
BitmapFactory.Options newOpts = new BitmapFactory.Options();
newOpts.inJustDecodeBounds = true;//只读边,不读内容,只是为了获取宽高
BitmapFactory.decodeFile(imgPath, newOpts);// 这里返回的bitmap是null,这段代码后,options.outWidth 和 options.outHeight就是我们想要的宽和高了
newOpts.inJustDecodeBounds = false;
int width = newOpts.outWidth;
int height = newOpts.outHeight;
float maxSize = config.getMaxPixel();
int be = 1;
if (width >= height && width > maxSize) {//缩放比,用高或者宽其中较大的一个数据进行计算
be = (int) (newOpts.outWidth / maxSize);
be++;
} else if (width < height && height > maxSize) {
be = (int) (newOpts.outHeight / maxSize);
be++;
}
newOpts.inSampleSize = be;//设置采样率
newOpts.inPreferredConfig = Config.ARGB_8888;//该模式是默认的,可不设
newOpts.inPurgeable = true;// 同时设置才会有效
newOpts.inInputShareable = true;//。当系统内存不够时候图片自动被回收
Bitmap bitmap = BitmapFactory.decodeFile(imgPath, newOpts);
if (config.isEnableQualityCompress()) {
compressImageByQuality(bitmap, imgPath, listener);//压缩好比例大小后再进行质量压缩
} else {
File thumbnailFile = getThumbnailFile(new File(imgPath));
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(thumbnailFile));
listener.onCompressSuccess(thumbnailFile.getPath());
}
}