【问题标题】:How to reduce bitmap quality without affecting it's size如何在不影响其大小的情况下降低位图质量
【发布时间】:2016-04-18 12:41:29
【问题描述】:

有没有办法我可以在 android 中将 115kb 的图像压缩为 4kb 而不影响它的大小?。只是降低它的质量?

我只知道使用

  • BitmapFactory.Options 减小尺寸和质量

  • Bitmap.compress 不提供指定字节大小的选项。

public Bitmap compressImage(String imagePath) {

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
   bmp = BitmapFactory.decodeStream(new FileInputStream(imagePath),null, options);


  options.inSampleSize = calculateInSampleSize(options, actualWidth, actualHeight);
  options.inJustDecodeBounds = false;
    bmp = BitmapFactory.decodeStream(new FileInputStream(imagePath),null, options);

  return bmp;
}



public  int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
  final int height = options.outHeight;
  final int width = options.outWidth;
  int inSampleSize = 1;

  if (height > reqHeight || width > reqWidth) {
    final int heightRatio = Math.round((float) height / (float) reqHeight);
    final int widthRatio = Math.round((float) width / (float) reqWidth);
    inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
  }
  final float totalPixels = width * height;
  final float totalReqPixelsCap = reqWidth * reqHeight * 2;

  while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
    inSampleSize++;
  }

  return inSampleSize;
}

【问题讨论】:

  • 为什么需要这个?避免OOME?这无济于事! ... 解码 Bitmap 的文件是 4KB 还是 1MB 都没有关系 ... Bitmap 对象将占用相同数量的内存,仅取决于 Bitmap 宽度和高度以及像素格式
  • @Selvin 我不需要这个来避免 OOME。实际上我正在尝试实现一个图像共享应用程序,如whatsapp,用户将在下载之前看到图像的外观。因此,我正在考虑通过 googles GCM 向用户发送与原始尺寸相同但质量较低的图像,这将数据负载限制为 4kb :(
  • 没有其他方法可以像尝试使用不同的q 值的bitmap.compress(Bitmap.CompressFormat.JPG, q)...但是您可以尝试将文件拆分为4kb 的图片...或使用Google drive API(或任何其他可以存储)并仅发送链接
  • @Selvin 如果我使用这种方法,我无法判断图像字节超过 4kb。如果他们这样做并且我通过谷歌 GCM 发送他们,我会收到错误 MessageTooBig
  • 嗯?为什么...for(int q = 100; q &gt; 0; q--) {OutputStream out = getOutputFromFile(file);bitmap.compress(Bitmap.CompressFormat.JPG, q, out); if(checkFileSize(file) &lt; 4000) {sendFile(file); break;}} ...当然这不是最佳方式(它更像是蛮力)也可能需要很长时间...

标签: android bitmap bitmapfactory


【解决方案1】:

图像调整大小意味着您将缩短图像的分辨率。假设用户选择了一个 1000*1000 像素的图像。您要将图像转换为 300*300 的图像。因此图像尺寸会减小。

并且图像压缩在不影响分辨率的情况下降低了图像的文件大小。当然减小文件大小会影响图像的质量。有许多可用的压缩算法,可以在不影响图像质量的情况下减小文件大小。

我在这里找到的一个方便的方法是:

    Bitmap original = BitmapFactory.decodeStream(getAssets().open("1024x768.jpg"));
ByteArrayOutputStream out = new ByteArrayOutputStream();
original.compress(Bitmap.CompressFormat.PNG, 100, out);
Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));

Log.e("Original   dimensions", original.getWidth()+" "+original.getHeight());
Log.e("Compressed dimensions", decoded.getWidth()+" "+decoded.getHeight());

给予

12-07 17:43:36.333:E/原始尺寸(278):1024 768 12-07

17:43:36.333:E/压缩尺寸(278):1024 768

【讨论】:

  • 我想要一个与原始大小相同但质量低的压缩图像。我正在尝试实现像whatsapp这样的图像共享,如果我向您发送图像,您将在从服务器下载之前看到图像的外观。这意味着我必须向正在考虑使用 google 的 GCM 的用户发送低质量图像 它不允许超过 4kb 的数据加载
【解决方案2】:
public static int getSquareCropDimensionForBitmap(Bitmap bitmap)
{
    int dimension;
    //If the bitmap is wider than it is tall
    //use the height as the square crop dimension
    if (bitmap.getWidth() >= bitmap.getHeight())
    {
        dimension = bitmap.getHeight();
    }
    //If the bitmap is taller than it is wide
    //use the width as the square crop dimension
    else
    {
        dimension = bitmap.getWidth();
    }

return  dimension;
}

int 维度 = getSquareCropDimensionForBitmap(bitmap);

        System.out.println("before cropped height " + bitmap.getHeight() + "and width: " + bitmap.getWidth());
        Bitmap croppedBitmap = ThumbnailUtils.extractThumbnail(bitmap, dimension, dimension);
        System.out.println("after cropped height "+croppedBitmap.getHeight() +"and width: " + croppedBitmap.getWidth());

它可以裁剪并且可以减小尺寸你可以指定你自己的尺寸

【讨论】:

  • 我会试一试并给你回应
  • 就我而言,上述解决方案能够降低质量,但 字节数仍然很大 所以我最终使用了bitmap.compress(Bitmap.CompressFormat.JPEG,10,outputStream),这对我来说没问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-09
  • 2017-02-28
  • 1970-01-01
  • 1970-01-01
  • 2013-05-04
  • 1970-01-01
相关资源
最近更新 更多