Android 图片压缩工具,仿微信朋友圈压缩策略。
2018-03-22 09:59
411 查看
项目描述
目前做App开发总绕不开图片这个元素。但是随着手机拍照分辨率的提升,图片的压缩成为一个很重要的问题。单纯对图片进行裁切,压缩已经有很多文章介绍。但是裁切成多少,压缩成多少却很难控制好,裁切过头图片太小,质量压缩过头则显示效果太差。于是自然想到
App巨头“微信”会是怎么处理,
Luban(鲁班)就是通过在微信朋友圈发送近100张不同分辨率图片,对比原图与微信压缩后的图片逆向推算出来的压缩算法。因为有其他语言也想要实现
Luban,所以描述了一遍算法步骤。因为是逆向推算,效果还没法跟微信一模一样,但是已经很接近微信朋友圈压缩后的效果,具体看以下对比!
效果与对比
内容 | 原图 | Luban | |
---|---|---|---|
截屏 720P | 720*1280,390k | 720*1280,87k | d77e720*1280,56k |
截屏 1080P | 1080*1920,2.21M | 1080*1920,104k | 1080*1920,112k |
拍照 13M(4:3) | 3096*4128,3.12M | 1548*2064,141k | 1548*2064,147k |
拍照 9.6M(16:9) | 4128*2322,4.64M | 1032*581,97k | 1032*581,74k |
滚动截屏 | 1080*6433,1.56M | 1080*6433,351k | 1080*6433,482k |
导入
compile 'top.zibin:Luban:1.1.3'
使用
异步调用
Luban内部采用
IO线程进行图片压缩,外部调用只需设置好结果监听即可:
Luban.with(this) .load(photos) // 传人要压缩的图片列表 .ignoreBy(100) // 忽略不压缩图片的大小 .setTargetDir(getPath()) // 设置压缩后文件存储位置 .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调用为例Flowable.just(photos) .observeOn(Schedulers.io()) .map(new Function<List<String>, List<File>>() { @Override public List<File> apply(@NonNull List<String> list) throws Exception { // 同步方法直接返回压缩后的文件 return Luban.with(MainActivity.this).load(list).get(); } }) .observeOn(AndroidSchedulers.mainThread()) .subscribe();
License
Copyright 2016 Zheng Zibin Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Github: https://github.com/Curzibn/Luban
相关文章推荐
- Luban(鲁班) —— Android图片压缩工具,仿微信朋友圈压缩策略。
- Android之图片压缩和Uri与String类型的路径转换,获取图片,文件大小的工具类
- Luban—— Android图片压缩工具
- Android图片压缩工具-----luban压缩
- Android——Luban图片压缩工具
- AndroidUtils(五)Android图片压缩、旋转工具
- 一款用于在Android设备上获取照片(拍照或从相册、文件中选择)、裁剪图片、压缩图片的开源工具库
- Android 图片压缩 策略
- android图片压缩工具
- Android免费的导出微信朋友圈文字和图片的工具
- TakePhoto是一款用于在Android设备上获取照片(拍照或从相册、文件中选择)、裁剪图片、压缩图片的开源工具库,目前最新版本3.0.0。
- bitmap的六种压缩方式,Android图片压缩
- Android:指定分辨率和清晰度的图片压缩方法源码
- Android学习之利用BitmapFactory工厂压缩图片
- android图片压缩总结2
- 图片压缩工具:grunt-contrib-imagemin
- Android初学------拍照之后获取没有被压缩的图片
- Android下的图片压缩及图片和视频的上传
- Android的图片压缩并上传
- Android拍照按路径保存图片以及图片压缩不失真