栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android图片压缩的实例详解

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android图片压缩的实例详解

Android图片压缩的实例详解

在做微信分享的时候,由于分享的缩略图要求不得大于32K,否则不能调起微信,所以总结了一下Android图片的压缩问题,大部分资料都是来自网上各位的分享,自己只是完善或修改了一下,本着继续分享的精神,也方便自己记忆,于是总结如下。

android图片压缩主要有两种方式:1.压缩图片分辨率 2.压缩图片质量

一、先看压缩图片分辨率,很好理解,如本来1280*768的图片压缩为640*384大小。废话不说,直接上代码:



 
  public static Bitmap createScaledBitmapByOutHeight(Bitmap inBitmap, int outHeight, boolean needRecycled) {      
    int bitmapHeight = inBitmap.getHeight(); 
    int bitmapWidth = inBitmap.getWidth(); 
    int outWidth = bitmapWidth * outHeight / bitmapHeight; 
     
    return createScaledBitmap(inBitmap, outWidth, outHeight, needRecycled); 
  }  
   
   
  public static Bitmap createScaledBitmapByOutWidth(Bitmap inBitmap, int outWidth, boolean needRecycled) {     
    int bitmapHeight = inBitmap.getHeight(); 
    int bitmapWidth = inBitmap.getWidth(); 
    int outHeight = bitmapHeight * outWidth / bitmapWidth; 
     
    return createScaledBitmap(inBitmap, outWidth, outHeight, needRecycled); 
  } 
   
   
  public static Bitmap createScaledBitmap(Bitmap inBitmap, int outWidth, int outHeight, boolean needRecycled) {  
     
    Bitmap thumbBmp = Bitmap.createScaledBitmap(inBitmap, outWidth, outHeight, true); 
    if (needRecycled) { 
      inBitmap.recycle(); 
    } 
     
    return thumbBmp; 
  } 

前两个方法可以指定期望的宽度或高度,并按比例缩放图片的分辨率,第3个方法可以随意指定期望的宽高,缩放图片。

上面代码是对输入的bitmap进行缩放,还可以从资源或文件中加载图片并缩放,具体如下:

 
  public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,  
      int outWidth, int outHeight) {  
    final BitmapFactory.Options options = new BitmapFactory.Options();  
    options.inJustDecodeBounds = true; // 假解,来获取图片大小  
    BitmapFactory.decodeResource(res, resId, options);  
    options.inSampleSize = calculateInSampleSize(options, outWidth, outHeight);  
    // 使用获取到的inSampleSize值再次解析图片  
    options.inJustDecodeBounds = false; 
    //options.inPreferredConfig = Config.RGB_565; 
    return BitmapFactory.decodeResource(res, resId, options);  
  }  
   
   
  public static Bitmap decodeSampledBitmapFromFile(String pathName, int outWidth, int outHeight) { 
    final BitmapFactory.Options options = new BitmapFactory.Options();  
    options.inJustDecodeBounds = true; // 假解,来获取图片大小  
    BitmapFactory.decodeFile(pathName, options); 
    options.inSampleSize = calculateInSampleSize(options, outWidth, outHeight);  
    // 使用获取到的inSampleSize值再次解析图片  
    options.inJustDecodeBounds = false; 
    //options.inPreferredConfig = Config.RGB_565; 
    return BitmapFactory.decodeFile(pathName, options); 
  }  
   
   
  public static int calculateInSampleSize(BitmapFactory.Options options,  
      int reqWidth, int reqHeight) {  
    // 源图片的高度和宽度  
    final int height = options.outHeight;  
    final int width = options.outWidth;  
    int inSampleSize = 1;  
    if (height > reqHeight || width > reqWidth) {  
      // 计算出实际宽高和目标宽高的比率  
      final int heightRatio = Math.round((float) height / (float) reqHeight);  
      final int widthRatio = Math.round((float) width / (float) reqWidth);  
      // 选择宽和高中最小的比率作为inSampleSize的值,这样可以保证最终图片的宽和高  
      // 一定都会大于等于目标的宽和高。  
      inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;  
    }  
    return inSampleSize;  
  }  

二、压缩图片的质量

 
  public static Bitmap compressImage(Bitmap inBitmap, int outSize, boolean needRecycled) {  
      
    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
    inBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
    int quality = 100;  
    while (baos.toByteArray().length / 1024 > outSize) { 
      if (quality <= 0) { 
 ByteArrayInputStream outBais = new ByteArrayInputStream(baos.toByteArray()); 
 return BitmapFactory.decodeStream(outBais, null, null);// 如果quaLity为0时还未达到32k以内,返回得到的最小值;如需要可结合分辨率压缩 
      } 
      baos.reset(); 
      //PNG格式下,这种压缩不起作用(quality:0-100,如果目标大小太小,有时候质量压缩不一定能达到效果,需结合分辨率压缩) 
      inBitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos); 
      Log.e("AN", "bitmap size:"+ baos.toByteArray().length / 1024 + "k"); 
      quality -= 10; 
    } 
    if (needRecycled) { 
      inBitmap.recycle(); 
    } 
     
     
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); 
    Bitmap outBitmap= BitmapFactory.decodeStream(bais, null, null);//ByteArrayInputStream转成bitmap 
     
    return outBitmap;  
  } 


需要注意的是compress方法,该压缩方法只对JPEG格式有效,对于PNG格式,则会忽略第二个参数quality,即压缩不起作用。这种压缩只是对图片质量有影响,并不会改变图片大小。

当然,如有需要,以上两种压缩方法可以结合使用。

以上就是Android 图片压缩的实现方法的详解,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/158096.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号