[关闭]
@946898963 2022-11-10T02:51:17.000000Z 字数 18567 阅读 1400

Bitmap压缩

Bitmap


在Android系统中,图片占用的内存比较大,由于加载图片导致的内存溢出是Android中内存溢出常见场景之一。所以在加载图片的时候,需要对图片进行相应的处理,关于图片的处理,一般情况下就是要对Bitmap进行合适的处理和优化。

在Android应用中,图片的主要存在方式:

  • 以File的形式存在于SD卡中
  • 以Stream的形式存在于内存中
  • 以Bitmap形式存在于内存中

在SD卡中图片占用的内存与以Stream形式大小一样,均小于Bitmap形式下内存占比。即当图片从SD卡中以流的形式加载到内存中时大小是不会发生变化的,但是stream转化为Bitmap时,其大小会突然变大。这也是为什么当加载大量图片时容易出现OOM的主要原因。

BitmapFactory提供了4类方法加载Bitmap对象:

  1. 1. decodeFile 从文件中加载Bitmap对象
  2. 2. decodeResource 从资源中加载Bitmap对象
  3. 3. decodeStream 从输入流中加载Bitmap对象
  4. 4. decodeByteArray 从字节数组中加载Bitmap对象

其中decodeFile和decodeResource间接的调用了decodeStream方法。

建议阅读:BitmapFactory

为什么转化为Bitmap时大小会突然变大呢?

以任意一张图片为例,我本地存了一张分辨率为750*1334,大小为119K。如果将这张图片以bitmap形式加载到内存中,它占用的大小是多少,如何计算呢?它的计算公式:

  1. 图片的占用内存 = 图片的长度(像素单位) * 图片的宽度(像素单位) * 单位像素所占字节数

其中单位占用字节数的大小是变化的,由BitmapFactory.Options的inPreferredConfig
决定,为Bitmap.Config类型,一般情况下默认为:ARGB_8888。不同类型占字节大小如下表所示:

此处输入图片的描述

可以计算出此图片以bitmap形式加载消耗内存为:750*1334*4=3.81M,还是由于我测试的图片任意找的一张图,现在手机拍的高清图基本上都是2000+ * 2000+ * 4 = 15M+ 了。如果不做任何处理的图片大量使用到我们APP上,结局你懂得!

下面介绍下图片压缩处理的三大基本方式:

  • 尺寸压缩,又分为采样率压缩和缩放压缩。
  • 质量压缩
  • 合理选择Bitmap的像素格式

尺寸压缩:改变宽高,减少像素。

质量压缩:内存不变,压缩转化后的bytes.length减少,适用于传输,png无效。

合理选择Bitmap的像素格式:改变字节数。

尺寸压缩

尺寸压缩会改变图片的尺寸,即压缩图片宽度和高度的像素点,从上面的计算公式我们可以得知,这样会降低图片由Stream转化为bitmap内存的占用,从而一定程度上减少OOM的概率。但是要注意,如果压缩比太大,也会由于像素点降低导致图片失真严重,最后图片由高清成了马赛克。

尺寸压缩主要用在图片资源本身较大,或者适当地采样并不会影响视觉效果的条件下,这时候我们输出的目标可能相对的较小,对图片的大小和分辨率都减小。

尺寸压缩有两种方式:采样率压缩和缩放压缩。

采样率压缩

如何进行尺寸压缩呢?其中一种方式就是合理设置BitmapFactory.Options的inSampleSize参数来设置所需要加载图片的尺寸。记住,是加载所需要合适的尺寸!例如一个ImageView显示图片,基本上ImageView所需要的尺寸不需要原始图片那么大,这时候我们就需要对图片进行一些必要的处理。按照ImageView尺寸大小,通过BitmapFactory.Options来设置恰当的inSampleSize参数值来压缩图片,显示在ImageView上,既避免了出现了OOM,也提高了Bitmap的加载性能。BitmapFactory提供了加载图片的四个方法都支持BitmapFactory.Options参数,通过他们就可以实现对图片进行压缩处理。

通过BitmapF.Options压缩图片的核心就在于inSampleSize参数,采样率或者采样率。采样率为整数,且为2的n次幂,n可以为0。即采样率为1,处理后的图片尺寸与原图一致。当采样率为2时,即宽、高均为原来的1/2,像素则为原来的1/4.其占有内存也为原来的1/4。当设置的采样率小于1时,其效果与1一样。当设置的inSampleSize大于1,不为2的指数时,系统会向下取一个最接近2的指数的值。

举个例子:

当imageView的尺寸为200*300,图片的原始尺寸为600 * 900,则压缩比为3即可。但是如果图片尺寸为1000 * 1800呢?如果采样率设置为5,缩放后的图片尺寸为 200*360,此时还是能接受的。但是如果采样率设置为6,缩放后的图片会小于200 * 300。此时图片会被拉伸导致变形。

压缩图片及获取采样率常规步骤:

  1. 1. BitmapFactory.OptionsinJustDecodeBounds参数设置为true加载图片。
  2. 2. BitmapFactory.Options中获取图片原始的宽高(outWidth/outHeight)值。
  3. 3. 根据采样率规则并结合所需要大小计算出合适的inSampleSize
  4. 4. BitmapFactory.Options参数设置为false,然后重新加载图片。

在上面4步处理后图片基本上就是缩放后的图片了。解释一下inJustDecodeBounds参数,当设置为true时,BitmapFactory只会解析图片的原始宽/高信息,不会真正的去加载图片,所以这个操作是轻量级的。

示例代码:

  1. import android.content.res.Resources;
  2. import android.graphics.Bitmap;
  3. import android.graphics.BitmapFactory;
  4. public class ImageUtil {
  5. /**
  6. * 根据目标View的尺寸压缩图片返回bitmap
  7. * @param resources
  8. * @param resId
  9. * @param width 目标view的宽
  10. * @param height 目标view的高
  11. * @return
  12. */
  13. public static Bitmap decodeBitmapFromResource(Resources resources, int resId,int width ,int height){
  14. BitmapFactory.Options options = new BitmapFactory.Options();
  15. options.inJustDecodeBounds = true;
  16. BitmapFactory.decodeResource(resources,resId,options);
  17. //获取采样率
  18. options.inSampleSize = calculateInSampleSize(options,width,height);
  19. options.inJustDecodeBounds = false;
  20. return BitmapFactory.decodeResource(resources,resId,options);
  21. }
  22. /**
  23. * 获取采样率
  24. * @param options
  25. * @param reqWidth 目标view的宽
  26. * @param reqHeight 目标view的高
  27. * @return 采样率
  28. */
  29. private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
  30. int originalWidth = options.outWidth;
  31. int originalHeight = options.outHeight;
  32. int inSampleSize = 1;
  33. if (originalHeight > reqHeight || originalWidth > reqHeight){
  34. int halfHeight = originalHeight / 2;
  35. int halfWidth = originalWidth / 2;
  36. //压缩后的尺寸与所需的尺寸进行比较
  37. while ((halfWidth / inSampleSize) >= reqHeight && (halfHeight /inSampleSize)>=reqWidth){
  38. inSampleSize *= 2;
  39. }
  40. }
  41. return inSampleSize;
  42. }
  43. }

官方文档方法:

  1. /**
  2. * 计算压缩的比例
  3. *
  4. * @param options 解析图片所需的BitmapFactory.Options
  5. * @param minSideLength 调整后图片最小的宽或高值,一般赋值为 -1
  6. * @param maxNumOfPixels 调整后图片的内存占用量上限
  7. * @return
  8. */
  9. public static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
  10. int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);
  11. int roundedSize;
  12. if (initialSize <= 8) {
  13. roundedSize = 1;
  14. while (roundedSize < initialSize) {
  15. roundedSize <<= 1;
  16. }
  17. } else {
  18. roundedSize = (initialSize + 7) / 8 * 8;
  19. }
  20. return roundedSize;
  21. }
  22. /**
  23. * 计算原始大小
  24. *
  25. * @param options 解析图片所需的BitmapFactory.Options
  26. * @param minSideLength 调整后图片最小的宽或高值,一般赋值为 -1
  27. * @param maxNumOfPixels 调整后图片的内存占用量上限
  28. * @return
  29. */
  30. private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
  31. double w = options.outWidth;
  32. double h = options.outHeight;
  33. int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
  34. int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));
  35. if (upperBound < lowerBound) {
  36. // return the larger one when there is no overlapping zone.
  37. return lowerBound;
  38. }
  39. if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
  40. return 1;
  41. } else if (minSideLength == -1) {
  42. return lowerBound;
  43. } else {
  44. return upperBound;
  45. }
  46. }

上面示例代码中采用的是decodeResource方法,其它几个方法基本类似.

缩放法压缩

样板代码:

  1. Matrix matrix = new Matrix();
  2. matrix.setScale(0.5f, 0.5f);
  3. bm = Bitmap.createBitmap(bit, 0, 0, bit.getWidth(),bit.getHeight(), matrix, true);
  4. Log.i("wechat", "压缩后图片的大小" + (bm.getByteCount() / 1024 / 1024)+ "M宽度为" + bm.getWidth() + "高度为" + bm.getHeight());

与采样率法类似。放缩法压缩使用的是通过矩阵对图片进行裁剪,也是通过缩放图片尺寸,来达到压缩图片的效果,和采样率的原理一样。

  1. /**
  2. * 矩阵缩放图片
  3. * @param sourceBitmap
  4. * @param width 要缩放到的宽度
  5. * @param height 要缩放到的长度
  6. * @return
  7. */
  8. private Bitmap getScaleBitmap(Bitmap sourceBitmap,float width,float height){
  9. Bitmap scaleBitmap;
  10. //定义矩阵对象
  11. Matrix matrix = new Matrix();
  12. float scale_x = width/sourceBitmap.getWidth();
  13. float scale_y = height/sourceBitmap.getHeight();
  14. matrix.postScale(scale_x,scale_y);
  15. try {
  16. scaleBitmap = Bitmap.createBitmap(sourceBitmap,0,0,sourceBitmap.getWidth(),sourceBitmap.getHeight(),matrix,true);
  17. }catch (OutOfMemoryError e){
  18. scaleBitmap = null;
  19. System.gc();
  20. }
  21. return scaleBitmap;
  22. }

其实除了使用矩阵之外,还可以使用createScaledBitmap方法对Bitmap进行缩放。

  1. bm = Bitmap.createScaledBitmap(bit, 150, 150, true);
  2. Log.i("wechat", "压缩后图片的大小" + (bm.getByteCount() / 1024) + "KB宽度为"
  3. + bm.getWidth() + "高度为" + bm.getHeight());

这里是将图片压缩成用户所期望的长度和宽度,但是这里要说,如果用户期望的长度和宽度和原图长度宽度相差太多的话,图片会很不清晰。

质量压缩

质量压缩不会改变图片的像素点,即前面我们说到的在转化为bitmap时占用内存变大时状况不会得到改善。因为质量压缩是在保持像素的前提下改变图片的位深及透明度,来达到压缩图片的目的,图片的长,宽,像素都不会改变,那么bitmap所占内存大小是不会变的。

但是质量压缩可以减少我们存储在本地文件的大小,即放到disk上的大小。质量压缩能方便的设置压缩百分比,达到我们需要的大小。还是先看方法:

  1. Bitmap.compress(CompressFormat format, int quality, OutputStream stream)

简单解释一下这三个参数,第一个表示Bitmap被压缩成的图片格式Bitmap.CompressFormat;第二个表示压缩的质量控制,范围0~100,很好理解。quality为80,表示压缩为原来80%的质量效果。有些格式,例如png,它是无损的,这个值设置了也是无效的。因为质量压缩是在保持像素前提下改变图片的位深及透明度等来压缩图片的,所以quality值与最后生成的图片的大小并不是线性关系,比如大小为300k的图片,当quality为90时,得到的图片大小并不是为270K。大家通过代码测试一下就会发现了。

质量压缩不会改变图片的像素点,即我们使用完质量压缩后,在转换Bitmap时占用内存依旧不会减小。但是可以减少我们存储在本地文件的大小,即放到disk上的大小。

样板代码:

  1. val baos = ByteArrayOutputStream()
  2. // 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
  3. bmRaw.compress(Bitmap.CompressFormat.JPEG, 50, baos)
  4. val bais = ByteArrayInputStream(baos.toByteArray())
  5. val bmScaled = BitmapFactory.decodeStream(bais, null, null)

说明:

使用JPEG格式的质量压缩

  1. bmRaw.compress(Bitmap.CompressFormat.JPEG, 50, baos)
  • 对一张透明图片(png),内存、宽高不变,bytes.length 减少。图片会失去透明度,透明处变黑。

  • 对一张非透明图片(png、jpg),内存、宽高不变,bytes.length 减少。

使用 PNG 格式的质量压缩

  1. bmRaw.compress(Bitmap.CompressFormat.PNG, 50, baos)
  • 对一张透明图片(png),没有影响

  • 对一张非透明图片(png、jpg),没有影响

--

  • CompressFormat.JPEG,

  • CompressFormat.PNG, PNG 格式是无损的,它无法再进行质量压缩,quality 这个参数就没有作用了,会被忽略,所以最后图片保存成的文件大小不会有变化;

  • CompressFormat.WEBP ,这个格式是 google 推出的图片格式,它会比 JPEG 更加省空间,经过实测大概可以优化 30% 左右。

核心代码:

  1. /**
  2. * 质量压缩方法,并不能减小加载到内存时所占用内存的空间,应该是减小的所占用磁盘的空间
  3. * @param image
  4. * @param compressFormat
  5. * @return
  6. */
  7. public static Bitmap compressbyQuality(Bitmap image, Bitmap.CompressFormat compressFormat) {
  8. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  9. image.compress(compressFormat, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
  10. int quality = 100;
  11. while ( baos.toByteArray().length / 1024 > 100) { //循环判断如果压缩后图片是否大于100kb,大于继续压缩
  12. baos.reset();//重置baos即清空baos
  13. if(quality > 20){
  14. quality -= 20;//每次都减少20
  15. }else {
  16. break;
  17. }
  18. image.compress(Bitmap.CompressFormat.JPEG, quality, baos);//这里压缩options%,把压缩后的数据存放到baos中
  19. }
  20. ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中
  21. BitmapFactory.Options options = new BitmapFactory.Options();
  22. options.inPreferredConfig = Bitmap.Config.RGB_565;
  23. Bitmap bmp = BitmapFactory.decodeStream(isBm, null, options);//把ByteArrayInputStream数据生成图片
  24. return bmp;
  25. }
  1. /**
  2. * 质量压缩并存到SD卡中
  3. * @param bitmap
  4. * @param reqSize 需要的大小
  5. * @return
  6. */
  7. public static String qualityCompress1(Bitmap bitmap ,int reqSize){
  8. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  9. //这里100表示不压缩,把压缩后的数据存放到baos中
  10. bitmap.compress(Bitmap.CompressFormat.JPEG,100,baos);
  11. int options = 95;
  12. //如果压缩后的大小超出所要求的,继续压缩
  13. while (baos.toByteArray().length / 1024 > reqSize){
  14. baos.reset();
  15. //每次减少5%质量
  16. if (options>5){//避免出现options<=0
  17. options -=5;
  18. } else {
  19. break;
  20. }
  21. bitmap.compress(Bitmap.CompressFormat.JPEG,options,baos);
  22. }
  23. //存入SD卡中
  24. SimpleDateFormat formatYMD = new SimpleDateFormat("yyyy/MM/dd");
  25. String compressImgUri = LOCAL_URL + "000/"
  26. + formatYMD.format(new Date()) + "/" + System.currentTimeMillis() + "a.jpg";
  27. File outputFile = new File(compressImgUri);
  28. if (!outputFile.exists()) {
  29. outputFile.getParentFile().mkdirs();
  30. } else {
  31. outputFile.delete();
  32. }
  33. FileOutputStream out = null;
  34. try {
  35. out = new FileOutputStream(outputFile);
  36. } catch (FileNotFoundException e) {
  37. e.printStackTrace();
  38. }
  39. bitmap.compress(Bitmap.CompressFormat.JPEG, options, out);
  40. return outputFile.getPath();
  41. }
  42. private void compressQuality() {
  43. Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.test);
  44. mSrcSize = bm.getByteCount() + "byte";
  45. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  46. bm.compress(Bitmap.CompressFormat.JPEG, 100, bos);
  47. byte[] bytes = bos.toByteArray();
  48. mSrcBitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
  49. }

代码相当简单,前面基本解释过了。

合理选择Bitmap的像素格式

ARG_B8888格式的图片,每像素占用4Byte,而RGB_565则是2Byte。显而易见,不同的像素格式其Bitmap的大小也就不同。

关于像素格式,建议阅读:Bitmap.Config

综合使用

压缩并保存图片

  1. /**
  2. * 压缩并保存图片
  3. * <p>
  4. * 图片小于50k
  5. *
  6. * @param localUrl 本地图片路径(例如:/storage/emulated/0/Pictures/multi_image_20180808_130928.jpg)
  7. * @param path 目标文件路径(例如:/storage/emulated/0/zhcx/img/cgzf/)
  8. * @param filename 文件名称(例如:33000019_0.jpg)
  9. */
  10. public static void saveCgzfPic(String localUrl, String path, String filename) {
  11. //获得图片的宽和高,但并不把图片加载到内存当中
  12. BitmapFactory.Options options = new BitmapFactory.Options();
  13. options.inPreferredConfig = Bitmap.Config.RGB_565;
  14. options.inJustDecodeBounds = true;
  15. BitmapFactory.decodeFile(localUrl, options);
  16. options.inSampleSize = computeSampleSize(options, -1, (int) (0.5 * 1024 * 1024));
  17. //使用获取到的inSampleSize再次解析图片
  18. options.inJustDecodeBounds = false;
  19. Bitmap bitmap = BitmapFactory.decodeFile(localUrl, options);
  20. LogUtil.myD("inSampleSize:" + options.inSampleSize);
  21. File rootFile = new File(path);
  22. if (!rootFile.exists()) {
  23. rootFile.mkdirs();
  24. }
  25. File file = new File(rootFile, filename);
  26. try {
  27. if (!file.exists()) {
  28. file.createNewFile();
  29. }
  30. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  31. bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
  32. //质量压缩
  33. int quality = 95;
  34. while (baos.toByteArray().length / 1024 > 50) { // 循环判断如果压缩后图片是否大于50kb,大于继续压缩
  35. //每次减少5%质量
  36. if (quality > 5) {//避免出现options<=0
  37. quality -= 5;
  38. } else {
  39. break;
  40. }
  41. LogUtil.d("caowj", "length:" + baos.toByteArray().length + ",,,quality:" + quality);
  42. baos.reset(); // 重置baos即清空baos
  43. bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);// 这里压缩options%,把压缩后的数据存放到baos中
  44. }
  45. LogUtil.d("caowj", "2length:" + baos.toByteArray().length + ",,,2quality:" + quality);
  46. //保存图片
  47. FileOutputStream fos = new FileOutputStream(file);
  48. bitmap.compress(Bitmap.CompressFormat.JPEG, quality, fos);
  49. //// TODO: 2018/8/8 问题:byte数组解析成bitmap后,再次解析成byte数组,变大了,为什么?
  50. // ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中
  51. // L.d("caowj", "3length:" + baos.toByteArray().length);
  52. //
  53. // Bitmap bitmap2 = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream数据生成图片
  54. //
  55. // bitmap2.compress(Bitmap.CompressFormat.JPEG, 100, baos);
  56. // L.d("caowj", "sss:" + baos.toByteArray().length);
  57. // bitmap2.compress(Bitmap.CompressFormat.JPEG, 100, fos);
  58. fos.flush();
  59. fos.close();
  60. if (bitmap.isRecycled()) {
  61. bitmap.recycle();
  62. }
  63. } catch (Exception e) {
  64. e.printStackTrace();
  65. }
  66. }
  67. /**
  68. * 计算压缩的比例
  69. *
  70. * @param options 解析图片所需的BitmapFactory.Options
  71. * @param minSideLength 调整后图片最小的宽或高值,一般赋值为 -1
  72. * @param maxNumOfPixels 调整后图片的内存占用量上限
  73. * @return
  74. */
  75. public static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
  76. int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);
  77. int roundedSize;
  78. if (initialSize <= 8) {
  79. roundedSize = 1;
  80. while (roundedSize < initialSize) {
  81. roundedSize <<= 1;
  82. }
  83. } else {
  84. roundedSize = (initialSize + 7) / 8 * 8;
  85. }
  86. return roundedSize;
  87. }
  88. /**
  89. * 计算原始大小
  90. *
  91. * @param options 解析图片所需的BitmapFactory.Options
  92. * @param minSideLength 调整后图片最小的宽或高值,一般赋值为 -1
  93. * @param maxNumOfPixels 调整后图片的内存占用量上限
  94. * @return
  95. */
  96. private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
  97. double w = options.outWidth;
  98. double h = options.outHeight;
  99. int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
  100. int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));
  101. if (upperBound < lowerBound) {
  102. // return the larger one when there is no overlapping zone.
  103. return lowerBound;
  104. }
  105. if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
  106. return 1;
  107. } else if (minSideLength == -1) {
  108. return lowerBound;
  109. } else {
  110. return upperBound;
  111. }
  112. }

下面的例子主要用到了Bitmap的采样压缩,同时使用到了inBitmap内存复用(深入理解Android Bitmap的各种操作,原来的似乎有问题,下面的是自己改动的)。

  1. /**
  2. * 采样率压缩,这个和矩阵来实现缩放有点类似,但是有一个原则是“大图小用用采样,小图大用用矩阵”。
  3. * 也可以先用采样来压缩图片,这样内存小了,可是图的尺寸也小。如果要是用 Canvas 来绘制这张图时,再用矩阵放大
  4. * @param image
  5. * @param compressFormat
  6. * @param requestWidth 要求的宽度
  7. * @param requestHeight 要求的长度
  8. * @return
  9. */
  10. public static Bitmap compressbySample(Bitmap image, Bitmap.CompressFormat compressFormat, int requestWidth, int requestHeight){
  11. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  12. image.compress(compressFormat, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
  13. ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中
  14. BitmapFactory.Options options = new BitmapFactory.Options();
  15. options.inPreferredConfig = Bitmap.Config.RGB_565;
  16. options.inPurgeable = true;
  17. options.inJustDecodeBounds = true;//只读取图片的头信息,不去解析真是的位图
  18. BitmapFactory.decodeStream(isBm,null,options);
  19. options.inSampleSize = calculateInSampleSize(options,requestWidth,requestHeight);
  20. //-------------inBitmap------------------
  21. options.inMutable = true;
  22. try{
  23. if (image != null && canUseForInBitmap(image, options)) {
  24. options.inBitmap = image;
  25. }
  26. }catch (OutOfMemoryError e){
  27. options.inBitmap = null;
  28. System.gc();
  29. }
  30. //---------------------------------------
  31. options.inJustDecodeBounds = false;//真正的解析位图
  32. isBm.reset();
  33. Bitmap compressBitmap;
  34. try{
  35. compressBitmap = BitmapFactory.decodeStream(isBm, null, options);//把ByteArrayInputStream数据生成图片
  36. }catch (OutOfMemoryError e){
  37. compressBitmap = null;
  38. System.gc();
  39. }
  40. return compressBitmap;
  41. }
  42. /**
  43. * 采样压缩比例
  44. * @param options
  45. * @param reqWidth 要求的宽度
  46. * @param reqHeight 要求的长度
  47. * @return
  48. */
  49. private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
  50. int originalWidth = options.outWidth;
  51. int originalHeight = options.outHeight;
  52. int inSampleSize = 1;
  53. if (originalHeight > reqHeight || originalWidth > reqHeight){
  54. // 计算出实际宽高和目标宽高的比率
  55. final int heightRatio = Math.round((float) originalHeight / (float) reqHeight);
  56. final int widthRatio = Math.round((float) originalWidth / (float) reqWidth);
  57. // 选择宽和高中最小的比率作为inSampleSize的值,这样可以保证最终图片的宽和高
  58. // 一定都会大于等于目标的宽和高。
  59. inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
  60. }
  61. return inSampleSize;
  62. }
  63. //用来判断bitmap是否能够被复用。
  64. static boolean canUseForInBitmap(Bitmap candidate, BitmapFactory.Options targetOptions) {
  65. if (candidate == null || !candidate.isMutable()) {
  66. return false;
  67. }
  68. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  69. // From Android 4.4 (KitKat) onward we can re-use if the byte size of
  70. // the new bitmap is smaller than the reusable bitmap candidate allocation byte count.
  71. int width = targetOptions.outWidth / targetOptions.inSampleSize;
  72. int height = targetOptions.outHeight / targetOptions.inSampleSize;
  73. int byteCount = width * height * getBytesPerPixel(targetOptions.inPreferredConfig);
  74. return byteCount <= candidate.getAllocationByteCount();
  75. }
  76. // On earlier versions, the dimensions must match exactly and the inSampleSize must be 1
  77. return candidate.getWidth() == targetOptions.outWidth && candidate.getHeight() == targetOptions.outHeight && targetOptions.inSampleSize == 1 && candidate.getConfig() == targetOptions.inPreferredConfig;
  78. }
  79. /**
  80. * A helper function to return the byte usage per pixel of a bitmap based on its configuration.
  81. */
  82. static int getBytesPerPixel(Bitmap.Config config) {
  83. if (config == Bitmap.Config.ARGB_8888) {
  84. return 4;
  85. } else if (config == Bitmap.Config.RGB_565) {
  86. return 2;
  87. } else if (config == Bitmap.Config.ARGB_4444) {
  88. return 2;
  89. } else if (config == Bitmap.Config.ALPHA_8) {
  90. return 1;
  91. }
  92. return 1;
  93. }

拍照或者相册选择图片后,能得到一个图片的路径,首先需要根据图片路径,利用采样率压缩一次,如BitmapUtils.decodeBitmapFromFilePath(“图片路径”, 800,800);如此操作得到的bitmap再压缩为字节输出流,然后写入file,就可以上传服务器了(当然,上传图片到服务器有多种方式)。bitmap再压缩为字节输出流(可以判断字节流大小,再决定是否继续压缩)再写入file,是耗时操作,必须放到子线程中执行。
如下所示:

  1. private String pathSource;//原图文件路径
  2. private String pathCompressed;//压缩后的图片文件路径
  3. private int kb_max = 1000;//压缩到多少KB,不能精确,只能<=kb_max
  4. private int quality_max = 80;//压缩精度,尽量>=50
  5. private int reqWidth = 1000;//期望的图片宽度
  6. private int reqHeight = 1000;//期望的图片高度
  1. final Bitmap bitmap = decodeBitmapFromFilePath(compressFileBean.getPathSource(), compressFileBean.getReqWidth(), compressFileBean.getReqHeight());
  2. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  3. int quality = 80;
  4. //压缩格式选取JPEG就行了,quality,压缩精度尽量不要低于50,否则影响清晰度
  5. bitmap.compress(Bitmap.CompressFormat.JPEG, quality, byteArrayOutputStream);
  6. while (byteArrayOutputStream.toByteArray().length / 1024 > compressFileBean.getKb_max() && quality > compressFileBean.getQuality_max()) {
  7. // 循环判断如果压缩后图片是否大于kb_max kb,大于继续压缩,
  8. byteArrayOutputStream.reset();
  9. quality -= 10;
  10. bitmap.compress(Bitmap.CompressFormat.JPEG, quality, byteArrayOutputStream);
  11. }
  12. try {
  13. final File fileCompressed = createFile(compressFileBean.getPathCompressed());
  14. FileOutputStream fileOutputStream = new FileOutputStream(fileCompressed);
  15. fileOutputStream.write(byteArrayOutputStream.toByteArray());//写入目标文件
  16. fileOutputStream.flush();
  17. fileOutputStream.close();
  18. byteArrayOutputStream.close();
  19. if (fileCompressed != null && fileCompressed.length() > 0)
  20. runOnUiThread(new Runnable() {
  21. @Override
  22. public void run() {
  23. //压缩成功
  24. compressFileCallback.onCompressFileFinished(fileCompressed, bitmap);
  25. }
  26. });
  27. } catch (final Exception e) {
  28. e.printStackTrace();
  29. runOnUiThread(new Runnable() {
  30. @Override
  31. public void run() {
  32. //压缩失败
  33. compressFileCallback.onCompressFileFailed("压缩图片文件失败" + e.getMessage());
  34. }
  35. });
  36. }

使用示例:

  1. BitmapUtils.compressFile(new BitmapUtils.CompressFileBean.Builder()
  2. .setFileSource(file.getAbsolutePath())
  3. .setFileCompressed(getExternalCacheDir() + File.separator + "Feedback" + File.separator + System.currentTimeMillis() + ".jpg")
  4. .setKb_max(100)
  5. .setQuality_max(50)
  6. .setReqWidth(800)
  7. .setReqHeight(800).build(), new BitmapUtils.CompressFileCallback() {
  8. @Override
  9. public void onCompressFileFinished(File file, Bitmap bitmap) {
  10. // if (feedbackUI.getRvAdapter().getList_bean().size() == 3)
  11. // feedbackUI.getRvAdapter().setHaveFootView(false);
  12. // feedbackUI.getRvAdapter().add(file);
  13. }
  14. @Override
  15. public void onCompressFileFailed(String s) {
  16. }
  17. });

整理的相关工具类BitmapUtils.java

图片很多时候压缩的过程比较耗时,不要放到主线程执行。由于质量压缩并不会减少图片转换为bitmap时的内存消耗,如果是为了避免出现OOM,建议选择合适的像素格式同时进行适当的尺寸压缩即可,但是如果是为了减少上传时候的流量消耗,则可以三者结合起来使用,即先选择合适的像素格式进行适当的尺寸压缩,然后再进一步进行质量压缩。

项目地址:点击下载

  1. public void reset()
  2. 将此字节数组输出流的count字段重置为零,从而丢弃输出流中目前已累积的所有数据输出。

详解Bitmap尺寸压缩与质量压缩

深入理解Android Bitmap的各种操作

Bitmap 的四种压缩方式详解

bitmap的六种压缩方式,Android图片压缩(有代码,有详细打印大小变化)

Android Bitmap的常用压缩方式

Bitmap压缩方法

bitmap 质量压缩并保存到本地

Android中高效的显示图片 - Bitmap的内存模型

Android Bitmap压缩的正确姿势

Java ByteArrayOutputStream类

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注