1.咱們作android開發的時候常常會用到壓縮圖片的功能。java
壓縮圖片android自己提供的也有,可是android本身壓縮的圖片,很差用。質量壓縮不過關,清晰度也不夠。android
本人由於項目須要找到比較合適的2款圖片壓縮工具。git
Luban多圖片壓縮(還有但圖片壓縮的Luban代碼,我以爲沒這個多圖片壓縮的好。)github
https://github.com/shaohui10086/AdvancedLuban算法
直接點開鏈接能夠簡單查看到API。同時想用這個壓縮工具最好仍是用Android Studio(雖然很難用,主要是牆牆牆)。async
AdvancedLuban
—— Is a convenient simple Android
image compression tool library.Provides multiple compression strategies.Different calling methods,Custom compression,Multi-Image synchronous compression and so on,Focus on a better picture compression experienceide
Image Count | Origin Total size | Compressed Total size | Time Cost |
---|---|---|---|
1 | 4.3Mb | 85Kb | 0.23s |
4 | 14.22Mb | 364Kb | 1.38s |
9 | 36.23Mb | 745Kb | 4.43s |
Maven工具
<dependency> <groupId>me.shaohui.advancedluban</groupId> <artifactId>library</artifactId> <version>1.3.5</version> <type>pom</type> </dependency>
or Gradleui
compile 'me.shaohui.advancedluban:library:1.3.5'
Listener
modeAdvanced Luban
internalComputation
thread for image compression, external calls simply set the Listener can be:this
Luban.compress(context, file) .putGear(Luban.THIRD_GEAR) // set the compress mode, default is : THIRD_GEAR .launch(listener); // start compression and set the listener
RxJava
modeRxJava
call the same defaultComputation
thread to compress, you can also define any thread, can be observed in any thread:
Luban.compress(context, file) .putGear(Luban.CUSTOM_GEAR) .asObservable() // generate Observable .subscribe(successAction, errorAction) // subscribe the compress result
根據您設置的限制壓縮映像文件,能夠限制:圖像文件的寬度,高度或文件大小
Luban.compress(context, file) .setMaxSize(500) // limit the final image size(unit:Kb) .setMaxHeight(1920) // limit image height .setMaxWidth(1080) // limit image width .putGear(Luban.CUSTOM_GEAR) // use CUSTOM GEAR compression mode .asObservable()
使用自定義算法,根據圖片寬高比,圖片被快速壓縮,產生的圖像大小約爲100Kb,對於通常壓縮,沒有文件大小限制和圖片寬度限制
簡化版本的第三代GEAR,壓縮圖像分辨率小於1280 x 720,最終文件小於60Kb。 適合快速壓縮,不管最終的圖像質量如何
If you choose to call the way Listener
:
Luban.get(this) .putGear(Luban.CUSTOM_GEAR) .load(fileList) // load all images .launch(multiCompressListener); // passing an OnMultiCompress Listener
or the RxJava
way to use:
Luban.get(this) .putGear(Luban.CUSTOM_GEAR) .load(fileList) // load all images .asListObservable() // Generates Observable <List<File>. Returns the result of all the images compressed successfully
=================================================================
還有另外一個單圖片壓縮工具,主要是比較簡單。而後出來的圖片質量比較高。因此才推薦這款
https://github.com/zetbaitsu/Compressor
這個API比較簡單。
Compressor是一款輕巧而強大的Android圖像壓縮庫。 壓縮器將容許您將大型照片壓縮成較小尺寸的照片,圖像質量損失少或忽略不計。
dependencies { compile 'id.zelory:compressor:1.0.4' }
compressedImageFile = Compressor.getDefault(this).compressToFile(actualImageFile);
compressedImageBitmap = Compressor.getDefault(this).compressToBitmap(actualImageFile);
compressedImage = new Compressor.Builder(this) .setMaxWidth(640) .setMaxHeight(480) .setQuality(75) .setCompressFormat(Bitmap.CompressFormat.WEBP) .setDestinationDirectoryPath(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES).getAbsolutePath()) .build() .compressToFile(actualImage);
Compressor.getDefault(this) .compressToFileAsObservable(actualImage) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Action1<File>() { @Override public void call(File file) { compressedImage = file; } }, new Action1<Throwable>() { @Override public void call(Throwable throwable) { showError(throwable.getMessage()); } });