an image compression framework.
Blog entry with introduction or Introduction of compression
ImageInfo | Tiny | |
---|---|---|
6.66MB (3500x2156) | 151KB (1280x788) | 135KB (1280x788) |
4.28MB (4160x3120) | 219KB (1280x960) | 195KB (1280x960) |
2.60MB (4032x3024) | 193KB (1280x960) | 173KB (1280x960) |
372KB (500x500) | 38.67KB (500x500) | 34.05KB (500x500) |
236KB (960x1280) | 127KB (960x1280) | 118KB (960x1280) |
Tiny
does not depend on any library , it keeps the code clean on architecture . Tiny
also uses an asynchronous thread pool to compress images , and will hand out the result in the main thread when compression is completed.
implementation 'com.zxy.android:tiny:1.1.0'
Tiny provide abi:armeabi
、armeabi-v7a
、arm64-v8a
、x86
.
Choose what you need "abi" version:
android {
defaultConfig {
ndk {
abiFilters 'armeabi','x86'//or armeabi-v7a、arm64-v8a、x86
}
}
}
Tiny.getInstance().init(this);
Tiny.BitmapCompressOptions options = new Tiny.BitmapCompressOptions();
//options.height = xxx;//some compression configuration.
Tiny.getInstance().source("").asBitmap().withOptions(options).compress(new BitmapCallback() {
@Override
public void callback(boolean isSuccess, Bitmap bitmap) {
//return the compressed bitmap object
}
});
Tiny.FileCompressOptions options = new Tiny.FileCompressOptions();
Tiny.getInstance().source("").asFile().withOptions(options).compress(new FileCallback() {
@Override
public void callback(boolean isSuccess, String outfile) {
//return the compressed file path
}
});
Tiny.FileCompressOptions options = new Tiny.FileCompressOptions();
Tiny.getInstance().source("").asFile().withOptions(options).compress(new FileWithBitmapCallback() {
@Override
public void callback(boolean isSuccess, Bitmap bitmap, String outfile) {
//return the compressed file path and bitmap object
}
});
Tiny.BitmapCompressOptions options = new Tiny.BitmapCompressOptions();
Tiny.getInstance().source("").batchAsBitmap().withOptions(options).batchCompress(new BitmapBatchCallback() {
@Override
public void callback(boolean isSuccess, Bitmap[] bitmaps) {
//return the batch compressed bitmap object
}
});
Tiny.FileCompressOptions options = new Tiny.FileCompressOptions();
Tiny.getInstance().source("").batchAsFile().withOptions(options).batchCompress(new FileBatchCallback() {
@Override
public void callback(boolean isSuccess, String[] outfile) {
//return the batch compressed file path
}
});
Tiny.FileCompressOptions options = new Tiny.FileCompressOptions();
Tiny.getInstance().source("").batchAsFile().withOptions(options).batchCompress(new FileWithBitmapBatchCallback() {
@Override
public void callback(boolean isSuccess, Bitmap[] bitmaps, String[] outfile) {
//return the batch compressed file path and bitmap object
}
});
/**
* @author : tyz
* @ClassName: ImageCompressUtil
* @Description:图片压缩工具类
* @time 2019/9/5 16:12
*/
public class ImageCompressUtil {
/**
* 初始化
*
* @param application 全局
* @param isDebug 是否调试
*/
public static void init(Application application, boolean isDebug) {
Tiny.getInstance().debug(isDebug).init(application);
}
/**
* 压缩
*
* @param resFile 图片路径
* @param outFile 输出路径
* @param callback 回调
*/
public static void compressAsFile(String resFile, String outFile, CompressCallback callback) {
try {
File file = new File(resFile);
compressAsFile(Bitmap.Config.ARGB_8888, file, outFile, callback);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 压缩
*
* @param config 图片属性
* @param resFile 图片文件
* @param outfile 输出路径
* @param callback 回调
*/
public static void compressAsFile(Bitmap.Config config, File resFile, String outfile, CompressCallback callback) {
try {
FileInputStream is = new FileInputStream(resFile);
long size = is.available();
if (size / 1024 <= 200) {
// 假如图片不足200K,不用压缩,直接上传
callback.callback(true, resFile.getAbsolutePath());
LogUtil.e("PicCompressUtil", "小于200K,使用原图");
return;
}
LogUtil.e("PicCompressUtil", "压缩图片:开始压缩");
Tiny.FileCompressOptions compressOptions = new Tiny.FileCompressOptions();
if (config == null) {
config = Bitmap.Config.ARGB_8888;
}
compressOptions.quality = 90;// 图片质量(0-100)
compressOptions.config = config;
compressOptions.outfile = outfile;// 输出图片路径
Tiny.getInstance().source(resFile).asFile().withOptions(compressOptions).compress((isSuccess, tinyFile, throwable) -> {
if (callback != null) {
if (isSuccess) {
callback.callback(true, tinyFile);
resFile.deleteOnExit();
} else {
callback.callback(false, resFile.getAbsolutePath());
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
public interface CompressCallback {
void callback(boolean isSuccess, String outfile);
}
}
- v0.0.1:The first version.
- v0.0.2:Optimize the compression strategy,and handle with the orientation of bitmap.
- v0.0.3:Unified as
libtiny.so
- v0.0.4:Add cover source file configuration—see
{@link FileCompressOptions#overrideSource}
, and setting up the batch compressed file paths—see{@link BatchFileCompressOptions#outfiles}
- v0.0.5:Fix google store reject.
- v0.0.6:Initialization is not must.Add clear compression directory method,see
{@link Tiny#clearCompressDirectory}
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ Copyright 2017 郑晓勇
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.