Android圖片壓縮框架-Tiny 集成


為了簡化對圖片壓縮的調用,提供最簡潔與合理的api壓縮邏輯,對於壓縮為Bitmap根據屏幕分辨率動態適配最佳大小,對於壓縮為File優化底層libjpeg的壓縮,整個圖片壓縮過程全在壓縮線程池中異步壓縮,結束后分發回UI線程。

支持的壓縮類型

Tiny圖片壓縮框架支持的壓縮數據源類型:

1、Bytes
2、File
3、Bitmap
4、Stream
5、Resource
6、Uri(network、file、content)

Tiny支持單個數據源壓縮以及批量壓縮,支持的壓縮類型:

1、數據源—>壓縮為Bitmap
2、數據源—>壓縮為File
3、數據源—>壓縮為File並返回壓縮后的Bitmap
4、批量數據源—>批量壓縮為Bitmap
5、批量數據源—>批量壓縮為File
6、批量數據源—>批量壓縮為File並返回壓縮后Bitmap

壓縮參數

Tiny.BitmapCompressOptions

Bitmap壓縮參數可配置三個:

1、width
2、height
3、Bitmap.Config

如果不配置,Tiny內部會根據屏幕動態適配以及默認使用ARGB_8888

Tiny.FileCompressOptions

File壓縮參數可配置四個:

1、quality-壓縮質量,默認為76
2、isKeepSampling-是否保持原數據源圖片的寬高
3、fileSize-壓縮后文件大小
4、outfile-壓縮后文件存儲路徑

如果不配置,Tiny內部會根據默認壓縮質量進行壓縮,壓縮后文件默認存儲在:ExternalStorage/Android/data/${packageName}/tiny/目錄下

Tiny項目地址 https://github.com/tianyingzhong/Tiny

Tiny與微信朋友圈的壓縮率比較

下面是使用Tiny圖片壓縮庫進行壓縮的效果對比示例:

 

圖片信息 Tiny Wechat
6.66MB (3500x2156) 151KB (1280x788) 135KB (1280x789)
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)

 

壓縮為Bitmap

1
2
3
4
5
6
7
Tiny.BitmapCompressOptions options = new Tiny.BitmapCompressOptions();
Tiny.getInstance().source("").asBitmap().withOptions(options).compress(new BitmapCallback() {
@Override
public void callback(boolean isSuccess, Bitmap bitmap) {
//return the compressed bitmap object
}
});

壓縮為File

1
2
3
4
5
6
7
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
}
});

壓縮為File並返回Bitmap

1
2
3
4
5
6
7
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
}
});

批量壓縮為Bitmap

1
2
3
4
5
6
7
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
}
});

批量壓縮為File

1
2
3
4
5
6
7
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
}
});

批量壓縮為File並返回Bitmap

1
2
3
4
5
6
7
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
}
});

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM