当前位置: 移动技术网 > IT编程>移动开发>Android > Android图片压缩的实例详解

Android图片压缩的实例详解

2019年07月24日  | 移动技术网IT编程  | 我要评论

恶魔幸存者2金手指,军海脑科,无限挑战e304

android图片压缩的实例详解

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

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

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



/** 
   * 按比例压缩图片分辨率 
   * @param inbitmap 
   * @param outheight 输出图片高度,可据此保持比例计算输出宽度 
   * @param needrecycled 是否回收inbitmap 
   * @return 
   */ 
  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); 
  }  
   
  /** 
   * 按比例压缩图片分辨率 
   * @param inbitmap 
   * @param outheight 输出图片宽度,可据此保持比例计算输出高度 
   * @param needrecycled 是否回收inbitmap 
   * @return 
   */ 
  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); 
  } 
   
  /** 
   * 指定输出的宽高缩放图片 
   * @param inbitmap 
   * @param outwidth 
   * @param outheight 
   * @param needrecycled 
   * @return 
   */ 
  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进行缩放,还可以从资源或文件中加载图片并缩放,具体如下:

/** 
   * 从资源加载并压缩图片 
   * @param res 
   * @param resid 
   * @param outwidth 目标宽度 
   * @param outheight 目标高度 
   * @return 
   */ 
  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);  
  }  
   
  /** 
   * 从文件中加载并压缩图片 
   * @param res 
   * @param resid 
   * @param outwidth 目标宽度 
   * @param outheight 目标高度 
   * @return 
   */ 
  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); 
  }  
   
  /** 
   * 计算options.insamplesize 
   * @param options 
   * @param reqwidth 
   * @param reqheight 
   * @return 
   */ 
  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;  
  }  

二、压缩图片的质量

/** 
   * 压缩图片质量,把图片压缩到outsize以内 
   * @param inbitmap 原始bitmap 
   * @param outsize 压缩到的大小 
   * @param needrecycled 是否回收bitmap 
   * @return 
   */ 
  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 图片压缩的实现方法的详解,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网