当前位置: 移动技术网 > IT编程>移动开发>Android > Android整理好的图片压缩工具类

Android整理好的图片压缩工具类

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

常州长征门诊,逝水伊人,dnf补助

android设备的内存有限,对于大图片,必须进行压缩后再进行显示,否则会出现内存溢出:oom;

处理策略:

1.使用缩略图(thumbnails);

android系统会给检测到的图片创建缩略图;可以操作media内容提供者中的image对图片进行操作;

2.手动压缩:

  • (1)根据图片和屏幕尺寸,等比压缩,完美显示;
  • (2)降低图片质量,压缩图片大小;

以下是自己整理的小工具类(对于按比例缩放后,在此并未再进行质量缩放,此时图片大小有可能超出我们期望的限制;假如我们有严格的大小限制需求,可先进行按比例缩放后,判断此时图片大小是否超出限制;如果超出限制,对其再进行质量缩放即可。建议使用按比例缩放,按质量缩放很有可能导致图片失真。)

</pre><p><pre name="code" class="java">package com.util; 
import java.io.bytearrayoutputstream; 
import java.io.ioexception; 
import java.io.inputstream; 
import android.graphics.bitmap; 
import android.graphics.matrix; 
import android.graphics.bitmap.compressformat; 
import android.graphics.bitmapfactory; 
import android.media.exifinterface; 
/** 
 * 图片压缩工具类 
 * @author 丶life_ 
 */ 
public class imagecompressutil { 
  /** 
   * 通过降低图片的质量来压缩图片 
   * @param bmp 
   *      要压缩的图片位图对象 
   * @param maxsize 
   *      压缩后图片大小的最大值,单位kb 
   * @return 压缩后的图片位图对象 
   */ 
  public static bitmap compressbyquality(bitmap bitmap, int maxsize) { 
    bytearrayoutputstream baos = new bytearrayoutputstream(); 
    int quality = 100; 
    bitmap.compress(compressformat.jpeg, quality, baos); 
    system.out.println("图片压缩前大小:" + baos.tobytearray().length + "byte"); 
    boolean iscompressed = false; 
    while (baos.tobytearray().length / 1024 > maxsize) { 
      quality -= 10; 
      baos.reset(); 
      bitmap.compress(compressformat.jpeg, quality, baos); 
      system.out.println("质量压缩到原来的" + quality + "%时大小为:" 
          + baos.tobytearray().length + "byte"); 
      iscompressed = true; 
    } 
    system.out.println("图片压缩后大小:" + baos.tobytearray().length + "byte"); 
    if (iscompressed) { 
      bitmap compressedbitmap = bitmapfactory.decodebytearray( 
          baos.tobytearray(), 0, baos.tobytearray().length); 
      recyclebitmap(bitmap); 
      return compressedbitmap; 
    } else { 
      return bitmap; 
    } 
  } 
  /** 
   * 传入图片url,通过压缩图片的尺寸来压缩图片大小
   * @param pathname 图片的完整路径 
   * @param targetwidth 缩放的目标宽度 
   * @param targetheight 缩放的目标高度 
   * @return 缩放后的图片 
   */ 
  public static bitmap compressbysize(string pathname, int targetwidth, 
      int targetheight) { 
    bitmapfactory.options opts = new bitmapfactory.options(); 
    opts.injustdecodebounds = true;// 不去真的解析图片,只是获取图片的头部信息,包含宽高等; 
    bitmap bitmap = bitmapfactory.decodefile(pathname, opts); 
    // 得到图片的宽度、高度; 
    int imgwidth = opts.outwidth; 
    int imgheight = opts.outheight; 
    // 分别计算图片宽度、高度与目标宽度、高度的比例;取大于等于该比例的最小整数; 
    int widthratio = (int) math.ceil(imgwidth / (float) targetwidth); 
    int heightratio = (int) math.ceil(imgheight / (float) targetheight); 
    if (widthratio > 1 || heightratio > 1) { 
      if (widthratio > heightratio) { 
        opts.insamplesize = widthratio; 
      } else { 
        opts.insamplesize = heightratio; 
      } 
    } 
    // 设置好缩放比例后,加载图片进内容; 
    opts.injustdecodebounds = false; 
    bitmap = bitmapfactory.decodefile(pathname, opts); 
    return bitmap; 
  } 
  /** 
   * 传入bitmap,通过压缩图片的尺寸来压缩图片大小  
   * @param bitmap 要压缩图片 
   * @param targetwidth 缩放的目标宽度 
   * @param targetheight 缩放的目标高度 
   * @return 缩放后的图片 
   */ 
  public static bitmap compressbysize(bitmap bitmap, int targetwidth, 
      int targetheight) { 
    bytearrayoutputstream baos = new bytearrayoutputstream(); 
    bitmap.compress(compressformat.jpeg, 100, baos); 
    bitmapfactory.options opts = new bitmapfactory.options(); 
    opts.injustdecodebounds = true; 
    bitmap = bitmapfactory.decodebytearray(baos.tobytearray(), 0, 
        baos.tobytearray().length, opts); 
    // 得到图片的宽度、高度; 
    int imgwidth = opts.outwidth; 
    int imgheight = opts.outheight; 
    // 分别计算图片宽度、高度与目标宽度、高度的比例;取大于该比例的最小整数; 
    int widthratio = (int) math.ceil(imgwidth / (float) targetwidth); 
    int heightratio = (int) math.ceil(imgheight / (float) targetheight); 
    if (widthratio > 1 || heightratio > 1) { 
      if (widthratio > heightratio) { 
        opts.insamplesize = widthratio; 
      } else { 
        opts.insamplesize = heightratio; 
      } 
    } 
    // 设置好缩放比例后,加载图片进内存; 
    opts.injustdecodebounds = false; 
    bitmap compressedbitmap = bitmapfactory.decodebytearray( 
        baos.tobytearray(), 0, baos.tobytearray().length, opts); 
    recyclebitmap(bitmap); 
    return compressedbitmap; 
  } 
  /** 
   * 通过压缩图片的尺寸来压缩图片大小,通过读入流的方式,可以有效防止网络图片数据流形成位图对象时内存过大的问题; 
   * @param inputstream 要压缩图片,以流的形式传入 
   * @param targetwidth 缩放的目标宽度 
   * @param targetheight 缩放的目标高度 
   * @return 缩放后的图片 
   * @throws ioexception 读输入流的时候发生异常 
   */ 
  public static bitmap compressbysize(inputstream is, int targetwidth, 
      int targetheight) throws ioexception { 
    bytearrayoutputstream baos = new bytearrayoutputstream(); 
    byte[] buff = new byte[1024]; 
    int len = 0; 
    while ((len = is.read(buff)) != -1) { 
      baos.write(buff, 0, len); 
    } 
    byte[] data = baos.tobytearray(); 
    bitmapfactory.options opts = new bitmapfactory.options(); 
    opts.injustdecodebounds = true; 
    bitmap bitmap = bitmapfactory.decodebytearray(data, 0, data.length, 
        opts); 
    // 得到图片的宽度、高度; 
    int imgwidth = opts.outwidth; 
    int imgheight = opts.outheight; 
    // 分别计算图片宽度、高度与目标宽度、高度的比例;取大于该比例的最小整数; 
    int widthratio = (int) math.ceil(imgwidth / (float) targetwidth); 
    int heightratio = (int) math.ceil(imgheight / (float) targetheight); 
    if (widthratio > 1 || heightratio > 1) { 
      if (widthratio > heightratio) { 
        opts.insamplesize = widthratio; 
      } else { 
        opts.insamplesize = heightratio; 
      } 
    } 
    // 设置好缩放比例后,加载图片进内存; 
    opts.injustdecodebounds = false; 
    bitmap = bitmapfactory.decodebytearray(data, 0, data.length, opts); 
    return bitmap; 
  } 
  /** 
   * 旋转图片摆正显示 
   * @param srcpath 
   * @param bitmap 
   * @return 
   */ 
  public static bitmap rotatebitmapbyexif(string srcpath, bitmap bitmap) { 
    exifinterface exif; 
    bitmap newbitmap = null; 
    try { 
      exif = new exifinterface(srcpath); 
      if (exif != null) { // 读取图片中相机方向信息 
        int ori = exif.getattributeint(exifinterface.tag_orientation, 
            exifinterface.orientation_normal); 
        int digree = 0; 
        switch (ori) { 
        case exifinterface.orientation_rotate_90: 
          digree = 90; 
          break; 
        case exifinterface.orientation_rotate_180: 
          digree = 180; 
          break; 
        case exifinterface.orientation_rotate_270: 
          digree = 270; 
          break; 
        } 
        if (digree != 0) { 
          matrix m = new matrix(); 
          m.postrotate(digree); 
          newbitmap = bitmap.createbitmap(bitmap, 0, 0, 
              bitmap.getwidth(), bitmap.getheight(), m, true); 
          recyclebitmap(bitmap); 
          return newbitmap; 
        } 
      } 
    } catch (ioexception e) { 
      e.printstacktrace(); 
    } 
    return bitmap; 
  } 
  /** 
   * 回收位图对象 
   * @param bitmap 
   */ 
  public static void recyclebitmap(bitmap bitmap) { 
    if (bitmap != null && !bitmap.isrecycled()) { 
      bitmap.recycle(); 
      system.gc(); 
      bitmap = null; 
    } 
  } 
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对移动技术网的支持。如果你想了解更多相关内容请查看下面相关链接

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

相关文章:

验证码:
移动技术网