android 图片压缩算法

来源:互联网 发布:淘宝部分店铺无法使用 编辑:程序博客网 时间:2024/04/30 23:04

由源图像压缩为固定目标尺寸的图像。

1.求源宽,高为计算压缩尺寸做准备。

BitampFactory.Options.inSampleSize  控制BitmapFactory分配恰当的内存空间。

BitampFactory.Options.inJustDecodeBounds  为true可以在decodeFile并且不分配内存空间,但可以计算出源图像的width和height。


2.根据reqwidth,reqheight,求inSampleSize。

if  width>reqwidth || height > reqheight

   int heightRatio = height / reqheight;

   int widthRatio = width /reqwidth;

   inSampleSize = heightRatio < widthRratio ? heightRatio : widthRatio;    // 保障最后imageview的dimension比request的大

  // 对于图片很长或很宽的情况做判断

   totalPixels = width * height;

   reqtotalPixels = reqwidth * reqheight * 2;

   while (totalPixels / (inSampleSize * inSampleSize) > reqtotalPixels){

       inSampleSize++;

   } 


return inSampleSize;

3.品质压缩 (android 原生的压缩)

Bitmap.Compress  参数阈值参考在75

当然质量要求高需要采用第三库了。

0 0