Luban(鲁班)—Image compression with efficiency very close to WeChat Moments/可能是最接近微信朋友圈的图片压缩算法

郑梓斌 e0e7ec081b update turbo 6 anos atrás
.idea c5e37167f0 update gradle version and format source code 6 anos atrás
Translation 49ef28d0fa improve docs format 8 anos atrás
example 6014fb1bca finish turbo version 6 anos atrás
gradle c5e37167f0 update gradle version and format source code 6 anos atrás
library 6014fb1bca finish turbo version 6 anos atrás
.gitignore 9f8d1ea298 优化算法,压缩速度更快,压缩效果更好 7 anos atrás
.travis.yml b28e6fb312 update travis 6 anos atrás
DESCRIPTION.md 49ef28d0fa improve docs format 8 anos atrás
LICENSE ec9290a733 normal commit 8 anos atrás
README.md 1315bd2be4 update turbo 6 anos atrás
build.gradle bc3f3d4c0d add `getOrientation` method in engine 6 anos atrás
gradle.properties af17aca396 新增Rxjava方式调用 8 anos atrás
gradlew 55cc4c0aeb first commit 8 anos atrás
gradlew.bat 55cc4c0aeb first commit 8 anos atrás
settings.gradle 55cc4c0aeb first commit 8 anos atrás

README.md

Luban

Build Status Download

Luban(鲁班) —— Android图片压缩工具,仿微信朋友圈压缩策略。

分支描述

本分支为Luban主体项目引入libjpeg-turbojni版本

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'

使用

引入JNI动态文件

  • build.gradle添加ndk配置

    android {
    defaultConfig {
        ndk {
            abiFilters 'armeabi-v7a', 'arm64-v8a' //or x86、x86_64
        }
    }
    }
    
  • 拷贝so文件到项目jniLibs文件夹

Luban 提供4个平台的so文件: armeabi-v7aarm64-v8ax86x86_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();

RELEASE NOTE

Here

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.