android -------- 压缩图片文件工具类
项目中常常遇到文件压缩问题,上传文件大小限制
今天简单的分享一点干货,文件压缩,图片压缩,压缩Bitmap
主要通过尺寸压缩和质量压缩,以达到清晰度最优
效果图

源码地址: https://github.com/DickyQie/android-util
工具类代码
public class CompressHelper {
private static volatile CompressHelper INSTANCE;
private Context context;
/**
* 最大宽度,默认为720
*/
private float maxWidth = 720.0f;
/**
* 最大高度,默认为960
*/
private float maxHeight = 960.0f;
/**
* 默认压缩后的方式为JPEG
*/
private Bitmap.CompressFormat compressFormat = Bitmap.CompressFormat.JPEG;
/**
* 默认的图片处理方式是ARGB_8888
*/
private Bitmap.Config bitmapConfig = Bitmap.Config.ARGB_8888;
/**
* 默认压缩质量为80
*/
private int quality = 80;
/**
* 存储路径
*/
private String destinationDirectoryPath;
/**
* 文件名前缀
*/
private String fileNamePrefix;
/**
* 文件名
*/
private String fileName;
public static CompressHelper getDefault(Context context) {
if (INSTANCE == null) {
synchronized (CompressHelper.class) {
if (INSTANCE == null) {
INSTANCE = new CompressHelper(context);
}
}
}
return INSTANCE;
}
private CompressHelper(Context context) {
this.context = context;
destinationDirectoryPath = context.getCacheDir().getPath() + File.pathSeparator + FileUtil.FILES_PATH;
}
/**
* 压缩成文件
* @param file 原始文件
* @return 压缩后的文件
*/
public File compressToFile(File file) {
return BitmapUtil.compressImage(context, Uri.fromFile(file), maxWidth, maxHeight,
compressFormat, bitmapConfig, quality, destinationDirectoryPath,
fileNamePrefix, fileName);
}
/**
* 压缩为Bitmap
* @param file 原始文件
* @return 压缩后的Bitmap
*/
public Bitmap compressToBitmap(File file) {
return BitmapUtil.getScaledBitmap(context, Uri.fromFile(file), maxWidth, maxHeight, bitmapConfig);
}
/**
* 采用建造者模式,设置Builder
*/
public static class Builder {
private CompressHelper mCompressHelper;
public Builder(Context context) {
mCompressHelper = new CompressHelper(context);
}
/**
* 设置图片最大宽度
* @param maxWidth 最大宽度
*/
public Builder setMaxWidth(float maxWidth) {
mCompressHelper.maxWidth = maxWidth;
return this;
}
/**
* 设置图片最大高度
* @param maxHeight 最大高度
*/
public Builder setMaxHeight(float maxHeight) {
mCompressHelper.maxHeight = maxHeight;
return this;
}
/**
* 设置压缩的后缀格式
*/
public Builder setCompressFormat(Bitmap.CompressFormat compressFormat) {
mCompressHelper.compressFormat = compressFormat;
return this;
}
/**
* 设置Bitmap的参数
*/
public Builder setBitmapConfig(Bitmap.Config bitmapConfig) {
mCompressHelper.bitmapConfig = bitmapConfig;
return this;
}
/**
* 设置压缩质量,建议80
* @param quality 压缩质量,[0,100]
*/
public Builder setQuality(int quality) {
mCompressHelper.quality = quality;
return this;
}
/**
* 设置目的存储路径
* @param destinationDirectoryPath 目的路径
*/
public Builder setDestinationDirectoryPath(String destinationDirectoryPath) {
mCompressHelper.destinationDirectoryPath = destinationDirectoryPath;
return this;
}
/**
* 设置文件前缀
* @param prefix 前缀
*/
public Builder setFileNamePrefix(String prefix) {
mCompressHelper.fileNamePrefix = prefix;
return this;
}
/**
* 设置文件名称
* @param fileName 文件名
*/
public Builder setFileName(String fileName) {
mCompressHelper.fileName = fileName;
return this;
}
public CompressHelper build() {
return mCompressHelper;
}
}
}
使用
File oldFile = CompressHelper.getDefault(getApplicationContext()).compressToFile(file);
自定义属性使用
File newFile = new CompressHelper.Builder(this)
.setMaxWidth(720) // 默认最大宽度为720
.setMaxHeight(960) // 默认最大高度为960
.setQuality(80) // 默认压缩质量为80
.setFileName(yourFileName) // 文件名称
.setCompressFormat(CompressFormat.JPEG) // 设置默认压缩为jpg格式
.setDestinationDirectoryPath(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).getAbsolutePath())//路径
.build()
.compressToFile(oldFile);
该案例参考了:
- https://github.com/zetbaitsu/Compressor
- https://github.com/Curzibn/Luban
- https://github.com/nanchen2251/CompressHelper
android -------- 压缩图片文件工具类的更多相关文章
- android ImageUtils 图片处理工具类
/** * 加入文字到图片.相似水印文字. * @param gContext * @param gResId * @param gText * @return */ public static Bi ...
- Android FileUtil(android文件工具类)
android开发和Java开发差不了多少,也会有许多相同的功能.像本文提到的文件存储,在Java项目和android项目里面用到都是相同的.只是android开发的一些路径做了相应的处理. 下面就是 ...
- Android开源项目大全 - 工具类
主要包括那些不错的开发库,包括依赖注入框架.图片缓存.网络相关.数据库ORM建模.Android公共库.Android 高版本向低版本兼容.多媒体相关及其他. 一.依赖注入DI 通过依赖注入减少Vie ...
- Android经常使用的工具类
主要介绍总结的Android开发中经常使用的工具类,大部分相同适用于Java. 眼下包含HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils. Pr ...
- 图片处理工具类 - ImageUtils.java
纯JAVA实现的图片处理工具类,提供图片的裁剪.压缩.获取尺寸.制作圆角等方法. 源码如下:(点击下载 -ImageUtils.java .FolderUtils.java .commons-io-2 ...
- Java操作图片的工具类
操作图片的工具类: import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.a ...
- 自动扫描FTP文件工具类 ScanFtp.java
package com.util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ja ...
- java图片处理工具类
直接上代码: package com.zxd.tool; /** * Created by zhang on 14-3-1. * 图片的常用操作类 */ import java.awt.AlphaCo ...
- Android开发调试日志工具类[支持保存到SD卡]
直接上代码: package com.example.callstatus; import java.io.File; import java.io.FileWriter; import java.i ...
随机推荐
- 4、jeecg 笔记之 自定义显示按钮 (exp 属性)
1.需求 先看一下需求吧,我们希望 datagrid 操作栏中的按钮,可以根据条件进行动态显示. 2.实现 其实 jeecg 提供了一个属性 - exp ,通过该属性即可实现. <t:dgFun ...
- 拦截器、过滤器、@Aspect 区别
1.需求场景 之前也有在文章写道 “拦截器\过滤器" 的区别,文章链接,在实际开发过程中,我们可能会遇到拦截请求参数的需求,在这我举个场景. 某一个接口的请求参数都是加密的,而请求参中还有一 ...
- 【LeetCode每天一题】Jump Game II(跳跃游戏II)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Ch04 映射和元组 - 练习
1. 设置一个映射,其中包含你想要的一些装备,以及它们的价格.然后构建另一个映射,采用同一组键,但在价格上打9折. import scala.collection.JavaConversions.as ...
- enzyme design 整体流程及感想
想起什么来写什么吧. 整体流程(以Ceas2, TPP, G3P为例): 准备蛋白即配体参数文件: 设置CST文件: 准备protocol和flag文件: 运行enzyme_design: 结果处理. ...
- [渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:建立一个EF数据模型
英文渣水平,大伙凑合着看吧…… 这是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第一篇: ...
- How do you explain Machine Learning and Data Mining to non Computer Science people?
How do you explain Machine Learning and Data Mining to non Computer Science people? Pararth Shah, ...
- Mysql授权root用户远程登录
默认情况下Mysql的root用户不支持远程登录,使用以下命令授权 [Charles@localhost ~]$ mysql -uroot -p123 MariaDB [(none)]> u ...
- mysql数据据备份与还原
http://www.cnblogs.com/kissdodog/p/4174421.html备份:mysqldump -u username -p dbname > BackupName.sq ...
- 利用js和JQuery定义一个导航条菜单
利用js和JQuery定义一个导航条 效果: 一.html代码: <div class="Maintenance"> <div class="Title ...