简体   繁体   English

如何压缩图像?

[英]How to compress an image?

I am working on a project in android studio where I am uploading images. 我正在上载图像的android studio项目。 Some of the images are very large in size and I want to compress before upload them in the app. 有些图片尺寸很大,我想先压缩一下再上传到应用程序中。 Can anyone please suggest me soemthing? 有人可以建议我做点什么吗?

You can use this code to compress images 您可以使用此代码压缩图像

Bitmap imageBitmap;
int newHeight  = 20 // define your height here
int newWidth = 30 //define new height 
Bitmap resizedBitmap = Bitmap.createScaledBitmap(imageBitmap,newWidth, 
               newHeight, 
                         true);

https://developer.android.com/reference/android/graphics/Bitmap.html see documentation for details. https://developer.android.com/reference/android/graphics/Bitmap.html有关详细信息, 参见文档。

使用此库压缩任何文件: https : //github.com/zetbaitsu/Compressor

try this function 试试这个功能

 public static Bitmap scaleDown(Bitmap realImage, float maxImageSize, boolean filter) {

    float ratio = Math.min(
            maxImageSize / realImage.getWidth(),
            maxImageSize / realImage.getHeight());
    int width = Math.round(ratio * realImage.getWidth());
    int height = Math.round(ratio * realImage.getHeight());

    Bitmap newBitmap = Bitmap.createScaledBitmap(realImage, width,
            height, filter);
    return newBitmap;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM