【问题标题】:Android reducing image size before uploading to serverAndroid在上传到服务器之前减小图像大小
【发布时间】:2016-07-03 13:18:41
【问题描述】:

我必须将从相机/图库中捕获的图像上传到服务器。在许多应用程序中,我看到分辨率为 1000X560 的图像大小为 35 KB。而在我的情况下,图像大小高达 380 KB。我手机的摄像头可拍摄分辨率为 2368X4224 且大小

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。我面临的问题是如何确定图像的最小宽度和最小高度。

【问题讨论】:

  • 通过应用 scaleFactor 缩小图像
  • @MD 我没明白你的意思。能详细点吗?

标签: android image-processing bit-manipulation


【解决方案1】:

你可以查看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;

            }
        }

【讨论】:

  • 我已经阅读了这个教程。他们使用的最大宽度和高度为 816 和 612。这就是我没有得到的。我应该使用硬编码值还是应该做一些计算或者这些值是由哪些因素决定的?实际上,我将在电子商务应用程序中使用它
  • 好的,但这取决于您的图像大小要求。还有我的电子商务应用程序:决定最大 w 和 h,如果图像大于这个大小,那么你可以调整大小。请检查已编辑的答案(调整大小功能)。
  • 试过了,但分辨率降到了 457X816,文件大小仍然是 276 KB。我必须在保持文件大小较小的同时保持高分辨率。
  • 尽量减少 compress 80 到 60 即 scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 60, out);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-10
  • 2021-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-17
  • 2017-02-26
相关资源
最近更新 更多