Roe*_*oee 3 java compression android bitmap image-compression
我正在尝试压缩用相机拍摄的照片而不缩放图像尺寸。
我尝试了 bitmap.compress,但没有成功。只是为了确保我尝试压缩从资源加载的位图,但仍然没有成功。
有任何想法吗?
示例代码:
//load bitmap from resources:
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.trolltunga);
Log.d("roee", "onImageCaptured: before compress = " + bitmap.getByteCount() + ", " + bitmap.getWidth() + ", " + bitmap.getHeight());
//compressing
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, bos);
Bitmap comp = BitmapFactory.decodeStream(new ByteArrayInputStream(bos.toByteArray()));
Log.d("roee", "onImageCaptured: after compress = " + comp.getByteCount() + ", " + comp.getWidth() + ", " + comp.getHeight());
Run Code Online (Sandbox Code Playgroud)
记录结果:
D/roee: onImageCaptured: before compress = 10800000, 3000, 900
D/roee: onImageCaptured: after compress = 10800000, 3000, 900
Run Code Online (Sandbox Code Playgroud)
简短版本:压缩位图并随后立即解压缩
长版:
ABitmap
未被压缩。它是字节数组的容器类,以全尺寸表示图像,以加快绘制和处理速度。
它通常用于将另一种格式的图片(例如 JPEG)加载到内存中进行显示或编辑,并且有多种方法可以直接加载或保存为其他文件格式。
其中之一是compress
将未压缩的字节数组转换为压缩文件格式(在您的情况下为 JPEG),然后将其输出到流,以便您可以将其保存到存储或其他位置。
当您使用BitmapFactory.decodeStream
它时,它会读取流中的信息,识别文件格式并将其解码为未压缩的字节数组。通常您只需使用它来加载图像形式的存储。
您在这里所做的是将Bitmap
JPEG 压缩为 JPEG(丢失一些信息),然后再次将该 JPEG 解码为未压缩的位图,该位图具有相同的分辨率,因此在内存中具有相同的字节数和相同的大小。
解决方案:
如果您想查看压缩是否有效,只需使用以下命令测量输出流大小即可bos.toByteArray().length
归档时间: |
|
查看次数: |
4166 次 |
最近记录: |