天天看点

关于Bitmap对象,尺寸压缩,质量压缩 关于Bitmap对象,尺寸压缩,质量压缩

关于Bitmap对象,尺寸压缩,质量压缩

2016-01-21 17:56  2125人阅读  评论(1)  收藏  举报

关于Bitmap对象,尺寸压缩,质量压缩 关于Bitmap对象,尺寸压缩,质量压缩

  分类: android(39) 

关于Bitmap对象,尺寸压缩,质量压缩 关于Bitmap对象,尺寸压缩,质量压缩

版权声明:本文为博主原创文章,未经博主允许不得转载。

一:在什么时候我们需要对图片质量压缩?

  一般情况下,我们在网上上传图片的时候要压缩图片的质量(体积),因为有的时候服务器对图片大小有限制

二: 在什么时候我们需要对图片尺寸压缩?

        我们从服务器请求的图片通常要转化为Bitmap对象,如果该图片尺寸越大,通常所需要的Bitmap也越大,为防止oom,所以通常我们要对图片进行尺寸压缩

三: 在Bitmap中我们要用到BitmapFactory.Options这个类,所以对这个类的几个属性先进行说明一下

           options.inJustDecodeBounds = true;//设置为true后,在通过BitmapFactory返回对象的时候,并不会真的返回你一个bitmap,而是会返给这个bitmap的宽和高,防止oom

           options.inPurgeable = true;//设置为true 当系统内存不足时可以回收bitmap对象,但是当需要重新编译的时候可以访问存储bitmap的原始数据

   opts.inInputShareable = true;// 设置是否深拷贝,与inPurgeable结合使用

           options.inPreferredConfig = Config.RGB_565;//默认值ARGB_8888改为RGB_565,节约一半内存。

            Bitmap.Config ARGB_4444:每个像素占四位,即A=4,R=4,G=4,B=4,那么一个像素点占4+4+4+4=16位 

Bitmap.Config ARGB_8888:每个像素占四位,即A=8,R=8,G=8,B=8,那么一个像素点占8+8+8+8=32位

Bitmap.Config RGB_565:每个像素占四位,即R=5,G=6,B=5,没有透明度,那么一个像素点占5+6+5=16位

Bitmap.Config ALPHA_8:每个像素占四位,只有透明度,没有颜色

四:图片尺寸压缩的写法

         图片压缩的原理是抽取像素点,分辨率(像素密度大小不变),只改变图片的尺寸

[java]  view plain  copy

  1.     @SuppressWarnings("finally")  
  2.     public static Bitmap getImageThumbnail(String imagePath, int maxWidth,  
  3.             int maxHeight, boolean isDeleteFile) {  
  4.         Bitmap bitmap = null;  
  5.         BitmapFactory.Options options = new BitmapFactory.Options();  
  6.         options.inJustDecodeBounds = true;  
  7.         options.inPurgeable = true;  
  8.         options.inInputShareable = true;  
  9.         options.inPreferredConfig = Config.RGB_565;  
  10.         bitmap = BitmapFactory.decodeFile(imagePath, options);  
  11.         options.inJustDecodeBounds = false;   
  12.         options.inSampleSize = calculateInSampleSize(options, maxWidth, maxHeight);  
  13.         FileInputStream inputStream = null;  
  14.         File file = new File(imagePath);  
  15.         try {  
  16.             if(file != null && file.exists()){  
  17.                 inputStream = new FileInputStream(file);  
  18.                 bitmap = BitmapFactory.decodeStream(inputStream, null,options);  
  19.                 if(isDeleteFile){  
  20.                     file.delete();  
  21.                 }  
  22.             }  
  23.         } catch (IOException e) {  
  24.             e.printStackTrace();  
  25.         } finally {  
  26.             try {  
  27.                 if (inputStream != null)  
  28.                     inputStream.close();  
  29.             } catch (IOException e) {  
  30.                 e.printStackTrace();  
  31.             }  
  32.             return bitmap;  
  33.         }  
  34.     }  

[java]  view plain  copy

  1. </pre><pre name="code" class="java">  

五:图片质量压缩的写法

[java]  view plain  copy

  1. 图片质量压缩的,会使图片占用内存减小(file),像素数不变,所生成bitmap对象大小不变  

[java]  view plain  copy

  1. <pre name="code" class="java">  
  2.     private static Bitmap compressImage(Bitmap image,int size,int options) {  
  3.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  4.         // 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中  
  5.         image.compress(Bitmap.CompressFormat.JPEG, 80, baos);  
  6.         // 循环判断如果压缩后图片是否大于100kb,大于继续压缩  
  7.         while (baos.toByteArray().length / 1024 > size) {  
  8.             options -= 10;// 每次都减少10  
  9.             baos.reset();// 重置baos即清空baos  
  10.             // 这里压缩options%,把压缩后的数据存放到baos中  
  11.             image.compress(Bitmap.CompressFormat.JPEG, options, baos);  
  12.         }  
  13.         // 把压缩后的数据baos存放到ByteArrayInputStream中  
  14.         ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());  
  15.         // 把ByteArrayInputStream数据生成图片  
  16.         Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);  
  17.         return bitmap;  
  18.     }  

[java]  view plain  copy

  1. </pre><pre name="code" class="java">六:两种方法的混合使用  

[java]  view plain  copy

  1. <pre name="code" class="java">   
  2. public static Bitmap compressBitmap(String imgUrl,int reqWidth,int size,int quality){  
  3.     // 创建bitMap  
  4.     BitmapFactory.Options options = new BitmapFactory.Options();  
  5.     options.inJustDecodeBounds = true;  
  6.     BitmapFactory.decodeFile(imgUrl, options);  
  7.     int height = options.outHeight;  
  8.     int width = options.outWidth;  
  9.     int reqHeight;  
  10.     reqHeight = (reqWidth * height) / width;  
  11.     // 在内存中创建bitmap对象,这个对象按照缩放比例创建的  
  12.     options.inSampleSize = calculateInSampleSize(  
  13.             options, reqWidth, reqHeight);  
  14.     options.inJustDecodeBounds = false;  
  15.     Bitmap bm = BitmapFactory.decodeFile(  
  16.             imgUrl, options);  
  17.     Bitmap mBitmap = compressImage(Bitmap.createScaledBitmap(  
  18.             bm, 480, reqHeight, false),size,quality);  
  19.     return  mBitmap;  
  20. }  

[java]  view plain  copy

  1. </pre><pre name="code" class="java">  

[java]  view plain  copy

  1.     private static int calculateInSampleSize(BitmapFactory.Options options,  
  2.                                              int reqWidth, int reqHeight) {  
  3.         final int height = options.outHeight;  
  4.         final int width = options.outWidth;  
  5.         int inSampleSize = 1;  
  6.         if (height > reqHeight || width > reqWidth) {  
  7.             if (width > height) {  
  8.                 inSampleSize = Math.round((float) height / (float) reqHeight);  
  9.             } else {  
  10.                 inSampleSize = Math.round((float) width / (float) reqWidth);  
  11.             }  
  12.         }  
  13.         return inSampleSize;  
  14.     }  

[java]  view plain  copy

  1. </pre><pre name="code" class="java">  

[java]  view plain  copy

  1. </pre><pre name="code" class="java">