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()); } });