英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论