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

 

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

  2125人阅读  评论(1)  收藏  举报
  分类:

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

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


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

        我们从服务器请求的图片通常要转化为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. /** 
  2.      *  
  3.      * @param imagePath 
  4.      * @param maxWidth 
  5.      * @param maxHeight 
  6.      * @return 
  7.      */  
  8.     @SuppressWarnings("finally")  
  9.     public static Bitmap getImageThumbnail(String imagePath, int maxWidth,  
  10.             int maxHeight, boolean isDeleteFile) {  
  11.         Bitmap bitmap = null;  
  12.         BitmapFactory.Options options = new BitmapFactory.Options();  
  13.         options.inJustDecodeBounds = true;  
  14.         options.inPurgeable = true;  
  15.         options.inInputShareable = true;  
  16.         options.inPreferredConfig = Config.RGB_565;  
  17.         bitmap = BitmapFactory.decodeFile(imagePath, options);  
  18.         options.inJustDecodeBounds = false;   
  19.         options.inSampleSize = calculateInSampleSize(options, maxWidth, maxHeight);  
  20.         FileInputStream inputStream = null;  
  21.         File file = new File(imagePath);  
  22.         try {  
  23.             if(file != null && file.exists()){  
  24.                 inputStream = new FileInputStream(file);  
  25.                 bitmap = BitmapFactory.decodeStream(inputStream, null,options);  
  26.                 if(isDeleteFile){  
  27.                     file.delete();  
  28.                 }  
  29.             }  
  30.         } catch (IOException e) {  
  31.             e.printStackTrace();  
  32.         } finally {  
  33.             try {  
  34.                 if (inputStream != null)  
  35.                     inputStream.close();  
  36.             } catch (IOException e) {  
  37.                 e.printStackTrace();  
  38.             }  
  39.             return bitmap;  
  40.         }  
  41.     }  


[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.      * 质量压缩图片,图片占用内存减小,像素数不变,常用于上传 
  3.      * @param image 
  4.      * @param size 期望图片的大小,单位为kb 
  5.      * @param options 图片压缩的质量,取值1-100,越小表示压缩的越厉害,如输入30,表示压缩70% 
  6.      * @return 
  7.      */  
  8.     private static Bitmap compressImage(Bitmap image,int size,int options) {  
  9.   
  10.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  11.         // 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中  
  12.         image.compress(Bitmap.CompressFormat.JPEG, 80, baos);  
  13.         // 循环判断如果压缩后图片是否大于100kb,大于继续压缩  
  14.         while (baos.toByteArray().length / 1024 > size) {  
  15.             options -= 10;// 每次都减少10  
  16.             baos.reset();// 重置baos即清空baos  
  17.             // 这里压缩options%,把压缩后的数据存放到baos中  
  18.             image.compress(Bitmap.CompressFormat.JPEG, options, baos);  
  19.         }  
  20.         // 把压缩后的数据baos存放到ByteArrayInputStream中  
  21.         ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());  
  22.         // 把ByteArrayInputStream数据生成图片  
  23.         Bitmap bitmap = BitmapFactory.decodeStream(isBm, nullnull);  
  24.         return bitmap;  
  25.     }  
 
 
[java]  view plain  copy
  1. </pre><pre name="code" class="java">六:两种方法的混合使用  
[java]  view plain  copy
  1. <pre name="code" class="java"/** 
  2.  * 二次压缩,先按照像素压缩再按照质量压缩 
  3.  * @param imgUrl 图片路径 
  4.  * @param reqWidth 期望宽度 可以根据市面上的常用分辨率来设置 
  5.  * @param size 期望图片的大小,单位为kb 
  6.  * @param quality 图片压缩的质量,取值1-100,越小表示压缩的越厉害,如输入30,表示压缩70% 
  7.  * @return Bitmap 压缩后得到的图片 
  8.  */  
  9. public static Bitmap compressBitmap(String imgUrl,int reqWidth,int size,int quality){  
  10.     // 创建bitMap  
  11.     BitmapFactory.Options options = new BitmapFactory.Options();  
  12.     options.inJustDecodeBounds = true;  
  13.     BitmapFactory.decodeFile(imgUrl, options);  
  14.     int height = options.outHeight;  
  15.     int width = options.outWidth;  
  16.     int reqHeight;  
  17.     reqHeight = (reqWidth * height) / width;  
  18.     // 在内存中创建bitmap对象,这个对象按照缩放比例创建的  
  19.     options.inSampleSize = calculateInSampleSize(  
  20.             options, reqWidth, reqHeight);  
  21.     options.inJustDecodeBounds = false;  
  22.     Bitmap bm = BitmapFactory.decodeFile(  
  23.             imgUrl, options);  
  24.     Bitmap mBitmap = compressImage(Bitmap.createScaledBitmap(  
  25.             bm, 480, reqHeight, false),size,quality);  
  26.     return  mBitmap;  
  27. }  
[java]  view plain  copy
  1. </pre><pre name="code" class="java">  
[java]  view plain  copy
  1. /** 
  2.      * 计算像素压缩的缩放比例 
  3.      * @param options 
  4.      * @param reqWidth 
  5.      * @param reqHeight 
  6.      * @return 
  7.      */  
  8.     private static int calculateInSampleSize(BitmapFactory.Options options,  
  9.                                              int reqWidth, int reqHeight) {  
  10.         final int height = options.outHeight;  
  11.         final int width = options.outWidth;  
  12.         int inSampleSize = 1;  
  13.   
  14.   
  15.         if (height > reqHeight || width > reqWidth) {  
  16.             if (width > height) {  
  17.                 inSampleSize = Math.round((float) height / (float) reqHeight);  
  18.             } else {  
  19.                 inSampleSize = Math.round((float) width / (float) reqWidth);  
  20.             }  
  21.         }  
  22.         return inSampleSize;  
  23.     }  

 
 
[java]  view plain  copy
  1. </pre><pre name="code" class="java">  
[java]  view plain  copy
  1. </pre><pre name="code" class="java">  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Android中,你可以使用Bitmap类的createScaledBitmap方法来实现同时等比例缩放和压缩Bitmap的功能。具体步骤如下: 1. 通过BitmapFactory.decodeFile或BitmapFactory.decodeStream方法获取原始Bitmap对象。 2. 计算出缩放比例,公式为:scaleWidth = 目标宽度 / 原始宽度(或者scaleHeight = 目标高度 / 原始高度,取两者中比例较小的值)。 3. 调用Bitmap类的createScaledBitmap方法,传入原始Bitmap对象、缩放后的宽度和高度以及是否保持原始比例的参数,获取压缩后的Bitmap对象。 下面是示例代码: ```java public static Bitmap compressBitmap(Bitmap bitmap, int targetWidth, int targetHeight) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); float scaleWidth = ((float) targetWidth) / width; float scaleHeight = ((float) targetHeight) / height; float scale = Math.min(scaleWidth, scaleHeight); Matrix matrix = new Matrix(); matrix.postScale(scale, scale); Bitmap compressedBitmap = Bitmap.createScaledBitmap(bitmap, (int) (width * scale), (int) (height * scale), true); return compressedBitmap; } ``` 在上述示例代码中,compressBitmap方法接受三个参数:原始Bitmap对象、目标宽度和目标高度。方法内部首先计算出缩放比例scale,然后通过Matrix类的postScale方法来设置缩放比例,最后再通过Bitmap类的createScaledBitmap方法来获取压缩后的Bitmap对象。需要注意的是,createScaledBitmap方法的第四个参数为是否保持原始比例,如果设置为true,则目标宽度和高度参数将被解释为最大值,否则将被解释为精确值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值