Android对图片进行压缩

 

Android对图片进行压缩

分类: Android相关

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

处理大图片:

1.使用缩略图(Thumbnails);

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

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

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

[java]  view plain copy
  1. import java.io.ByteArrayOutputStream;  
  2. import java.io.IOException;  
  3. import java.io.InputStream;  
  4.   
  5. import android.graphics.Bitmap;  
  6. import android.graphics.Bitmap.CompressFormat;  
  7. import android.graphics.BitmapFactory;  
  8.   
  9. /** 
  10.  * 图片压缩工具类 
  11.  * @author 丶Life_ 
  12.  * 
  13.  */  
  14. public class ImageCompressUtil {  
  15.   
  16.     /** 
  17.      * 通过降低图片的质量来压缩图片 
  18.      *  
  19.      * @param bmp 
  20.      *            要压缩的图片 
  21.      * @param maxSize 
  22.      *            压缩后图片大小的最大值,单位KB 
  23.      * @return 压缩后的图片 
  24.      */  
  25.     public static Bitmap compressByQuality(Bitmap bitmap, int maxSize) {  
  26.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  27.         int quality = 100;  
  28.         bitmap.compress(CompressFormat.JPEG, quality, baos);  
  29.         System.out.println("图片压缩前大小:" + baos.toByteArray().length + "byte");  
  30.         while (baos.toByteArray().length / 1024 > maxSize) {  
  31.             quality -= 10;  
  32.             baos.reset();  
  33.             bitmap.compress(CompressFormat.JPEG, quality, baos);  
  34.             System.out.println("质量压缩到原来的" + quality + "%时大小为:"  
  35.                     + baos.toByteArray().length + "byte");  
  36.         }  
  37.         System.out.println("图片压缩后大小:" + baos.toByteArray().length + "byte");  
  38.         bitmap = BitmapFactory.decodeByteArray(baos.toByteArray(), 0,  
  39.                 baos.toByteArray().length);  
  40.         return bitmap;  
  41.     }  
  42.   
  43.     /** 
  44.      * 通过压缩图片的尺寸来压缩图片大小 
  45.      *  
  46.      * @param pathName 
  47.      *            图片的完整路径 
  48.      * @param targetWidth 
  49.      *            缩放的目标宽度 
  50.      * @param targetHeight 
  51.      *            缩放的目标高度 
  52.      * @return 缩放后的图片 
  53.      */  
  54.     public static Bitmap compressBySize(String pathName, int targetWidth,  
  55.             int targetHeight) {  
  56.         BitmapFactory.Options opts = new BitmapFactory.Options();  
  57.         opts.inJustDecodeBounds = true;// 不去真的解析图片,只是获取图片的头部信息,包含宽高等;  
  58.         Bitmap bitmap = BitmapFactory.decodeFile(pathName, opts);  
  59.         // 得到图片的宽度、高度;  
  60.         int imgWidth = opts.outWidth;  
  61.         int imgHeight = opts.outHeight;  
  62.         // 分别计算图片宽度、高度与目标宽度、高度的比例;取大于等于该比例的最小整数;  
  63.         int widthRatio = (int) Math.ceil(imgWidth / (float) targetWidth);  
  64.         int heightRatio = (int) Math.ceil(imgHeight / (float) targetHeight);  
  65.         if (widthRatio > 1 || widthRatio > 1) {  
  66.             if (widthRatio > heightRatio) {  
  67.                 opts.inSampleSize = widthRatio;  
  68.             } else {  
  69.                 opts.inSampleSize = heightRatio;  
  70.             }  
  71.         }  
  72.         // 设置好缩放比例后,加载图片进内容;  
  73.         opts.inJustDecodeBounds = false;  
  74.         bitmap = BitmapFactory.decodeFile(pathName, opts);  
  75.         return bitmap;  
  76.     }  
  77.   
  78.     /** 
  79.      * 通过压缩图片的尺寸来压缩图片大小 
  80.      *  
  81.      * @param bitmap 
  82.      *            要压缩图片 
  83.      * @param targetWidth 
  84.      *            缩放的目标宽度 
  85.      * @param targetHeight 
  86.      *            缩放的目标高度 
  87.      * @return 缩放后的图片 
  88.      */  
  89.     public static Bitmap compressBySize(Bitmap bitmap, int targetWidth,  
  90.             int targetHeight) {  
  91.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  92.         bitmap.compress(CompressFormat.JPEG, 100, baos);  
  93.         BitmapFactory.Options opts = new BitmapFactory.Options();  
  94.         opts.inJustDecodeBounds = true;  
  95.         bitmap = BitmapFactory.decodeByteArray(baos.toByteArray(), 0,  
  96.                 baos.toByteArray().length, opts);  
  97.         // 得到图片的宽度、高度;  
  98.         int imgWidth = opts.outWidth;  
  99.         int imgHeight = opts.outHeight;  
  100.         // 分别计算图片宽度、高度与目标宽度、高度的比例;取大于该比例的最小整数;  
  101.         int widthRatio = (int) Math.ceil(imgWidth / (float) targetWidth);  
  102.         int heightRatio = (int) Math.ceil(imgHeight / (float) targetHeight);  
  103.         if (widthRatio > 1 && widthRatio > 1) {  
  104.             if (widthRatio > heightRatio) {  
  105.                 opts.inSampleSize = widthRatio;  
  106.             } else {  
  107.                 opts.inSampleSize = heightRatio;  
  108.             }  
  109.         }  
  110.         // 设置好缩放比例后,加载图片进内存;  
  111.         opts.inJustDecodeBounds = false;  
  112.         bitmap = BitmapFactory.decodeByteArray(baos.toByteArray(), 0,  
  113.                 baos.toByteArray().length, opts);  
  114.         return bitmap;  
  115.     }  
  116.       
  117.     /** 
  118.      * 通过压缩图片的尺寸来压缩图片大小,通过读入流的方式,可以有效防止网络图片数据流形成位图对象时内存过大的问题; 
  119.      *  
  120.      * @param InputStream 
  121.      *            要压缩图片,以流的形式传入 
  122.      * @param targetWidth 
  123.      *            缩放的目标宽度 
  124.      * @param targetHeight 
  125.      *            缩放的目标高度 
  126.      * @return 缩放后的图片 
  127.      * @throws IOException 
  128.      *             读输入流的时候发生异常 
  129.      */  
  130.     public static Bitmap compressBySize(InputStream is, int targetWidth,  
  131.             int targetHeight) throws IOException {  
  132.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  133.         byte[] buff = new byte[1024];  
  134.         int len = 0;  
  135.         while ((len = is.read(buff)) != -1) {  
  136.             baos.write(buff, 0, len);  
  137.         }  
  138.   
  139.         byte[] data = baos.toByteArray();  
  140.         BitmapFactory.Options opts = new BitmapFactory.Options();  
  141.         opts.inJustDecodeBounds = true;  
  142.         Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opts);  
  143.         // 得到图片的宽度、高度;  
  144.         int imgWidth = opts.outWidth;  
  145.         int imgHeight = opts.outHeight;  
  146.         // 分别计算图片宽度、高度与目标宽度、高度的比例;取大于该比例的最小整数;  
  147.         int widthRatio = (int) Math.ceil(imgWidth / (float) targetWidth);  
  148.         int heightRatio = (int) Math.ceil(imgHeight / (float) targetHeight);  
  149.         if (widthRatio > 1 && widthRatio > 1) {  
  150.             if (widthRatio > heightRatio) {  
  151.                 opts.inSampleSize = widthRatio;  
  152.             } else {  
  153.                 opts.inSampleSize = heightRatio;  
  154.             }  
  155.         }  
  156.         // 设置好缩放比例后,加载图片进内存;  
  157.         opts.inJustDecodeBounds = false;  
  158.         bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opts);  
  159.         return bitmap;  
  160.     }  
  161.   
  162. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值