1.我们做android开发的时候经常会用到压缩图片的功能。
压缩图片android本身提供的也有,但是android自己压缩的图片,不好用。质量压缩不过关,清晰度也不够。
本人因为项目需要找到比较合适的2款图片压缩工具。
Luban多图片压缩(还有但图片压缩的Luban代码,我觉得没这个多图片压缩的好。)
https://github.com/shaohui10086/AdvancedLuban
直接点开连接可以简单查看到API。同时想用这个压缩工具最好还是用Android Studio(虽然很难用,主要是墙墙墙)。
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 experience
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 |
Import
Maven
<dependency>
<groupId>me.shaohui.advancedluban</groupId>
<artifactId>library</artifactId>
<version>1.3.5</version>
<type>pom</type>
</dependency>
or Gradle
compile 'me.shaohui.advancedluban:library:1.3.5'
Usage
Listener
mode
Advanced Luban
internalComputation
thread for image compression, external calls simply set the Listener can be:
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
mode
RxJava
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
Compression mode
1. CUSTOM_GEAR[压缩方式]
根据您设置的限制压缩映像文件,可以限制:图像文件的宽度,高度或文件大小
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()
2. THIRD_GEAR
使用自定义算法,根据图片宽高比,图片被快速压缩,产生的图像大小约为100Kb,对于一般压缩,没有文件大小限制和图片宽度限制
3. FIRST_GEAR
简化版本的第三代GEAR,压缩图像分辨率小于1280 x 720,最终文件小于60Kb。 适合快速压缩,无论最终的图像质量如何
Multi-Image synchronous compression[多文件压缩]
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
Compressor是一款轻巧而强大的Android图像压缩库。 压缩器将允许您将大型照片压缩成较小尺寸的照片,图像质量损失少或忽略不计。
dependencies { compile 'id.zelory:compressor:1.0.4' }
开始图片压缩!
Compress Image File[把图片压缩成File文件]
compressedImageFile = Compressor.getDefault(this).compressToFile(actualImageFile);
Compress Image File to Bitmap[把图片压缩成Bitmap]
compressedImageBitmap = Compressor.getDefault(this).compressToBitmap(actualImageFile);
I want custom Compressor![自定义比例压缩]
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);
Stay cool compress image asynchronously with RxJava![Rxjava压缩]
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()); } });