Android壓縮工具

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

Import

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'

Usage

Listener mode

Advanced 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 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

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());
            }
        });
相關文章
相關標籤/搜索