Android 图片压缩器
概述
详细
一、android图片压缩器特点
1.压缩比可以选择控制。
2.高效率的多线程压缩
3.支持批量压缩
二、实现过程
2.1 压缩任务线程类
该类是用于实现多线程压缩任务,利用CountDownLatch 计数计数实现多线程任务等待机制,核心代码如下:
/**
* Created by caizhiming on 2016/6/2.
* 图片压缩任务线程
*/
public class CompressTask extends Thread{
CountDownLatch mLatch;
Boolean mRet;
String mInFilePath;
String mOutFilePath;
boolean mIsNeedCompress;
public CompressTask(CountDownLatch latch,Boolean ret,String inFilePath,String outFilePath,boolean isNeedCompress){
mLatch = latch;
mRet = ret;
mInFilePath = inFilePath;
mOutFilePath = outFilePath;
mIsNeedCompress = isNeedCompress;
}
@Override
public void run() {
if(mIsNeedCompress) {
mRet = NativeBitmapUtil.syncCompressBitmap(mInFilePath,mOutFilePath);
mLatch.countDown();
}else{
mRet = true;
mLatch.countDown();
}
}
}
2.2 图片图像工具类
该工具类主要对Bitmap图像处理做了一些封装,比如Bitmap旋转,获取旋转角度等,核心代码如下:
/**
* 获取旋转后的图片
* @param path
* @return
*/
public static Bitmap getRotateBitmapByPath(String path) {
return rotateBitmapByDegree(BitmapFactory.decodeFile(path), getBitmapDegree(path));
}
/**
* 获取旋转后的图片
* @param path
* @return
*/
public static Bitmap getRotateBitmapByPath(String path,final int maxSize) {
BitmapFactory.Options options= new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
options.inSampleSize = BitmapUtils.calculateInSampleSize(options,maxSize,maxSize);
options.inJustDecodeBounds = false;
return rotateBitmapByDegree(BitmapFactory.decodeFile(path,options), getBitmapDegree(path));
}
/**
* 将图片按照某个角度进行旋转
*
* @param bm
* 需要旋转的图片
* @param degree
* 旋转角度
* @return 旋转后的图片
*/
public static Bitmap rotateBitmapByDegree(Bitmap bm, int degree) {
Bitmap returnBm = null; // 根据旋转角度,生成旋转矩阵
Matrix matrix = new Matrix();
matrix.postRotate(degree);
try {
if (bm != null) {
// 将原始图片按照旋转矩阵进行旋转,并得到新的图片
returnBm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
}
} catch (OutOfMemoryError e) {
}
if (returnBm == null) {
returnBm = bm;
}
if (bm != returnBm) {
bm.recycle();
}
return returnBm;
}
/**
* 读取图片的旋转的角度
*
* @param path
* 图片绝对路径
* @return 图片的旋转角度
*/
public static int getBitmapDegree(String path) {
int degree = 0;
try {
// 从指定路径下读取图片,并获取其EXIF信息
ExifInterface exifInterface = new ExifInterface(path);
// 获取图片的旋转信息
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
}
} catch (IOException e) {
e.printStackTrace();
}
return degree;
}
2.3 压缩器类的实现
该功能类只要实现多线程文件压缩功能,支持异步压缩单张图片和多张图片等特性。
2.3.1首先定义图片压缩状态监听接口,如下:
/**
* 图片压缩监听器
*/
public interface ImageCompressListener {
void onSuccess(List<String> outFilePathList); void onFailure(String message);
}
2.3.2 其次提供是否压缩判断方案:
public static long MAX_FILE_SIZE = 1024 * 1024;
/**
* 是否压缩图片:大于 1280*1280的 图片 需要压缩 或者 大小大于1M
* @param inFilePath
* @return isNeedCompress
*/
public static boolean isNeedCompress(String inFilePath){
boolean ret = true;
File file = new File(inFilePath);
if(file != null && file.exists()){
BitmapFactory.Options options= new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(inFilePath,options);
if(options.outWidth <= BitmapUtil.MAX_SIZE
&& options.outHeight <= BitmapUtil.MAX_SIZE){
ret = false;
}
if(file.length() > MAX_FILE_SIZE){
ret = true;
}
}
return ret;
}
2.3.3 最后实现异步压缩功能,核心代码如下:
/**
* 异步压缩多张图片
* @param inFilePathList:需要压缩的图片的路径列表
* @param outList:压缩后输出的新图片的路径列表
* @param listener:压缩监听器
*/
public static void compress(final List<String> inFilePathList, final List<String> outList
, final ImageCompressListener listener) {
final List<String> outFilePathList = (outList == null) ? createOutFilePathList(inFilePathList) : outList;
new AsyncTask<Void, Void, Boolean>() {
@Override
protected Boolean doInBackground(Void... params) {
boolean isSuccess = true;
CountDownLatch singal = new CountDownLatch(inFilePathList.size());
List<Boolean> retList = new ArrayList<>();
for (int i = 0; i < inFilePathList.size(); i++) {
retList.add(i, Boolean.TRUE);
boolean isNeedCompress = isNeedCompress(inFilePathList.get(i));
if(!isNeedCompress){
outFilePathList.set(i,inFilePathList.get(i));
}
CompressTask compressTask = new CompressTask(singal, retList.get(i)
, inFilePathList.get(i), outFilePathList.get(i),isNeedCompress);
compressTask.start();
}
try {
singal.await();
for (Boolean ret : retList) {
if(!ret) isSuccess = false;
} } catch (InterruptedException e) {
e.printStackTrace();
isSuccess = false;
}finally {
if(!isSuccess) {
String failErrorMsg = "";
for(int i =0;i < retList.size();i++){
if(!retList.get(i)){
failErrorMsg += "\nCompress error: "+inFilePathList.get(i);
}
}
}
return isSuccess;
}
} @Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
if(listener != null){
if(result) {
listener.onSuccess(outFilePathList);
for(String path :outFilePathList){
Log.v("czm","outFilePath="+path);
}
}else{
listener.onFailure("图片压缩失败!!");
}
}
}
}.execute();
}
三、使用方法
3.1 使用该压缩器很简单,直接一个方法搞定:
List<String> srcFilePathList = new ArrayList<>();
List<String> outputFilePathList = new ArrayList<>();
//默认压缩方法
XCImageCompressor.compress(srcFilePathList, new XCImageCompressor.ImageCompressListener() {
@Override
public void onSuccess(List<String> outFilePathList) { } @Override
public void onFailure(String message) { }
}); //支持压缩后的输出目录的压缩方法
XCImageCompressor.compress(srcFilePathList, outputFilePathList,new XCImageCompressor.ImageCompressListener() {
@Override
public void onSuccess(List<String> outFilePathList) { } @Override
public void onFailure(String message) { }
});
四、源码目录截图

注:本文著作权归作者,由demo大师发表,拒绝转载,转载需要作者授权
Android 图片压缩器的更多相关文章
- Android 图片压缩、照片选择、裁剪,上传、一整套图片解决方案
1.Android一整套图片解决方案 http://mp.weixin.qq.com/s?__biz=MzAxMTI4MTkwNQ==&mid=2650820998&idx=1& ...
- android图片压缩方法
android 图片压缩方法: 第一:质量压缩法: private Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = ...
- android图片压缩的3种方法实例
android 图片压缩方法: 第一:质量压缩法: private Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = ...
- Android 图片压缩各种方式
前言:由于公司项目当中需要用到压缩这块的相应技术,之前也做过的图片压缩都不是特别的理想, 所以这次花了很多心思,仔细研究和在网上找到了很多相对应的资料.为了就是 以后再做的时候直接拿来用就可以了 ...
- Android图片压缩上传(二)
之前有用到libjpeg,还是有一定的局限性,最近用了一个新的方式,效果还是挺不错,随着作者的版本更新,Bug也随之变少,目前项目中运用已上线. 1.之前的方式Android图片压缩,不失真,上线项目 ...
- Android图片压缩方法总结
本文总结Android应用开发中三种常见的图片压缩方法,分别是:质量压缩法.比例压缩法(根据路径获取图片并压缩)和比例压缩法(根据Bitmap图片压缩). 第一:质量压缩方法: ? 1 2 3 ...
- Android图片压缩
import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java ...
- 性能优化——Android图片压缩与优化的几种方式
图片优化压缩方式大概可以分为以下几类:更换图片格式,质量压缩,采样率压缩,缩放压缩,调用jpeg压缩等1.设置图片格式Android目前常用的图片格式有png,jpeg和webp,png:无损压缩图片 ...
- android图片压缩总结
一.bitmap 图片格式介绍 android中图片是以bitmap形式存在的,那么bitmap所占内存,直接影响到了应用所占内存大小,首先要知道bitmap所占内存大小计算方式: bitmap内存大 ...
随机推荐
- UESTC 2015dp专题 N 导弹拦截 dp
导弹拦截 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/65 Descrip ...
- 国内OCR供应商及其演示链接
最近因为需要收集了一些OCR的供应商,可以做身份证识别,银行卡识别,名片识别等特定场景下的文字识别.在通用场景下的文字识别效果还不是很理想.现在OCR在特定的场景下做得已经很不错了.下面列出下这些厂商 ...
- CentOS 6.9/7通过yum安装指定版本的Node.js
说明:通过yum好处其实很多,环境变量不用配置,配置文件放在大家都熟悉的地方,通过rpm -ql xxx可以知道全部文件的地方等等. Node.js(8.x) 一.安装和配置 1.安装Node.js ...
- Inverted bipolar transistor doubles as a signal clamp
A number of circuits, such as level detectors and AM demodulators, benefit from a rectifier with a l ...
- 前端 fetch 通信
随着前端异步的发展, XHR 这种耦合方式的书写不利于前端异步的 Promise 回调. 而且,写起来也是很复杂. fetch API 本来是在 SW(ServiceWorkers) 中提出的, 不过 ...
- 强悍的javascript手势库
/** * Toucher * git:https://github.com/cometwo/Toucher-1 */ "use strict"; (function (root, ...
- B-树学习笔记
转自:http://blog.csdn.net/acs713/article/details/6880375 B-tree(多路搜索树,并不是二叉的)是一种常见的数据结构.使用B-tree结构可以显著 ...
- redis主键失效机制
Memcached删除主键的方式与Redis有何异同 首先,Memcached 在删除失效主键时也是采用的消极方法,即 Memcached 内部也不会监视主键是否失效,而是在通过 Get 访问主键时才 ...
- EventBus (一) 使用详解——初步使用EventBus
版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 前言:EventBus是上周项目中用到的,网上的文章大都一样,或者过时,有用的没几篇,经过琢磨,请教他人,也终于弄清楚点眉目,记 ...
- 比較不错的一个ios找茬游戏源代码
找茬游戏源代码 .这个是一款很不错的ios找茬游戏源代码,该游戏的兼容性很好的.并且还能够支持ipad和iphone.UI界面设计得也很美丽,游戏源代码真的是一款很完美.并且又很完整的一款休闲类的游戏 ...