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

  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. 在struts里使用Kindeditor注意事项

    struts配置文件里 <filter-mapping>          <filter-name>struts2</filter-name>          ...

  2. 安装GRID时跑root.sh脚本报错(ORA-27091: unable to queue I/O)

    在安装GRID过程中,运行root.sh脚本时报如下信息: Adding Clusterware entries to upstart CRS-2672: Attempting to start 'o ...

  3. iOS 事件穿透

    - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...

  4. 布置theano(Ubuntu14.04 LTS)

    引言 由于研究生阶段将会从事自然语言处理方向的研究,目前要用到机器学习和深度学习相关的框架,那应老师的要求,将要使用theano,由于theano官方文档中关于ubuntu下配置的问题并没有给出很好的 ...

  5. Java String.split()

    在java.lang包中有String.split()方法,返回是一个数组 我在应用中用到一些,给大家总结一下,仅供大家参考: 1.如果用“.”作为分隔的话,必须是如下写法,String.split( ...

  6. CPU informition

    tar jxvf util-linux-ng-2.18.bz2cd util-linux-ng-2.18/./configure --enable-arch --enable-partx --enab ...

  7. 转:python webdriver 环境搭建

    第一节 环境搭建准备工具如下:-------------------------------------------------------------下载 python[python 开发环境]ht ...

  8. The golden ratio: 1.618

    http://www.chinaz.com/design/2015/1109/467968_2.shtml The golden ratio: 1.618 a/b=b/(a+b) The Fibona ...

  9. Keyframes介绍

    Keyframes被称为关键帧,其类似于Flash中的关键帧.在CSS3中其主要以“@keyframes”开头,后面紧跟着是动画名称加上一对花括号“{…}”,括号中就是一些不同时间段样式规则. @ke ...

  10. II7下配置SSAS通过HTTP 远程链接访问

    IIS7下配置SSAS通过HTTP远程连接 安装环境操作系统:Windows7.Windows Server2008IIS版本:7.5 IIS7下配置SSAS通过HTTP远程连接详细的步骤如下:1.首 ...