【问题标题】:Image compression while maintaining the quality图像压缩同时保持质量
【发布时间】:2016-03-28 13:39:40
【问题描述】:

我正在制作一个自定义相机,它可以拍摄文档照片并通过网络发送它们。然后对图像进行 OCRed。为了通过网络发送它们,它们必须是小尺寸(小于 100 Kb)。我首先缩小图像,然后转换为灰度并以 100% 压缩为 JPG。图像大小仍然超过 100Kb。如果我降低 JPG 压缩百分比,质量真的很差。如何解决这个问题。这是代码:

//图像缩放

 public static Bitmap decodeSampledBitmapFromResource(byte[] data,
                                                         int reqWidth, int reqHeight) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeByteArray(data, 0, data.length, options);
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
        options.inJustDecodeBounds = false;
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        return BitmapFactory.decodeByteArray(data, 0, data.length, options);
    }

//图像灰度。使用渲染脚本

uchar grayscale = pixelIn.r * 0.299 + pixelIn.g * 0.587 + pixelIn.b * 0.114;

uchar4 pixelOut;
pixelOut.a = pixelIn.a;
pixelOut.r = grayscale;
pixelOut.g = grayscale;
pixelOut.b = grayscale;

return pixelOut;

//JPG压缩

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);

【问题讨论】:

    标签: android android-camera android-image image-compression


    【解决方案1】:

    看看this的文章,相信你会得到你想要的。

    【讨论】:

      【解决方案2】:

      如果您改为使用 Bitmap.CompressFormat.PNG,您会得到什么?以及如何尝试缩小图像(根据文档,总是缩小 2 倍)

      JPEG 是全彩色的,所以最终转换为灰度没有帮助,GIF 是 8 位索引或灰度,所以即使这样也值得一试。

      【讨论】:

        【解决方案3】:

        保持质量。压缩图像。使用短像素。即使是 10MB/图像。

        【讨论】:

          【解决方案4】:

          以下三点有助于减小图片尺寸: 在保持纵横比的同时缩小图像:

          public static Bitmap decodeSampledBitmapFromResource(byte[] data,
                                                                       int reqWidth, int reqHeight) {
                      final BitmapFactory.Options options = new BitmapFactory.Options();
                      options.inJustDecodeBounds = true;
                      BitmapFactory.decodeByteArray(data, 0, data.length, options);
                      options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
                      options.inJustDecodeBounds = false;
                      options.inPreferredConfig = Bitmap.Config.RGB565;
                      return BitmapFactory.decodeByteArray(data, 0, data.length, options);
                  }
          

          移除 alpha 通道,使用以下配置:

          options.inPreferredConfig = Bitmap.Config.RGB565;
          

          使用 JPEG 压缩来压缩图像:

          bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
          

          100 表示没有压缩并且 0 表示 100% 压缩

          灰度缩放不会缩小尺寸。

          【讨论】:

            猜你喜欢
            • 2012-07-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-12-27
            • 1970-01-01
            相关资源
            最近更新 更多