当前位置: 移动技术网 > IT编程>移动开发>Android > Android Tiny集成图片压缩框架的使用

Android Tiny集成图片压缩框架的使用

2019年07月24日  | 移动技术网IT编程  | 我要评论

为了简化对图片压缩的调用,提供最简洁与合理的api压缩逻辑,对于压缩为bitmap根据屏幕分辨率动态适配最佳大小,对于压缩为file优化底层libjpeg的压缩,整个图片压缩过程全在压缩线程池中异步压缩,结束后分发回ui线程。

支持的压缩类型

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

1、bytes
2、file
3、bitmap
4、stream
5、resource
6、uri(network、file、content)

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

1、数据源—>压缩为bitmap
2、数据源—>压缩为file
3、数据源—>压缩为file并返回压缩后的bitmap
4、批量数据源—>批量压缩为bitmap
5、批量数据源—>批量压缩为file
6、批量数据源—>批量压缩为file并返回压缩后bitmap

压缩参数

tiny.bitmapcompressoptions

bitmap压缩参数可配置三个:

1、width
2、height
3、bitmap.config

如果不配置,tiny内部会根据屏幕动态适配以及默认使用argb_8888

tiny.filecompressoptions

file压缩参数可配置四个:

1、quality-压缩质量,默认为76
2、iskeepsampling-是否保持原数据源图片的宽高
3、filesize-压缩后文件大小
4、outfile-压缩后文件存储路径

如果不配置,tiny内部会根据默认压缩质量进行压缩,压缩后文件默认存储在:externalstorage/android/data/${packagename}/tiny/目录下

tiny项目地址: https://github.com/tianyingzhong/tiny

tiny与微信朋友圈的压缩率比较

下面是使用tiny图片压缩库进行压缩的效果对比示例:

图片信息 tiny wechat
6.66mb (3500x2156) 151kb (1280x788) 135kb (1280x789)
4.28mb (4160x3120) 219kb (1280x960) 195kb (1280x960)
2.60mb (4032x3024) 193kb (1280x960)) 173kb (1280x960)
372kb (500x500) 38.67kb (500x500) 34.05kb (500x500)
236kb (960x1280) 127kb (960x1280) 118kb (960x1280)

压缩为bitmap

tiny.bitmapcompressoptions options = new tiny.bitmapcompressoptions();
tiny.getinstance().source("").asbitmap().withoptions(options).compress(new bitmapcallback() {
  @override
  public void callback(boolean issuccess, bitmap bitmap) {
    //return the compressed bitmap object
  }
});

压缩为file 

 tiny.filecompressoptions options = new tiny.filecompressoptions();
tiny.getinstance().source("").asfile().withoptions(options).compress(new filecallback() {
  @override
  public void callback(boolean issuccess, string outfile) {
    //return the compressed file path
  }
});

压缩为file并返回bitmap

 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) {
    //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) {
    //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) {
    //return the batch compressed file path
  }
});

批量压缩为file并返回bitmap

 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) {
    //return the batch compressed file path and bitmap object
  }
});

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网