Android图片压缩框架-Tiny









zhengxiaoyong

  <header class="post-header">



      <h1 class="post-title" itemprop="name headline">



            Android图片压缩框架-Tiny


      </h1>


    <div class="post-meta">
      <span class="post-time">

          <span class="post-meta-item-icon">
            <i class="fa fa-calendar-o"></i>
          </span>

            <span class="post-meta-item-text">发表于</span>

          <time title="Дата создания записи" itemprop="dateCreated datePublished" datetime="2017-04-23T16:23:56+08:00">
            2017-04-23
          </time>





      </span>


        <span class="post-category">

          <span class="post-meta-divider">|</span>

          <span class="post-meta-item-icon">
            <i class="fa fa-folder-o"></i>
          </span>

            <span class="post-meta-item-text">分类于</span>


            <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
              <a href="/categories/开源框架/" itemprop="url" rel="index">
                <span itemprop="name">开源框架</span>
              </a>
            </span>




        </span>




          <span class="post-comments-count">
            <span class="post-meta-divider">|</span>
            <span class="post-meta-item-icon">
              <i class="fa fa-comment-o"></i>
            </span>
            <a href="/2017/04/23/Android图片压缩框架-Tiny/#comments" itemprop="discussionUrl">
              <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2017/04/23/Android图片压缩框架-Tiny/" itemprop="commentCount"></span>
            </a>
          </span>





         <span id="/2017/04/23/Android图片压缩框架-Tiny/" class="leancloud_visitors" data-flag-title="Android图片压缩框架-Tiny">
           <span class="post-meta-divider">|</span>
           <span class="post-meta-item-icon">
             <i class="fa fa-eye"></i>
           </span>

             <span class="post-meta-item-text">阅读次数 </span>

             <span class="leancloud-visitors-count">7761</span>
         </span>








    </div>
  </header>



<div class="post-body" itemprop="articleBody">





    <h2 id="目的"><a href="#目的" class="headerlink" title="目的"></a><strong>目的</strong></h2><p>为了简化对图片压缩的调用,提供最简洁与合理的api压缩逻辑,对于压缩为Bitmap根据屏幕分辨率动态适配最佳大小,对于压缩为File优化底层<code>libjpeg</code>的压缩,整个图片压缩过程全在压缩线程池中异步压缩,结束后分发回UI线程。</p>

支持的压缩类型

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/目录下

接入

见项目README

压缩为Bitmap

1
2
3
4
5
6
7
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

1
2
3
4
5
6
7
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

1
2
3
4
5
6
7
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

1
2
3
4
5
6
7
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

1
2
3
4
5
6
7
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

1
2
3
4
5
6
7
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
}
});

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

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

图片信息TinyWechat
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)

Tiny项目地址

Tiny

也谈图片压缩

</div>

<div>




</div>

<div>


坚持原创技术分享,您的支持将鼓励我继续创作!

转载至:http://zhengxiaoyong.me/2017/04/23/Android%E5%9B%BE%E7%89%87%E5%8E%8B%E7%BC%A9%E6%A1%86%E6%9E%B6-Tiny/


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值