<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">用了很久图片压缩,之前人们一直使用google的官方图片压缩方法</span>

final BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodeResource(res, resId, options);options.inSampleSize = calculateInSampleSize(options, reqWidth,reqHeight);options.inJustDecodeBounds = false;return BitmapFactory.decodeResource(res, resId, options);
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {// Raw height and width of imagefinal int height = options.outHeight;final int width = options.outWidth;int inSampleSize = 1;if (height > reqHeight || width > reqWidth) {final int halfHeight = height / 2;final int halfWidth = width / 2;// Calculate the largest inSampleSize value that is a power of 2 and keeps both// height and width larger than the requested height and width.while ((halfHeight / inSampleSize) > reqHeight&& (halfWidth / inSampleSize) > reqWidth) {inSampleSize *= 2;}}return inSampleSize;
}

代码来自google

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

仔细看calucelateInSamplesize方法,该算法返回的是一种压缩比例,仔细一看他的计算过程,你会发现,inSampleSize的变化过程是2-4-8,,而真正进入wile循环时,宽高就已经被看成是小了一半来计算的了,所以,上面那个网站说12M能压缩到0.75M,就是因为差距太大,如果安卓手机内部压缩自己的图片(大概是2M压缩到100K),所以此时,这个方法就适用于android编码压缩了。

所以鄙人针对于android编码时压缩,在此对它进行了优化,优化后代码如下:

运行:   culculateInSampleSize(bm,200,300)效果:

<pre name="code" class="java"><span style="font-family: Arial, Helvetica, sans-serif;">/**</span>

* 计算压缩比例值(改进版 by touch_ping)

* 原版2>4>8...倍压缩
* 当前2>3>4...倍压缩

* @param options
*            解析图片的配置信息
* @param reqWidth
*            所需图片压缩尺寸最小宽度
* @param reqHeight
*            所需图片压缩尺寸最小高度
* @return
*/
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {

final int picheight = options.outHeight;
final int picwidth = options.outWidth;
Log.i("test", "原尺寸:" +  picwidth + "*" +picheight);

int targetheight = picheight;
int targetwidth = picwidth;
int inSampleSize = 1;

if (targetheight > reqHeight || targetwidth > reqWidth) {
while (targetheight  >= reqHeight
&& targetwidth>= reqWidth) {
Log.i("test","压缩:" +inSampleSize + "倍");
inSampleSize += 1;
targetheight = picheight/inSampleSize;
targetwidth = picwidth/inSampleSize;
}
}

Log.i("test","最终压缩比例:" +inSampleSize + "倍");
Log.i("test", "新尺寸:" +  targetwidth + "*" +targetheight);
return inSampleSize;
}

压缩效果如下:

文件大小从1.12M变成81.75k

最终附上完整压缩工具类:

package com.example.mqtest;import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;/*** 图片压缩工具类* @author touch_ping* 2015-1-5 下午1:29:59*/
public class BitmapCompressor {/*** 质量压缩* @author ping 2015-1-5 下午1:29:58* @param image* @param maxkb* @return*/public static Bitmap compressBitmap(Bitmap image,int maxkb) {//L.showlog("压缩图片");ByteArrayOutputStream baos = new ByteArrayOutputStream();image.compress(Bitmap.CompressFormat.JPEG, 50, baos);// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中int options = 100;
//      Log.i("test","原始大小" + baos.toByteArray().length);while (baos.toByteArray().length / 1024 > maxkb) { // 循环判断如果压缩后图片是否大于(maxkb)50kb,大于继续压缩
//          Log.i("test","压缩一次!");baos.reset();// 重置baos即清空baosoptions -= 10;// 每次都减少10image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 这里压缩options%,把压缩后的数据存放到baos中}
//      Log.i("test","压缩后大小" + baos.toByteArray().length);ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream数据生成图片return bitmap;}/*** http://developer.android.com/training/displaying-bitmaps/load-bitmap.html* 官网:获取压缩后的图片* * @param res* @param resId* @param reqWidth*            所需图片压缩尺寸最小宽度* @param reqHeight*            所需图片压缩尺寸最小高度* @return*/public static Bitmap decodeSampledBitmapFromResource(Resources res,int resId, int reqWidth, int reqHeight) {final BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodeResource(res, resId, options);options.inSampleSize = calculateInSampleSize(options, reqWidth,reqHeight);options.inJustDecodeBounds = false;return BitmapFactory.decodeResource(res, resId, options);}/*** 官网:获取压缩后的图片* * @param res* @param resId* @param reqWidth*            所需图片压缩尺寸最小宽度* @param reqHeight*            所需图片压缩尺寸最小高度* @return*/public static Bitmap decodeSampledBitmapFromFile(String filepath,int reqWidth, int reqHeight) {final BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodeFile(filepath, options);options.inSampleSize = calculateInSampleSize(options, reqWidth,reqHeight);options.inJustDecodeBounds = false;return BitmapFactory.decodeFile(filepath, options);}public static Bitmap decodeSampledBitmapFromBitmap(Bitmap bitmap,int reqWidth, int reqHeight) {ByteArrayOutputStream baos = new ByteArrayOutputStream();bitmap.compress(Bitmap.CompressFormat.PNG, 90, baos);byte[] data = baos.toByteArray();final BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodeByteArray(data, 0, data.length, options);options.inSampleSize = calculateInSampleSize(options, reqWidth,reqHeight);options.inJustDecodeBounds = false;return BitmapFactory.decodeByteArray(data, 0, data.length, options);}/*** 计算压缩比例值(改进版 by touch_ping)* * 原版2>4>8...倍压缩* 当前2>3>4...倍压缩* * @param options*            解析图片的配置信息* @param reqWidth*            所需图片压缩尺寸最小宽度O* @param reqHeight*            所需图片压缩尺寸最小高度* @return*/public static int calculateInSampleSize(BitmapFactory.Options options,int reqWidth, int reqHeight) {final int picheight = options.outHeight;final int picwidth = options.outWidth;Log.i("test", "原尺寸:" +  picwidth + "*" +picheight);int targetheight = picheight;int targetwidth = picwidth;int inSampleSize = 1;if (targetheight > reqHeight || targetwidth > reqWidth) {while (targetheight  >= reqHeight&& targetwidth>= reqWidth) {Log.i("test","压缩:" +inSampleSize + "倍");inSampleSize += 1;targetheight = picheight/inSampleSize;targetwidth = picwidth/inSampleSize;}}Log.i("test","最终压缩比例:" +inSampleSize + "倍");Log.i("test", "新尺寸:" +  targetwidth + "*" +targetheight);return inSampleSize;}
}
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {// Raw height and width of imagefinal int height = options.outHeight;final int width = options.outWidth;int inSampleSize = 1;if (height > reqHeight || width > reqWidth) {final int halfHeight = height / 2;final int halfWidth = width / 2;// Calculate the largest inSampleSize value that is a power of 2 and keeps both// height and width larger than the requested height and width.while ((halfHeight / inSampleSize) > reqHeight&& (halfWidth / inSampleSize) > reqWidth) {inSampleSize *= 2;}}return inSampleSize;
}

一个好用的android图片压缩工具类相关推荐

  1. android图片压缩工具类

    好久没写博客了,一方面是因为最近找了家实习单位,很累基本上下班后就没有打不起精神去学习,另一方面我自己觉得写博客确实有点耗时间,趁着周六周日想花点时间研究下fresco,picass,Glide等框架 ...

  2. 整合的图片压缩工具类ImageTools

    根据网上找的资料和自己用到的地方进行修改的图片压缩工具类,有什么不对的地方请见谅,源码如下: public final class ImageTools {/*** Transfer drawable ...

  3. java图片压缩工具类

    java图片压缩工具类 PicCompressUtil.java import java.io.ByteArrayInputStream; import java.io.ByteArrayOutput ...

  4. java图片压缩工具类(指定压缩大小)

    1:先导入依赖 <!--thumbnailator图片处理--><dependency><groupId>net.coobird</groupId>&l ...

  5. Java图片压缩工具类(递归压缩到指定大小范围)

    Java图片压缩工具 工具类使用场景 公司做人脸识别项目时候需要上传学生.家长.教师.访客的正面照图片,但是人脸识别机器有限制只接收200KB-1M的图片,所以必须做图片压缩到指定范围大少. APP上 ...

  6. Android CompressImage图片压缩工具类介绍

    CompressImage是集成图片的质量压缩,像素压缩以及基于Luban算法的图片压缩三种压缩方式的工具 功能如下: 1. 质量压缩:从质量层面进行相关压缩(原图片可通过该工具压缩至小于期望大小的图 ...

  7. Android图片转换类 1. Bitmap去色,转换为黑白的灰度图, 2. Bitmap图片加圆角效果

    /** * 处理图片的工具类. * */public class ImageTools { /***//** * 图片去色,返回灰度图片 * @param bmpOriginal 传入的图片 * @r ...

  8. 分享一个整理了很多Android开发工具类的链接AndroidUtilCode

    https://github.com/Blankj/AndroidUtilCode API Activity相关→ActivityUtils.java→Demo isActivityExists : ...

  9. Luban—— Android图片压缩工具

    项目描述 目前做app开发总绕不开图片这个元素.但是随着手机拍照分辨率的提升,图片的压缩成为一个很重要的问题.单纯对图片进行裁切,压缩已经有很多文章介绍.但是裁切成多少,压缩成多少却很难控制好,裁切过 ...

最新文章

  1. python excel数据框_使用python pandas使用新数据框附加现有excel表
  2. linux svn 开机启动
  3. php传值到模板,laravel 实现向公共模板中传值 (view composer)
  4. englishpod主持人对话文本_Englishpod 69 | 主持人文本讲解 | How Would You Like Your Eggs?...
  5. 对比学习(Contrastive Learning)相关进展梳理
  6. 阿里云Spark Shuffle的优化
  7. Weblogic数据池测试出错
  8. Java使用Redis实现分布式锁来防止重复提交问题
  9. Kubernetes引发“军备赛”,K8s真是企业生存的关键吗
  10. leetcode538 把二叉搜索树转换成累加树
  11. 代码安全检视方法有_在华为写了 13 年代码,都是宝贵的经验
  12. python软件中文翻译_python 写一个桌面版的翻译软件
  13. sql转化为int类型
  14. header简单用处
  15. visual studio写python_将 Visual Studio 变身为 Python IDE
  16. 维护通讯录的方法及群组通讯录 管理平台
  17. 主机window7 64位 虚拟机下安装Ubuntu系统如何实现网络共享ixi
  18. C语言 - 输入x的值,输出y相应的值 x (x<1) y= 2x-1 (1≤x<10) 3x-11 (x≥10)
  19. bcdedit无法打开启动配置数据存储拒绝访问
  20. 三维建模,三维地理信息的作用

热门文章

  1. html跳转京东app,iOS APP 跳转到京东详情页面
  2. 如何通过SEO搜索引擎关键词优化获客?
  3. 怎样才能在技术领域走的更远?
  4. 求6+66+666+6666+66666。(10分)(JAVA)
  5. 失落的帝国-亚特兰蒂斯
  6. 常用的Python第三方编辑器
  7. 如何认识那些从硅谷文化中淘金的公司们
  8. 英语dyamaund钻石dyamaund单词
  9. 手机 听广播 不用 耳机 android,FM手机调频收音机无广告
  10. 为什么服务器刷微信后反复重启,就在刚刚!微信又挂了!有人卸载重装、有人重启手机……好捉急!...