不错的图片压缩技术

来源:互联网 发布:知乎 pocket 编辑:程序博客网 时间:2024/06/10 01:06

Android不错的图片压缩方法


一、图片质量压缩

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. /** 
  2.      * 质量压缩方法 
  3.      * 
  4.      * @param image 
  5.      * @return 
  6.      */  
  7.     public static Bitmap compressImage(Bitmap image) {  
  8.   
  9.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  10.         image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中  
  11.         int options = 90;  
  12.   
  13.         while (baos.toByteArray().length / 1024 > 100) { // 循环判断如果压缩后图片是否大于100kb,大于继续压缩  
  14.             baos.reset(); // 重置baos即清空baos  
  15.             image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 这里压缩options%,把压缩后的数据存放到baos中  
  16.             options -= 10;// 每次都减少10  
  17.         }  
  18.         ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中  
  19.         Bitmap bitmap = BitmapFactory.decodeStream(isBm, nullnull);// 把ByteArrayInputStream数据生成图片  
  20.         return bitmap;  
  21.     }  



二、按比例大小压缩 (路径获取图片)

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. /** 
  2.      * 图片按比例大小压缩方法 
  3.      * 
  4.      * @param srcPath (根据路径获取图片并压缩) 
  5.      * @return 
  6.      */  
  7.     public static Bitmap getimage(String srcPath) {  
  8.   
  9.         BitmapFactory.Options newOpts = new BitmapFactory.Options();  
  10.         // 开始读入图片,此时把options.inJustDecodeBounds 设回true了  
  11.         newOpts.inJustDecodeBounds = true;  
  12.         Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);// 此时返回bm为空  
  13.   
  14.         newOpts.inJustDecodeBounds = false;  
  15.         int w = newOpts.outWidth;  
  16.         int h = newOpts.outHeight;  
  17.         // 现在主流手机比较多是800*480分辨率,所以高和宽我们设置为  
  18.         float hh = 800f;// 这里设置高度为800f  
  19.         float ww = 480f;// 这里设置宽度为480f  
  20.         // 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可  
  21.         int be = 1;// be=1表示不缩放  
  22.         if (w > h && w > ww) {// 如果宽度大的话根据宽度固定大小缩放  
  23.             be = (int) (newOpts.outWidth / ww);  
  24.         } else if (w < h && h > hh) {// 如果高度高的话根据宽度固定大小缩放  
  25.             be = (int) (newOpts.outHeight / hh);  
  26.         }  
  27.         if (be <= 0)  
  28.             be = 1;  
  29.         newOpts.inSampleSize = be;// 设置缩放比例  
  30.         // 重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了  
  31.         bitmap = BitmapFactory.decodeFile(srcPath, newOpts);  
  32.         return compressImage(bitmap);// 压缩好比例大小后再进行质量压缩  
  33.     }  



三、按比例大小压缩 (Bitmap)

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. /** 
  2.      * 图片按比例大小压缩方法 
  3.      * 
  4.      * @param image (根据Bitmap图片压缩) 
  5.      * @return 
  6.      */  
  7.     public static Bitmap compressScale(Bitmap image) {  
  8.   
  9.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  10.         image.compress(Bitmap.CompressFormat.JPEG, 100, baos);  
  11.   
  12.         // 判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出  
  13.         if (baos.toByteArray().length / 1024 > 1024) {  
  14.             baos.reset();// 重置baos即清空baos  
  15.             image.compress(Bitmap.CompressFormat.JPEG, 80, baos);// 这里压缩50%,把压缩后的数据存放到baos中  
  16.         }  
  17.         ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());  
  18.         BitmapFactory.Options newOpts = new BitmapFactory.Options();  
  19.         // 开始读入图片,此时把options.inJustDecodeBounds 设回true了  
  20.         newOpts.inJustDecodeBounds = true;  
  21.         Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);  
  22.         newOpts.inJustDecodeBounds = false;  
  23.         int w = newOpts.outWidth;  
  24.         int h = newOpts.outHeight;  
  25.         Log.i(TAG, w + "---------------" + h);  
  26.         // 现在主流手机比较多是800*480分辨率,所以高和宽我们设置为  
  27.         // float hh = 800f;// 这里设置高度为800f  
  28.         // float ww = 480f;// 这里设置宽度为480f  
  29.         float hh = 512f;  
  30.         float ww = 512f;  
  31.         // 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可  
  32.         int be = 1;// be=1表示不缩放  
  33.         if (w > h && w > ww) {// 如果宽度大的话根据宽度固定大小缩放  
  34.             be = (int) (newOpts.outWidth / ww);  
  35.         } else if (w < h && h > hh) { // 如果高度高的话根据高度固定大小缩放  
  36.             be = (int) (newOpts.outHeight / hh);  
  37.         }  
  38.         if (be <= 0)  
  39.             be = 1;  
  40.         newOpts.inSampleSize = be; // 设置缩放比例  
  41.         // newOpts.inPreferredConfig = Config.RGB_565;//降低图片从ARGB888到RGB565  
  42.   
  43.         // 重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了  
  44.         isBm = new ByteArrayInputStream(baos.toByteArray());  
  45.         bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);  
  46.   
  47.         return compressImage(bitmap);// 压缩好比例大小后再进行质量压缩  
  48.   
  49.         //return bitmap;  
  50.     }  
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 二年级孩子语文差怎么办 孩子二年级语文成绩差怎么办 孩子小学二年级语文差怎么办 二年级孩子语文理解能力差怎么办 深圳租房被坑了怎么办 小鸣单车押金退不了怎么办 联想台式一体机忘记密码怎么办 ps直线工具变成箭头了怎么办 笔记本图形处理速度慢怎么办 微信语音发不出去怎么办 ps里的图层锁定怎么办 ps图层丢失了怎么办 PS标题画面太小怎么办 轮胎蹭掉一块皮怎么办 吃香蕉吃的胃难受怎么办 qq糖卡在喉咙里怎么办 头发上粘到了qq糖怎么办 老房子土墙掉土怎么办 速写画的太慢怎么办 艺术生文化课没过线怎么办 5岁儿童坐飞机忘带证件怎么办 儿童坐飞机没带证件怎么办 儿童坐飞机没带户口本怎么办 儿童坐飞机没有带户口本怎么办 刚打蜡的车下雨怎么办 飞机票不能退票不能改签怎么办 深圳航空买机票姓名错了怎么办 大众cc打不着火怎么办 手上扎了仙人掌刺怎么办 pscs5界面字体太小怎么办 儿童做飞机没带户口本怎么办 黑户口想做飞机怎么办 宝宝坐飞机忘记带证件怎么办 值机柜台关闭了怎么办 值机迟到几分钟怎么办 婴儿坐飞机没带证件怎么办 飞机票买了一天降价1000怎么办 社保卡磁性没了怎么办 社保卡民族错了怎么办 坐飞机婴儿出生证明没带怎么办 手提行李超过5kg怎么办