gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:48:41 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com