Android压缩工具

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 CountOrigin Total sizeCompressed Total sizeTime Cost
14.3Mb85Kb0.23s
414.22Mb364Kb1.38s
936.23Mb745Kb4.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

Android Arsenal

ss.png

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

 

转载于:https://my.oschina.net/u/1399599/blog/873587

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值