我正在尝试使用以下内容查看各种压缩质量的结果:
private static Bitmap codec(Bitmap src, Bitmap.CompressFormat format,
int quality) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
src.compress(format, quality, os);
byte[] array = os.toByteArray();
return BitmapFactory.decodeByteArray(array, 0, array.length);
}
我有一个名为 bitmap
的位图,我将其与压缩版本进行比较:
Bitmap compressed = codec(bitmap,Bitmap.CompressFormat.JPEG ,10);
Log.d("result","bitmap=" + bitmap.getByteCount() + " compressed=" + compressed.getByteCount());
无论我选择加载到位图
中的照片,压缩版本的字节数都与位图的字节数相同 - 但是,如果我将压缩
加载到ImageView
,质量明显降低。
尺寸真的保持不变,同时降低了图像的视觉质量吗?我是否错误地获取了文件的大小?
编辑:
更奇怪的是,图库详细信息中显示 1.04mb
的图像的结果大小显示为 16343040
字节。
我通过 onActivityResult
获取原始位图
,使用:
InputStream is = getContentResolver().openInputStream(selectedImageUri);
bitmap = BitmapFactory.decodeStream(is);
is.close();
其中 selectedImageUri
是来自 getData()
或从设备存储中选择的文件。
最佳答案
由 Android Developer reference对于 Bitmap
,getByteCount()
返回可用于表示图像中像素的最小字节数,即最大压缩大小,即使对于未压缩的图像也是如此!您应该使用 getAllocationByteCount()
来代替,因为它返回 Bitmap
实际使用的字节数。
关于java - 压缩后位图字节数保持不变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32400631/