图片压缩框架--Tiny

最近项目中用到了图片压缩的知识点。在此记录一下。
Tiny优势:
Tiny在异步线程池中压缩图片,并在压缩完成后将结果返回到主线程

一、支持的压缩类型

图片压缩框架支持的压缩数据源类型:

  • Bytes
  • File
  • Bitmap
  • Stream
  • Resource
  • Uri(network、file、content)

单个数据源压缩以及批量压缩,支持的压缩类型:

  • 数据源—>压缩为Bitmap
  • 数据源—>压缩为File
  • 数据源—>压缩为File并返回压缩后的Bitmap
  • 批量数据源—>批量压缩为Bitmap
  • 批量数据源—>批量压缩为File
  • 批量数据源—>批量压缩为File并返回压缩后Bitmap

二、压缩参数

Tiny.BitmapCompressOptions
Bitmap压缩参数可配置三个:

  • width
  • height
  • Bitmap.Config

如果不配置,Tiny内部会根据屏幕动态适配以及默认使用 ARGB_8888
Tiny.FileCompressOptions
File压缩参数可配置四个:

  • quality-压缩质量,默认为76
  • isKeepSampling-是否保持原数据源图片的宽高
  • fileSize-压缩后文件大小
  • outfile-压缩后文件存储路径
如果不配置,Tiny内部会根据默认压缩质量进行压缩,压缩后文件默认存储在:ExternalStorage/Android/data/${packageName}/tiny/目录下 //String path = getExternalFilesDir(null).getParent() + File.separator + "tiny";

使用:

依赖:

implementation 'com.zxy.android:tiny:0.1.0'

配置需要的abi

android {
    
    
    defaultConfig {
    
    
    ...
        ndk {
    
    
            abiFilters 'armeabi','x86'//or armeabi-v7a、arm64-v8a、x86
        }
  
   ...
    }
}

在application中初始化:

Tiny.getInstance().init(this);

使用方法:

转成bitmap

Tiny.BitmapCompressOptions options = new Tiny.BitmapCompressOptions();
        //options.height = xxx;//some compression configuration.
        Tiny.getInstance().source("").asBitmap().withOptions(options).compress(new BitmapCallback() {
    
    
            @Override
            public void callback(boolean isSuccess, Bitmap bitmap, Throwable t) {
    
    
               
            }
        });

转成File路径

Tiny.FileCompressOptions options = new Tiny.FileCompressOptions();
        Tiny.getInstance().source("").asFile().withOptions(options).compress(new FileCallback() {
    
    
            @Override
            public void callback(boolean isSuccess, String outfile, Throwable t) {
    
    
                
            }
        });

转成bitmap和File路径

      Tiny.FileCompressOptions options = new Tiny.FileCompressOptions();
        Tiny.getInstance().source("").asFile().withOptions(options).compress(new FileWithBitmapCallback() {
    
    
            @Override
            public void callback(boolean isSuccess, Bitmap bitmap, String outfile, Throwable t) {
    
    
                //return the compressed file path and bitmap object
            }
        });

批量转成bitmap

  Tiny.BitmapCompressOptions options = new Tiny.BitmapCompressOptions();
        Tiny.getInstance().source("").batchAsBitmap().withOptions(options).batchCompress(new BitmapBatchCallback() {
    
    
            @Override
            public void callback(boolean isSuccess, Bitmap[] bitmaps, Throwable t) {
    
    
                //return the batch compressed bitmap object
            }
        });

批量转成file路径

Tiny.FileCompressOptions options = new Tiny.FileCompressOptions();
        Tiny.getInstance().source("").batchAsFile().withOptions(options).batchCompress(new FileBatchCallback() {
    
    
            @Override
            public void callback(boolean isSuccess, String[] outfile, Throwable t) {
    
    
                
            }
        });

批量转成bitmap和file路径

    Tiny.FileCompressOptions options = new Tiny.FileCompressOptions();
        Tiny.getInstance().source("").batchAsFile().withOptions(options).batchCompress(new FileWithBitmapBatchCallback() {
    
    
            @Override
            public void callback(boolean isSuccess, Bitmap[] bitmaps, String[] outfile, Throwable t) {
    
    
                //return the batch compressed file path and bitmap object
            }
        });

最后注意添加权限:
Manifest.permission.WRITE_EXTERNAL_STORAGE
Manifest.permission.READ_EXTERNAL_STORAGE

Guess you like

Origin blog.csdn.net/lixinxiaos/article/details/115181731