Android图片压缩

一、质量压缩(图片文件大小压缩)

压缩后加载成bitmap占用内存不会变,SD卡中存储大小变小

Bimap 中compress(Bitmap.CompressFormat format,int option,Outputstream outStream)方法三个参数

1、format------压缩后图片的格式Bitmap.CompressFormat .JPEG、.PNG、.WEBP。

2、option(0-100)值越小图片失真越大,压缩后所占内存越少

3、outStream压缩后输入位置

  1. public static void compressBmpToFile(Bitmap bmp,File file){  
  2.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  3.         int options = 100;//
  4.         bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);  
  5.         while (baos.toByteArray().length / 1024 > 100) {   
  6.             baos.reset();  
  7.             options -= 5;  
  8.             bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);  
  9.         }  
  10.         try {  
  11.             FileOutputStream fos = new FileOutputStream(file);  
  12.             fos.write(baos.toByteArray());  
  13.             fos.flush();  
  14.             fos.close();  
  15.         } catch (Exception e) {  
  16.             e.printStackTrace();  
  17.         }  
  18.     }  



二、尺寸压缩(bitmap像素的压缩)

原理就是先加载图片的宽高而不加载图片的内容,根据图片要显示控件的宽高计算压缩后图片的宽高

BitmapFactory.decodeFile(filePath, options)从新加载生成图片,参数filePath算图片的路径,options图片像素大小等相关属性。

这里尺寸压缩也就是常说的二次采样,

  1.  /** 
  2.      * @param filePath   要加载的图片路径 
  3.      * @param destWidth  显示图片的控件宽度 
  4.      * @param destHeight 显示图片的控件的高度 
  5.      * @return 
  6.      */  
  7.     public static Bitmap decodeBitmap(String filePath, int destWidth, int destHeight) {  
  8.         //第一次采样  
  9.         BitmapFactory.Options options = new BitmapFactory.Options();  
  10.         //该属性设置为true只会加载图片的边框进来,并不会加载图片具体的像素点  
  11.         options.inJustDecodeBounds = true;  
  12.         //第一次加载图片,这时只会加载图片的边框进来,并不会加载图片中的像素点  
  13.         BitmapFactory.decodeFile(filePath, options);  
  14.         //获得原图的宽和高  
  15.         int outWidth = options.outWidth;  
  16.         int outHeight = options.outHeight;  
  17.         //定义缩放比例  
  18.         int sampleSize = 1;  
  19.         while (outHeight / sampleSize > destHeight || outWidth / sampleSize > destWidth) {  
  20.             //如果宽高的任意一方的缩放比例没有达到要求,都继续增大缩放比例  
  21.             //sampleSize应该为2的n次幂,如果给sampleSize设置的数字不是2的n次幂,那么系统会就近取值  
  22.             sampleSize *= 2;  
  23.         }  
  24.         /********************************************************************************************/  
  25.         //至此,第一次采样已经结束,我们已经成功的计算出了sampleSize的大小  
  26.         /********************************************************************************************/  
  27.         //二次采样开始  
  28.         //二次采样时我需要将图片加载出来显示,不能只加载图片的框架,因此inJustDecodeBounds属性要设置为false  
  29.         options.inJustDecodeBounds = false;  
  30.         //设置缩放比例  
  31.         options.inSampleSize = sampleSize;  
  32.         options.inPreferredConfig = Bitmap.Config.ARGB_8888;  
  33.         //加载图片并返回  
  34.         return BitmapFactory.decodeFile(filePath, options);  
  35.     }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值