如何压缩图像

huangapple go评论64阅读模式
英文:

How to Compress an image

问题

所以我在本地存储中有一张图片。这张图片是使用应用程序内的相机拍摄的。但问题是图片的大小约为3-4 MB。在将其保存到本地存储之前,是否有方法压缩此图片?目前我正在使用CameraX来拍摄照片。谢谢!

英文:

So I have an image saved in local storage. This image was taken using camera from the app itself. But the problem is the size of the image is about 3-4 MB. Is there anyway to compress this image before saving it to my local storage? Currently I am using CameraX to capture the picture. Thanks!

答案1

得分: 1

我一直在使用Tiny库,它工作得很好。它易于使用,您可以添加配置来压缩图像。

1.- 添加依赖项

implementation 'com.zxy.android:tiny:1.1.0';

2.- 初始化

Tiny.getInstance().init(this);

3.- 压缩

  • 如果您想添加一些配置,请创建选项

    Tiny.BitmapCompressOptions options = new Tiny.BitmapCompressOptions();
    
  • 异步

    Tiny.getInstance().source("").asBitmap().withOptions(options).compress(new BitmapCallback() {
          @Override
          public void callback(boolean isSuccess, Bitmap bitmap, Throwable t) {
              // 返回压缩后的位图对象
          }
      });
    
  • 同步

    BitmapResult result = Tiny.getInstance().source("").asBitmap().withOptions(options).compressSync();
    

获取更多详情 -> https://github.com/Sunzxyong/Tiny

英文:

I have been using Tiny library and it works fine. It is easy to use and you can add configuration for compress image.

1.- Add dependency

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

2.- Initialization

Tiny.getInstance().init(this);

3.- Compress

  • Create options if you want to add some configuration

    Tiny.BitmapCompressOptions options = new Tiny.BitmapCompressOptions();
    
  • Async

    Tiny.getInstance().source("").asBitmap().withOptions(options).compress(new BitmapCallback() {
          @Override
          public void callback(boolean isSuccess, Bitmap bitmap, Throwable t) {
              //return the compressed bitmap object
          }
      });
    
  • Sync

    BitmapResult result = Tiny.getInstance().source("").asBitmap().withOptions(options).compressSync();
    

For more details -> https://github.com/Sunzxyong/Tiny

huangapple
  • 本文由 发表于 2020年10月14日 11:29:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/64346204.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定