其他分享
首页 > 其他分享> > Android在上传到服务器之前减小图像大小

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

作者:互联网

我必须将从相机/画廊捕获的图像上传到服务器.在许多应用程序中,我看到分辨率为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;

            }
        }

标签:bit-manipulation,image-processing,android
来源: https://codeday.me/bug/20191118/2028746.html