Android 在上传到服务器之前减小图像大小

标签 android image-processing bit-manipulation

我必须将从相机/画廊拍摄的图像上传到服务器。在许多应用程序中,我看到分辨率为 1000X560 且大小为 35 KB 的图像。而在我的例子中,图像大小高达 380 KB。我手机的相机拍摄分辨率为 2368X4224 且大小 < 2 MB 的图像。如何在保持较小尺寸的同时获得高分辨率图像?到目前为止,这是我尝试过的:

BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(realPath, bmOptions);
bmOptions.inSampleSize = 1;
bmOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
bmOptions.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(realPath, bmOptions);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

我读过这个documentation .我面临的问题是如何确定图像的最小宽度和最小高度。

最佳答案

嘿,你能查一下吗http://voidcanvas.com/whatsapp-like-image-compression-in-android/ 这个链接

我认为这是最好的图像压缩教程。
还要检查这个答案:https://stackoverflow.com/a/26928768/5275436
希望对您有所帮助。

编辑:这里是图像调整大小。

//      max Height and width values of the compressed image is taken as 816x612

    float maxHeight = 816.0f;
    float maxWidth = 612.0f;
    float imgRatio = actualWidth / actualHeight;
    float maxRatio = maxWidth / maxHeight;


 //      width and height values are set maintaining the aspect ratio of the image
        if (actualHeight > maxHeight || actualWidth > maxWidth) {
            if (imgRatio < maxRatio) {               imgRatio = maxHeight / actualHeight;                actualWidth = (int) (imgRatio * actualWidth);               actualHeight = (int) maxHeight;             } else if (imgRatio > maxRatio) {
                imgRatio = maxWidth / actualWidth;
                actualHeight = (int) (imgRatio * actualHeight);
                actualWidth = (int) maxWidth;
            } else {
                actualHeight = (int) maxHeight;
                actualWidth = (int) maxWidth;

            }
        }

关于Android 在上传到服务器之前减小图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37893420/

相关文章:

android - 设计相关布局

android - OnClickListener 不适用于 fragment

android - android中的自由手字符识别

python - OpenCV - 检测矩形或五边形

C++ 位串到字节

java - Android 无法解释的 LogCat 错误

java - 在 Grails 中调整图像大小

actionscript - 对 RGB 值使用逻辑位移

java - 如何在java中进行按位排列

android - 如何更改微调器的 TextSize 和背景颜色