Luban(鲁班)—Image compression with efficiency very close to WeChat Moments/可能是最接近微信朋友圈的图片压缩算法
|
6 anos atrás | |
---|---|---|
.idea | 6 anos atrás | |
Translation | 8 anos atrás | |
example | 6 anos atrás | |
gradle | 6 anos atrás | |
library | 6 anos atrás | |
.gitignore | 7 anos atrás | |
.travis.yml | 6 anos atrás | |
DESCRIPTION.md | 8 anos atrás | |
LICENSE | 8 anos atrás | |
README.md | 6 anos atrás | |
build.gradle | 6 anos atrás | |
gradle.properties | 8 anos atrás | |
gradlew | 8 anos atrás | |
gradlew.bat | 8 anos atrás | |
settings.gradle | 8 anos atrás |
Luban
(鲁班) —— Android
图片压缩工具,仿微信朋友圈压缩策略。
本分支为Luban
主体项目引入libjpeg-turbo
的jni
版本
libjpeg-turbo
是一个C语音编写的高效JPEG图像处理库,Android
系统在7.0版本之前内部使用的是libjpeg
非turbo版,并且为了性能关闭了Huffman
编码。在7.0之后的系统内部使用了libjpeg-turbo
库并且启用Huffman
编码。
使用本分支可使7.0之前的系统也用上压缩速度更快,压缩效果更好的libjpeg-turbo
库,但是需引入额外的so
文件,会增加最终打包后app
文件的大小。
本分支c语音源代码可查看此项目:Luban-Turbo
implementation 'top.zibin:Luban-turbo:1.0.0'
build.gradle
添加ndk
配置
android {
defaultConfig {
ndk {
abiFilters 'armeabi-v7a', 'arm64-v8a' //or x86、x86_64
}
}
}
拷贝so
文件到项目jniLibs
文件夹
Luban 提供4个平台的so
文件:
armeabi-v7a
、
arm64-v8a
、
x86
、
x86_64
方法 | 描述 |
---|---|
load | 传入原图 |
filter | 设置开启压缩条件 |
ignoreBy | 不压缩的阈值,单位为K |
setFocusAlpha | 设置是否保留透明通道 |
setTargetDir | 缓存压缩图片路径 |
setCompressListener | 压缩回调接口 |
setRenameListener | 压缩前重命名接口 |
Luban
内部采用IO
线程进行图片压缩,外部调用只需设置好结果监听即可:
Luban.with(this)
.load(photos)
.ignoreBy(100)
.setTargetDir(getPath())
.filter(new CompressionPredicate() {
@Override
public boolean apply(String path) {
return !(TextUtils.isEmpty(path) || path.toLowerCase().endsWith(".gif"));
}
})
.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();
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.