欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android图片压缩几种方式总结

程序员文章站 2023-11-29 19:58:46
android图片压缩几种方式总结 图片压缩在android开发中很常见也很重要,防止图片的oom也是压缩的重要原因。 首先看下bitmap图片文件的大小的决定因素:...

android图片压缩几种方式总结

图片压缩在android开发中很常见也很重要,防止图片的oom也是压缩的重要原因。

首先看下bitmap图片文件的大小的决定因素:

bitmap所占用的内存 = 图片长度 x 图片宽度 x 一个像素点占用的字节数。3个参数,任意减少一个的值,就达到了压缩的效果。

接下来看下bitmap图片的几种格式的特点:

alpha_8
 表示8位alpha位图,即a=8,一个像素点占用1个字节,它没有颜色,只有透明度
 argb_4444
表示16位argb位图,即a=4,r=4,g=4,b=4,一个像素点占4+4+4+4=16位,2个字节
argb_8888
表示32位argb位图,即a=8,r=8,g=8,b=8,一个像素点占8+8+8+8=32位,4个字节
rgb_565
表示16位rgb位图,即r=5,g=6,b=5,它没有透明度,一个像素点占5+6+5=16位,2个字节

如果进行图片格式的压缩的话,一般情况下都是argb_8888转为rgb565进行压缩。

写了一个工具类,基本上列举了android上图片的几种基本压缩方式:

1.质量压缩

2.采样率压缩

3.尺寸压缩

4.matrix压缩

5.图片格式的压缩,例如png和jpg保存后的图片大小是不同的

public class utils { 
 
  /** 
   * 采样率压缩 
   * 
   * @param bitmap 
   * @param samplesize 采样率为2的整数倍,非整数倍四舍五入,如4的话,就是原图的1/4 
   * @return 尺寸变化 
   */ 
  public static bitmap getbitmap(bitmap bitmap, int samplesize) { 
    bitmapfactory.options options = new bitmapfactory.options(); 
    options.insamplesize = samplesize; 
    bytearrayoutputstream baos = new bytearrayoutputstream(); 
    bitmap.compress(bitmap.compressformat.jpeg, 100, baos); 
    byte[] bytes = baos.tobytearray(); 
    bitmap bit = bitmapfactory.decodebytearray(bytes, 0, bytes.length, options); 
    log.i("info", "图片大小:" + bit.getbytecount());//2665296  10661184 
    return bit; 
  } 
 
  /** 
   * 图片质量压缩 
   * 
   * @param bitmap 
   * @param quality 
   * @return 尺寸不变,质量变小 
   */ 
  public static bitmap compressbyquality(bitmap bitmap, int quality) { 
    bytearrayoutputstream baos = new bytearrayoutputstream(); 
    bitmap.compress(bitmap.compressformat.jpeg, quality, baos); 
    byte[] bytes = baos.tobytearray(); 
    bitmap bit = bitmapfactory.decodebytearray(bytes, 0, bytes.length); 
    log.i("info", "图片大小:" + bit.getbytecount());//10661184 
    return bit; 
  } 
 
  /** 
   * 图片质量压缩 
   * 
   * @param src 
   * @param maxbytesize 
   * @return 
   */ 
  public static bitmap compressbyquality(bitmap src, long maxbytesize) { 
    bytearrayoutputstream baos = new bytearrayoutputstream(); 
    int quality = 100; 
    src.compress(bitmap.compressformat.jpeg, quality, baos); 
    while (baos.tobytearray().length > maxbytesize && quality > 0) { 
      baos.reset(); 
      src.compress(bitmap.compressformat.jpeg, quality -= 5, baos); 
    } 
    if (quality < 0) return null; 
    byte[] bytes = baos.tobytearray(); 
    bitmap bit = bitmapfactory.decodebytearray(bytes, 0, bytes.length); 
    return bit; 
  } 
 
  public static bitmap compressbyformat(bitmap bitmap, int format) { 
    bytearrayoutputstream baos = new bytearrayoutputstream(); 
    bitmap.compress(bitmap.compressformat.jpeg, 100, baos); 
    byte[] bytes = baos.tobytearray(); 
    bitmap bit = bitmapfactory.decodebytearray(bytes, 0, bytes.length); 
    log.i("info", "图片大小:" + bit.getbytecount());//10661184 
    return bit; 
  } 
 
  /** 
   * matrix缩放 
   * 
   * @param bitmap 
   * @param scalewidth 
   * @param scaleheight 
   * @return 尺寸和大小变化 
   */ 
  public static bitmap getbitmapbysize(bitmap bitmap, float scalewidth, float scaleheight) { 
    matrix matrix = new matrix(); 
    matrix.postscale(scalewidth, scaleheight); 
    bitmap bit = bitmap.createbitmap(bitmap, 0, 0, bitmap.getwidth(), bitmap.getheight(), matrix, false); 
    log.i("info", "图片大小:" + bit.getbytecount()); 
    return bit; 
  } 
 
  /** 
   * 按照图片格式配置压缩 
   * 
   * @param path 
   * @param config alpha_8,argb_4444,argb_8888,rgb_565; 
   * @return rgb_565比argb_8888节省一半内存 
   */ 
  public static bitmap getbitmapbyformatconfig(string path, bitmap.config config) { 
    bitmapfactory.options options = new bitmapfactory.options(); 
    options.inpreferredconfig = config; 
    bitmap bitmap = bitmapfactory.decodefile(path, options); 
    log.i("info", "图片大小:" + bitmap.getbytecount()); 
    return bitmap; 
  } 
 
  /** 
   * 指定大小缩放 
   * 
   * @param bitmap 
   * @param width 
   * @param height 
   * @return 
   */ 
  public static bitmap getbitmapbyscalesize(bitmap bitmap, int width, int height) { 
    bitmap bit = bitmap.createscaledbitmap(bitmap, width, height, true); 
    log.i("info", "图片大小:" + bit.getbytecount()); 
    return bit; 
  } 
 
  /** 
   * 通过保存格式压缩 
   * 
   * @param bitmap 
   * @param format jpeg,png,webp 
   * @return 
   */ 
  public static bitmap getbitmapbyformat(bitmap bitmap, bitmap.compressformat format) { 
    bytearrayoutputstream baos = new bytearrayoutputstream(); 
    bitmap.compress(format, 100, baos); 
    byte[] bytes = baos.tobytearray(); 
    bitmap bit = bitmapfactory.decodebytearray(bytes, 0, bytes.length); 
    log.i("info", "图片大小:" + bit.getbytecount()); 
    return bit; 
  } 
 
  /** 
   * 文件加载压缩 
   * 
   * @param filepath 
   * @param insamplesize 
   * @return 
   */ 
  public static bitmap getbitmap(string filepath, int insamplesize) { 
    bitmapfactory.options options = new bitmapfactory.options(); 
    options.injustdecodebounds = true; 
    bitmapfactory.decodefile(filepath, options);//此时不耗费和占用内存 
    options.insamplesize = insamplesize; 
    options.injustdecodebounds = false; 
    return bitmapfactory.decodefile(filepath, options); 
  } 
 
  public static bitmap getbitmap(string filepath) { 
    return bitmapfactory.decodefile(filepath); 
  } 
 
  public static bitmap view2bitmap(view view) { 
    if (view == null) return null; 
    bitmap ret = bitmap.createbitmap(view.getwidth(), view.getheight(), bitmap.config.argb_8888); 
    canvas canvas = new canvas(ret); 
    drawable bgdrawable = view.getbackground(); 
    if (bgdrawable != null) { 
      bgdrawable.draw(canvas); 
    } else { 
      canvas.drawcolor(color.white); 
    } 
    view.draw(canvas); 
    return ret; 
  } 
 
  public static void savebitmap(bitmap bitmap) { 
    file file = new file(environment.getexternalstoragedirectory() + "/img.jpg"); 
    try { 
      fileoutputstream fileoutputstream = new fileoutputstream(file); 
      bitmap.compress(bitmap.compressformat.jpeg, 100, fileoutputstream); 
      fileoutputstream.flush(); 
      fileoutputstream.close(); 
    } catch (filenotfoundexception e) { 
      e.printstacktrace(); 
    } catch (ioexception e) { 
      e.printstacktrace(); 
    } 
  } 
 
  public static void savebitmap(bitmap bitmap,bitmap.compressformat format) { 
    file file = new file(environment.getexternalstoragedirectory() + "/img.jpg"); 
    try { 
      fileoutputstream fileoutputstream = new fileoutputstream(file); 
      bitmap.compress(format, 100, fileoutputstream); 
      fileoutputstream.flush(); 
      fileoutputstream.close(); 
    } catch (filenotfoundexception e) { 
      e.printstacktrace(); 
    } catch (ioexception e) { 
      e.printstacktrace(); 
    } 
  } 
} 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!