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

  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. 学习OpenCV——用OpenCv画漫画

    闲的时候用OpenCV画漫画也挺有意思,虽然效果不好(达不到上面所实现的效果), 参数需要调整,还是大头贴而且噪声小的图像比较合适 而且可以熟悉一下关于各种滤波的操作比如:双边滤波: #include ...

  2. 1029-对c语言文法的理解

    <程序>→<外部声明>|<程序><外部声明> <外部声明>→<函数定义>|<声明> <函数定义>→< ...

  3. hduoj 4706 Children&#39;s Day 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4706 Children's Day Time Limit: 2000/1000 MS (Java/Others) ...

  4. csuoj 1328: 近似回文词

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1328 1328: 近似回文词 Time Limit: 1 Sec  Memory Limit: 1 ...

  5. fzuoj Problem 2182 水题

    http://acm.fzu.edu.cn/problem.php?pid=2182 Problem 2182 水题 Accept: 188    Submit: 277Time Limit: 100 ...

  6. C++之路进阶——codevs3287(货车运输)

    3287 货车运输 2013年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond   题目描述 Description A 国有 n ...

  7. Java的正则表达式

    package RegexTest; /** * Created by hu on 2016/3/29. */ /* * Java的正则表达式 在正则表达式中,用\d表示一位数字,如果在其它语言中使用 ...

  8. css3:与背景的相关样式

    1. (1)background-origin : border-box | padding-box | content-box;(设置元素背景图片的原始起始位置.) //需要注意的是,如果背景不是n ...

  9. yii2语言设置

    1.父配置文件在yii2/base/Application中的$language="en-US", 修改项目的语言可以修改项目的配置文件main.php中加'language'=& ...

  10. linux_c学习笔记之curl的使用一

    参考文档 使用libcurl发送PUT请求上传数据以及DELETE请求删除数据 http://blog.163.com/lixiangqiu_9202/blog/static/535750372014 ...