android压缩图片的工具,android -------- 压缩图片文件工具类

public classCompressHelper {private static volatileCompressHelper INSTANCE;privateContext 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;/*** 存储路径*/

privateString destinationDirectoryPath;/*** 文件名前缀*/

privateString fileNamePrefix;/*** 文件名*/

privateString fileName;public staticCompressHelper getDefault(Context context) {if (INSTANCE == null) {synchronized (CompressHelper.class) {if (INSTANCE == null) {

INSTANCE= newCompressHelper(context);

}

}

}returnINSTANCE;

}privateCompressHelper(Context context) {this.context =context;

destinationDirectoryPath= context.getCacheDir().getPath() + File.pathSeparator +FileUtil.FILES_PATH;

}/*** 压缩成文件

*@paramfile 原始文件

*@return压缩后的文件*/

publicFile compressToFile(File file) {returnBitmapUtil.compressImage(context, Uri.fromFile(file), maxWidth, maxHeight,

compressFormat, bitmapConfig, quality, destinationDirectoryPath,

fileNamePrefix, fileName);

}/*** 压缩为Bitmap

*@paramfile 原始文件

*@return压缩后的Bitmap*/

publicBitmap compressToBitmap(File file) {returnBitmapUtil.getScaledBitmap(context, Uri.fromFile(file), maxWidth, maxHeight, bitmapConfig);

}/*** 采用建造者模式,设置Builder*/

public static classBuilder {privateCompressHelper mCompressHelper;publicBuilder(Context context) {

mCompressHelper= newCompressHelper(context);

}/*** 设置图片最大宽度

*@parammaxWidth 最大宽度*/

public Builder setMaxWidth(floatmaxWidth) {

mCompressHelper.maxWidth=maxWidth;return this;

}/*** 设置图片最大高度

*@parammaxHeight 最大高度*/

public Builder setMaxHeight(floatmaxHeight) {

mCompressHelper.maxHeight=maxHeight;return this;

}/*** 设置压缩的后缀格式*/

publicBuilder setCompressFormat(Bitmap.CompressFormat compressFormat) {

mCompressHelper.compressFormat=compressFormat;return this;

}/*** 设置Bitmap的参数*/

publicBuilder setBitmapConfig(Bitmap.Config bitmapConfig) {

mCompressHelper.bitmapConfig=bitmapConfig;return this;

}/*** 设置压缩质量,建议80

*@paramquality 压缩质量,[0,100]*/

public Builder setQuality(intquality) {

mCompressHelper.quality=quality;return this;

}/*** 设置目的存储路径

*@paramdestinationDirectoryPath 目的路径*/

publicBuilder setDestinationDirectoryPath(String destinationDirectoryPath) {

mCompressHelper.destinationDirectoryPath=destinationDirectoryPath;return this;

}/*** 设置文件前缀

*@paramprefix 前缀*/

publicBuilder setFileNamePrefix(String prefix) {

mCompressHelper.fileNamePrefix=prefix;return this;

}/*** 设置文件名称

*@paramfileName 文件名*/

publicBuilder setFileName(String fileName) {

mCompressHelper.fileName=fileName;return this;

}publicCompressHelper build() {returnmCompressHelper;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Luban(鲁班)——Android图片压缩工具,仿微信朋友圈压缩策略。项目描述目前做app开发总绕不开图片这个元素。但是随着手机拍照分辨率的提升,图片压缩成为一个很重要的问题。单纯对图片进行裁切,压缩已经有很多文章介绍。但是裁切成多少,压缩成多少却很难控制好,裁切过头图片太小,质量压缩过头则显示效果太差。于是自然想到app巨头“微信”会是怎么处理,Luban(鲁班)就是通过在微信朋友圈发送近100张不同分辨率图片,对比原图与微信压缩后的图片逆向推算出来的压缩算法。因为有其他语言也想要实现 Luban,所以描述了一遍算法步骤 因为是逆向推算,效果还没法跟微信一模一样,但是已经很接近微信朋友圈压缩后的效果,具体看以下对比!效果与对比内容原图LubanWechat截屏 720P720*1280,390k720*1280,87k720*1280,56k截屏 1080P1080*1920,2.21M1080*1920,104k1080*1920,112k拍照 13M(4:3)3096*4128,3.12M1548*2064,141k1548*2064,147k拍照 9.6M(16:9)4128*2322,4.64M1032*581,97k1032*581,74k滚动截屏1080*6433,1.56M1080*6433,351k1080*6433,482k导入compile 'io.reactivex:rxandroid:1.2.1' compile 'io.reactivex:rxjava:1.1.6' compile 'top.zibin:Luban:1.0.5'使用Listener方式Luban内部采用io线程进行图片压缩,外部调用只需设置好结果监听即可Luban.get(this)     .load(File)                     //传人要压缩图片     .putGear(Luban.THIRD_GEAR)      //设定压缩档次,默认三挡     .setCompressListener(new OnCompressListener() { //设置回调         @Override         public void onStart() {             //TODO 压缩开始前调用,可以在方法内启动 loading UI         }         @Override         public void onSuccess(File file) {             //TODO 压缩成功后调用,返回压缩后的图片文件         }         @Override         public void onError(Throwable e) {             //TODO 当压缩过去出现问题时调用         }     }).launch();    //启动压缩RxJava方式RxJava 调用方式请自行随意控制线程Luban.get(this)         .load(file)         .putGear(Luban.THIRD_GEAR)         .asObservable()         .subscribeOn(Schedulers.io())         .observeOn(AndroidSchedulers.mainThread())         .doOnError(new Action1() {             @Override             public void call(Throwable throwable) {                 throwable.printStackTrace();             }         })         .onErrorResumeNext(new Func1>() {             @Override             public Observable<? extends File> call(Throwable throwable) {                 return Observable.empty();             }         })         .subscribe(new Action1() {             @Override             public void call(File file) {                 //TODO 压缩成功后调用,返回压缩后的图片文件             }         }); 标签:Luban(鲁班)
Android图片压缩工具类是一种用于压缩Android应用中的图片文件工具。根据引用和引用的内容,这个工具类的具体实现可能包括以下功能: - 通过将图片文件转换为字节数组,以便进行后续的压缩处理。 - 使用缓冲输出流将压缩后的图片数据写入输出流中。 - 通过设置合适的压缩参数,对图片进行压缩操作,减小文件大小但尽量保持良好的图像质量。 - 可能还包括一些其他的图像处理操作,如旋转、裁剪等。 具体的实现细节可以参考引用和引用提供的代码示例。在这些示例中,使用了ByteArrayInputStream和ByteArrayOutputStream类来处理字节数组的输入和输出。通过设置合适的压缩参数,可以实现对图片压缩操作。此外,还可以使用BufferedOutputStream类来提高输出流的写入性能。 如果想要进一步了解关于Android图片压缩工具类的使用和实现细节,可以参考引用提供的配套资料,该资料可能提供了更详细的解释和示例代码。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Android开发之图片压缩工具类完整实例](https://download.csdn.net/download/weixin_38517904/14018839)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [android图片压缩工具类分享](https://download.csdn.net/download/weixin_38616330/14881444)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Spring Boot(六十四):SpringBoot集成Gzip压缩数据](https://download.csdn.net/download/u013938578/88221156)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值