前言:目前一般手机的相机都能达到800万像素,像我的Galaxy Nexus才500万像素,拍摄的照片也有1.5M左右。这么大的照片上传到服务器,不仅浪费流量,同时还浪费时间。

在开发Android企业应用时,会经常上传图片到服务器,而我们公司目前维护的一个项目便是如此。该项目是通过私有apn与服务器进行交互的,联通的还好,但移动的速度实在太慢,客户在使用软件的过程中,由于上传的信息中可能包含多张图片,会经常出现上传图片失败的问题,为了解决这个问题,我们决定把照片压缩到100k以下,并且保证图片不失真(目前图片经过压缩后,大约300k左右)。于是我就重新研究了一下Android的图片压缩技术。

Android端目录结构如下图所示:

其中ksoap2-android-xxx.jar是Android用来调用webservice的,gson-xx.jar是把JavaBean转成Json数据格式的。 本篇博客主要讲解图片压缩的,核心代码如下:

  1. //计算图片的缩放值
  2. public static int calculateInSampleSize(BitmapFactory.Options options,int reqWidth, int reqHeight) {
  3. final int height = options.outHeight;
  4. final int width = options.outWidth;
  5. int inSampleSize = 1;
  6. if (height > reqHeight || width > reqWidth) {
  7. final int heightRatio = Math.round((float) height/ (float) reqHeight);
  8. final int widthRatio = Math.round((float) width / (float) reqWidth);
  9. inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
  10. }
  11. return inSampleSize;
  12. }
//计算图片的缩放值
public static int calculateInSampleSize(BitmapFactory.Options options,int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1; if (height > reqHeight || width > reqWidth) {
final int heightRatio = Math.round((float) height/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
  1. // 根据路径获得图片并压缩,返回bitmap用于显示
  2. public static Bitmap getSmallBitmap(String filePath) {
  3. final BitmapFactory.Options options = new BitmapFactory.Options();
  4. options.inJustDecodeBounds = true;
  5. BitmapFactory.decodeFile(filePath, options);
  6. // Calculate inSampleSize
  7. options.inSampleSize = calculateInSampleSize(options, 480, 800);
  8. // Decode bitmap with inSampleSize set
  9. options.inJustDecodeBounds = false;
  10. return BitmapFactory.decodeFile(filePath, options);
  11. }
// 根据路径获得图片并压缩,返回bitmap用于显示
public static Bitmap getSmallBitmap(String filePath) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options); // Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, 480, 800); // Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(filePath, options);
}
  1. //把bitmap转换成String
  2. public static String bitmapToString(String filePath) {
  3. Bitmap bm = getSmallBitmap(filePath);
  4. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  5. bm.compress(Bitmap.CompressFormat.JPEG, 40, baos);
  6. byte[] b = baos.toByteArray();
  7. return Base64.encodeToString(b, Base64.DEFAULT);
  8. }
//把bitmap转换成String
public static String bitmapToString(String filePath) { Bitmap bm = getSmallBitmap(filePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 40, baos);
byte[] b = baos.toByteArray();
return Base64.encodeToString(b, Base64.DEFAULT);
}

查看全部源码,请访问: https://github.com/feicien/StudyDemo/tree/master/FileUploadDemo
压缩原理讲解:压缩一张图片。我们需要知道这张图片的原始大小,然后根据我们设定的压缩比例进行压缩。 这样我们就需要做3件事: 1.获取原始图片的长和宽

  1. BitmapFactory.Options options = new BitmapFactory.Options();
  2. options.inJustDecodeBounds = true;
  3. BitmapFactory.decodeFile(filePath, options);
  4. int height = options.outHeight;
  5. int width = options.outWidth;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
int height = options.outHeight;
int width = options.outWidth;

以上代码是对图片进行解码,inJustDecodeBounds设置为true,可以不把图片读到内存中,但依然可以计算出图片的大小,这正好可以满足我们第一步的需要。 2.计算压缩比例

  1. int height = options.outHeight;
  2. int width = options.outWidth;
  3. int inSampleSize = 1;
  4. int reqHeight=800;
  5. int reqWidth=480;
  6. if (height > reqHeight || width > reqWidth) {
  7. final int heightRatio = Math.round((float) height/ (float) reqHeight);
  8. final int widthRatio = Math.round((float) width / (float) reqWidth);
  9. inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
  10. }
int height = options.outHeight;
int width = options.outWidth;
int inSampleSize = 1;
int reqHeight=800;
int reqWidth=480;
if (height > reqHeight || width > reqWidth) {
final int heightRatio = Math.round((float) height/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}

一般手机的分辨率为 480*800 ,所以我们压缩后图片期望的宽带定为480,高度设为800,这2个值只是期望的宽度与高度,实际上压缩后的实际宽度也高度会比期望的要大。如果图片的原始高度或者宽带大约我们期望的宽带和高度,我们需要计算出缩放比例的数值。否则就不缩放。heightRatio是图片原始高度与压缩后高度的倍数,widthRatio是图片原始宽度与压缩后宽度的倍数。inSampleSize为heightRatio与widthRatio中最小的那个,inSampleSize就是缩放值。 inSampleSize为1表示宽度和高度不缩放,为2表示压缩后的宽度与高度为原来的1/2 3.缩放并压缩图片

  1. //在内存中创建bitmap对象,这个对象按照缩放大小创建的
  2. options.inSampleSize = calculateInSampleSize(options, 480, 800);
  3. options.inJustDecodeBounds = false;
  4. Bitmap bitmap= BitmapFactory.decodeFile(filePath, options);
  5. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  6. bm.compress(Bitmap.CompressFormat.JPEG, 60, baos);
  7. byte[] b = baos.toByteArray();
//在内存中创建bitmap对象,这个对象按照缩放大小创建的
options.inSampleSize = calculateInSampleSize(options, 480, 800);
options.inJustDecodeBounds = false;
Bitmap bitmap= BitmapFactory.decodeFile(filePath, options); ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 60, baos);
byte[] b = baos.toByteArray();

前3行的代码其实已经得到了一个缩放的bitmap对象,如果你在应用中显示图片,就可以使用这个bitmap对象了。由于考虑到网络流量的问题。我们好需要牺牲图片的质量来换取一部分空间,这里调用bm.compress()方法进行压缩,这个方法的第二个参数,如果是100,表示不压缩,我这里设置的是60,你也可以更加你的需要进行设置,在实验的过程中我设置为30,图片都不会失真。 压缩效果:本demo可以把1.5M左右的图片压缩到100K左右,并且没有失真。 效果图如下:

转自:http://my.eoe.cn/575776/archive/564.html

Android压缩图片到100K以下并保持不失真的高效方法的更多相关文章

  1. Android --------- 压缩图片的尺寸和大小

    压缩图片大小,尺寸不变 将已知路径的图片压缩至不大于目标大小,并保存至指定路径 /** * 质量压缩,通过给定的路径来压缩图片并保存到指定路径 * * @param srcPath * 资源图片的路径 ...

  2. [Android] 压缩图片并保存

    不难,但用的时候有时候突然会想不起来..记录一下吧 原文地址请保留http://www.cnblogs.com/rossoneri/p/3995096.html 先加权限 <uses-permi ...

  3. android 压缩图片大小,防止OOM

    android开发中,图片的处理是非常普遍的,经常是需要将用户选择的图片上传到服务器,但是现在手机的分辨率越来越好了,随便一张照片都是2M或以上,如果直接显示到ImageView中,是会出现OOM的, ...

  4. android -------- 压缩图片文件工具类

    项目中常常遇到文件压缩问题,上传文件大小限制 今天简单的分享一点干货,文件压缩,图片压缩,压缩Bitmap 主要通过尺寸压缩和质量压缩,以达到清晰度最优 效果图 源码地址: https://githu ...

  5. Xamarin.Android 压缩图片并上传到WebServices

    随着手机的拍照像素越来越高,导致图片赞的容量越来越大,如果上传多张图片不进行压缩.质量处理很容易出现OOM内存泄漏问题. 最近做了一个项目,向webservices上传多张照片,但是项目部署出来就会出 ...

  6. Android 下压缩图片—微弱失真

    Android下压缩图片的方法: 大概能将3M左右的图片压缩到100K左右, 几乎不失真. 代码如下: import java.io.FileNotFoundException; import jav ...

  7. Android 高清加载巨图方案 拒绝压缩图片

    Android 高清加载巨图方案 拒绝压缩图片 转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/49300989: 本文出自:[张 ...

  8. LocalResizeIMG前端HTML5本地压缩图片上传,兼容移动设备IOS,android

    LocalResizeIMG前端HTML5本地压缩图片上传,兼容移动设备IOS,android jincon 发表于 2015-02-26 18:31:01 发表在: php开发 localresiz ...

  9. 上传图片转为base64格式预览并压缩图片(不兼容IE9以下浏览器,兼容移动端ios,android)

    前些天公司要求在微信移动端做上传图片并预览的功能,要求能够调用摄像头拍照并立即预览. 在网上搜了一些方法,开始自己写了个简单的功能实现代码.结果发现移动端拍照出来的图片动不动就2M+,又因为要批量上传 ...

随机推荐

  1. html之select标签

    循环select标签 <select name="group_id"> {% for row in group_list %} <option value={{r ...

  2. ssh登录 The authenticity of host 192.168.0.xxx can't be established. 的问题

    scp免密码登录:Linux基础 - scp免密码登陆进行远程文件同步 执行scp一直是OK的,某天在本地生成了公钥私钥后,scp到某个IP报以下错误 The authenticity of host ...

  3. [LeetCode] Factor Combinations 因子组合

    Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write a func ...

  4. 【JavaWeb学习】文件的上传和下载

    一.文件上传 1.1.概述 实现web开发中的文件上传功能,需完成如下二步操作: 在web页面中添加上传输入项 在servlet中读取上传文件的数据,并保存到本地硬盘中 如何在web页面中添加上传输入 ...

  5. poj 1251 Jungle Roads (最小生成树)

    poj   1251  Jungle Roads  (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...

  6. mybatis返回数据类型为map,值为null的key没返回

    创建mybatis-config.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ...

  7. Azure AD Connect 手动同步

    我们目前采用工具Azure AD Connect 目录同步工具将本地域控制器的用户信息同步至office365和Azure 在之前目录同步工具中使用Windows 任务计划程序或单独的 Windows ...

  8. 玩转Redis之Window安装使用(干货)

    距离上次定Gc.Db框架,好久没有更新博客了,今日没什么事,就打算就Redis写点东西. Redis是一个开源(BSD许可),内存存储的数据结构服务器,可用作数据库,高速缓存和消息队列代理.它支持字符 ...

  9. mybatis 一对一与一对多collection和association的使用

    在mybatis如何进行一对一.一对多的多表查询呢?这里用一个简单的例子说明. 一.一对一 1.association association通常用来映射一对一的关系,例如,有个类user,对应的实体 ...

  10. bitset用法总结

    b.any() b中是否存在置为1的二进制位? b.none() b中不存在置为1的二进制位吗? b.count() b中置为1的二进制位的个数 b.size() b中二进制位的个数 b[pos] 访 ...