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

  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学习-使用Async-Http实现图片压缩并上传功能

    (转)Android学习-使用Async-Http实现图片压缩并上传功能 文章转载自:作者:RyaneLee链接:http://www.jianshu.com/p/940fc7ba39e1 让我头疼一 ...

  2. Android 调用jepg库进行图片压缩,保持图片不失真

    1. 浅谈为什么Android和iOS图片质量差距那么大? 首先来说,作为一个安卓狗,机器当然用的是安卓的手机.现在的安卓手机大多数都会以高清拍照,动不动就几千万柔光相机来吸引各种买家.买来后,拍照发 ...

  3. android apk瘦身之 图片压缩 tinypng

    参考地址: http://blog.csdn.net/jy692405180/article/details/52409369 http://www.tuicool.com/articles/BraI ...

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

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

  5. Android 拍照后保证保证图片不失真,进行压缩

    今天在网上找了一下参考,得出把图片压缩至KB 其他不想多说.直接上代码 拍完照后调用下面代码 BitmapUtils.compressBitmap(photoPath, photoPath, 640) ...

  6. Android图片压缩方法总结

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

  7. Android webview实现上传图片的效果(图片压缩)

    mainactivity代码 package com.bwie.webviewupload; import java.io.ByteArrayInputStream; import java.io.B ...

  8. Android中的图片压缩

    1.android中计算图片占用堆内存的kB大小跟图片本身的kB大小无关,而是根据图片的尺寸来计算的. 比如一张 480*320大小的图片占用的堆内存大小为: 480*320*4/1024=600kB ...

  9. Android的图片压缩并上传

    Android开发中上传图片很常见,一般为了节省流量会进行压缩的操作,本篇记录一下压缩和上传的方法. 图片压缩的方法 : import java.io.ByteArrayOutputStream; i ...

随机推荐

  1. layoutsubviews什么时候调用

    layoutSubviews在以下情况下会被调用:1.init初始化不会触发layoutSubviews2.addSubview会触发layoutSubviews3.设置view的Frame会触发la ...

  2. C++Primer 第六章

    //1.我们通过调用运算符来执行函数.调用运算符的形式是一对圆括号,他作用于一个表达式,该表达式是一个函数或者指向函数的指针.圆括号之内是用逗号分隔的实参列表,用于初始化函数形参.调用表达式的类型就是 ...

  3. C++Primer 第四章

    //1.当我们对运算符进行重载的时候,其包括运算对象的类型和返回值的类型都是由该运算符定义的,但是运算对象的个数和优先级,结合律都是不能改变的 //2.当一个对象被用作右值的时候,用的是对象的值(内容 ...

  4. PostgreSQL Replication之第十二章 与Postgres-XC一起工作(6)

    12.6 添加节点 Postgres-XC允许您在那个过程中的任何一个时间点添加新的服务器到计划中.所有您需要做的是按照我们之前演示的设置一个节点,并在 控制器上调用CREATE NODE.然后,该系 ...

  5. 入门训练 A+B问题

    http://lx.lanqiao.org/problemset.page?code=BEGIN-&userid=34549   入门训练 A+B问题   时间限制:1.0s   内存限制:2 ...

  6. UVA 10498 Happiness(线性规划-单纯形)

    Description Prof. Kaykobad has given Nasa the duty of buying some food for the ACM contestents. Nasa ...

  7. Spark on Mesos部署

    一.Mesos的安装和部署 1.下载mesos源码和依赖包 部署环境 centOS 6.6 mesos-0.21.0 spark-1.4.1 因为mesos官方只提供源码,所以必须要自己进行编译安装使 ...

  8. paper 60 :转载关于视觉SCI期刊

    ChanLee_1整理的计算机视觉领域稍微容易中的期刊 模式识别,计算机视觉领域,期刊 (1)pattern recognition letters, 从投稿到发表,一年半时间 (2)Pattern  ...

  9. 为centos添加额外的源

    使用这个命令: yum install epel-release

  10. [OrangePi] Backup internal EMMC to SD Card

    Boot your Orange PI board from EMMC without SD Card inserted login insert your SD Card Run: sudo ins ...