如何压缩图像?

时间:2019-08-27 16:15:06

标签: android

我正在上载图像的android studio项目。有些图片尺寸很大,我想先压缩一下再上传到应用程序中。有人可以建议我吃点东西吗?

3 个答案:

答案 0 :(得分:0)

您可以使用此代码压缩图像

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有关详细信息,请参见文档。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

尝试此功能

 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;
}