【问题标题】:what is the difference between compressing image and resizing image ? android压缩图像和调整图像大小有什么区别?安卓
【发布时间】:2015-07-01 08:41:42
【问题描述】:

在我的 android 应用程序中,我想将图像上传到服务器。服务器不接受大于2M的图片的问题。但用户可以选择大于2M的图片。

所以我想构建一个使图像小于 2M 的代码。

我有两种方法:

  1. 调整图像尺寸。如下:

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {

// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);

// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);

}

  1. 我也可以压缩图像

    image.compress(Bitmap.CompressFormat.PNG, 10, fos);

这两种方法有什么区别?

【问题讨论】:

  • 你为什么对我的问题投反对票?

标签: android image compression


【解决方案1】:

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

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

【讨论】:

  • 感谢您的回答。你能写一个将图像压缩到2M的代码吗?谢谢。
  • 感谢您的评论。但这不会压缩到特定的字节数。相反,它表明压缩不会改变图像的高度和宽度!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-30
  • 2018-02-25
  • 1970-01-01
  • 2018-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多