引用:http://104zz.iteye.com/blog/1694762

第一:我们先看下质量压缩方法:

  1. private Bitmap compressImage(Bitmap image) {
  2. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  3. image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
  4. int options = 100;
  5. while ( baos.toByteArray().length / 1024>100) {  //循环判断如果压缩后图片是否大于100kb,大于继续压缩
  6. baos.reset();//重置baos即清空baos
  7. image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中
  8. options -= 10;//每次都减少10
  9. }
  10. ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中
  11. Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream数据生成图片
  12. return bitmap;
  13. }

第二:图片按比例大小压缩方法(根据路径获取图片并压缩):

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

第三:图片按比例大小压缩方法(根据Bitmap图片压缩):

  1. private Bitmap comp(Bitmap image) {
  2. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  3. image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
  4. if( baos.toByteArray().length / 1024>1024) {//判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出
  5. baos.reset();//重置baos即清空baos
  6. image.compress(Bitmap.CompressFormat.JPEG, 50, baos);//这里压缩50%,把压缩后的数据存放到baos中
  7. }
  8. ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
  9. BitmapFactory.Options newOpts = new BitmapFactory.Options();
  10. //开始读入图片,此时把options.inJustDecodeBounds 设回true了
  11. newOpts.inJustDecodeBounds = true;
  12. Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
  13. newOpts.inJustDecodeBounds = false;
  14. int w = newOpts.outWidth;
  15. int h = newOpts.outHeight;
  16. //现在主流手机比较多是800*480分辨率,所以高和宽我们设置为
  17. float hh = 800f;//这里设置高度为800f
  18. float ww = 480f;//这里设置宽度为480f
  19. //缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
  20. int be = 1;//be=1表示不缩放
  21. if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放
  22. be = (int) (newOpts.outWidth / ww);
  23. } else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放
  24. be = (int) (newOpts.outHeight / hh);
  25. }
  26. if (be <= 0)
  27. be = 1;
  28. newOpts.inSampleSize = be;//设置缩放比例
  29. //重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
  30. isBm = new ByteArrayInputStream(baos.toByteArray());
  31. bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
  32. return compressImage(bitmap);//压缩好比例大小后再进行质量压缩
  33. }

android 图片压缩的更多相关文章

  1. Android 图片压缩、照片选择、裁剪,上传、一整套图片解决方案

    1.Android一整套图片解决方案 http://mp.weixin.qq.com/s?__biz=MzAxMTI4MTkwNQ==&mid=2650820998&idx=1& ...

  2. android图片压缩方法

    android 图片压缩方法: 第一:质量压缩法: private Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = ...

  3. android图片压缩的3种方法实例

    android 图片压缩方法: 第一:质量压缩法: private Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = ...

  4. Android 图片压缩器

    概述 Android 图片压缩器:一款高效的图片压缩器库,支持批量压缩,异步压缩.多线程多任务压缩,压缩比设置等特性. 详细 代码下载:http://www.demodashi.com/demo/12 ...

  5. Android 图片压缩各种方式

       前言:由于公司项目当中需要用到压缩这块的相应技术,之前也做过的图片压缩都不是特别的理想, 所以这次花了很多心思,仔细研究和在网上找到了很多相对应的资料.为了就是 以后再做的时候直接拿来用就可以了 ...

  6. Android图片压缩上传(二)

    之前有用到libjpeg,还是有一定的局限性,最近用了一个新的方式,效果还是挺不错,随着作者的版本更新,Bug也随之变少,目前项目中运用已上线. 1.之前的方式Android图片压缩,不失真,上线项目 ...

  7. Android图片压缩方法总结

    本文总结Android应用开发中三种常见的图片压缩方法,分别是:质量压缩法.比例压缩法(根据路径获取图片并压缩)和比例压缩法(根据Bitmap图片压缩).   第一:质量压缩方法:   ? 1 2 3 ...

  8. Android图片压缩

    import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java ...

  9. 性能优化——Android图片压缩与优化的几种方式

    图片优化压缩方式大概可以分为以下几类:更换图片格式,质量压缩,采样率压缩,缩放压缩,调用jpeg压缩等1.设置图片格式Android目前常用的图片格式有png,jpeg和webp,png:无损压缩图片 ...

  10. android图片压缩总结

    一.bitmap 图片格式介绍 android中图片是以bitmap形式存在的,那么bitmap所占内存,直接影响到了应用所占内存大小,首先要知道bitmap所占内存大小计算方式: bitmap内存大 ...

随机推荐

  1. Django分析之国际化处理

    最近在公司终于开始做web开发了,本以为会是简单的首页之类的小规模项目,结果上来就是一个处理大数据分析的项目,一个关于油品分析的系统,不过我接到的第一个任务是做这个网站的国际化处理,虽然项目还没有上线 ...

  2. java.lang.NoClassDefFoundError:

    异常信息:十一月 10, 2016 5:20:15 下午 org.apache.catalina.core.StandardContext loadOnStartup严重: Servlet /mgr ...

  3. char和byte的区别

    char的字符数据类型,是无符号型的,占2个字节:大小范围是0-65535: byte是字节数据类型,是有符号型的,占1个字节:大小范围为-128-127: 1, char c1=3; char c= ...

  4. struts-json-plugin result中配置对象的序列化

    注意: 在使用strtus-json-plugin序列化对象属性,配置result标签下includeProperties属性时使用 对象名.*或则指定属性名的方式,不能光写对象属性名. 问题: ac ...

  5. 【转】logback logback.xml常用配置详解(三) <filter>

    原创文章,转载请指明出处:http://aub.iteye.com/blog/1110008, 尊重他人即尊重自己 详细整理了logback常用配置, 不是官网手册的翻译版,而是使用总结,旨在更快更透 ...

  6. SQL SERVER 合并重复行,行列转换

    引用自:http://www.cnblogs.com/love-summer/archive/2012/03/27/2419778.html sql server2000 里面如何实现oracle10 ...

  7. 关于type erasure

    哇,好久没有写blog了,再不写的话,blog的秘密都要忘记了,嘿嘿. 最近在试着参与一个开源项目,名字叫avim(A Vibrate IM),别想多了哟.地址是:https://github.com ...

  8. undefined reference to `libiconv_open 无法编译PHP

    解决方法:#wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.1.tar.gz#tar -zxvf libiconv-1.13.1.tar. ...

  9. webbrowser 内核切换

    修改注册表位置: [HKEY_CURRENT_USER\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER ...

  10. jQuery $ 第二个参数的用法

    jQuery(selector, [context]),相当于 $(context).find(selector) 或者 context.find(selector) $('div').each(fu ...