我需要在拍照后压缩图像尺寸。我想将尺寸减小到最大400K。
因此,拍摄照片后的平均图像尺寸约为3.3MB。我需要将它压缩到400K。
最佳选择是什么?
我试过了:
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()));
。 下面的代码允许我通过宽度和高度来减小尺寸,但不是存储空间。
Bitmap bitmap = Bitmap.createScaledBitmap(capturedImage, width, height, true);
我从https://stackoverflow.com/a/823966/556337找到了此示例,但他没有解释如何制作尺寸为XXX.MB的图像。有一种方法可以实现我的问题。 ?
// Decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f) {
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
// The new size we want to scale to
final int REQUIRED_SIZE=70;
// Find the correct scale value. It should be the power of 2.
int scale = 1;
while(o.outWidth / scale / 2 >= REQUIRED_SIZE &&
o.outHeight / scale / 2 >= REQUIRED_SIZE) {
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}