将字节数组解码为Java中已压缩的位图
我按以下方式压缩位图
Bitmap bmpSig = getMyBitMap();
int size = bmpSig.getWidth() * bmpSig.getHeight();
ByteArrayOutputStream out = new ByteArrayOutputStream(size);
bmpSig.compress(Bitmap.CompressFormat.JPEG, 100, out);
byte[] bytSig = out.toByteArray();
,然后尝试在 Android ImageView 中显示字节数组中的图像。当我这样做时,我得到的图像是全黑图像。
ImageView myImg = (ImageView) findViewById(R.id.img_view);
myImg.setImageBitmap(BitmapFactory.decodeByteArray(bytSig, 0, bytSig.length));
我假设这是因为我在 BitmapFactory.decodeByteArray() 之前缺少一个步骤来反转 jpeg 压缩。或者我误解了压缩的工作原理?
I am compressing a bitmap in the following way
Bitmap bmpSig = getMyBitMap();
int size = bmpSig.getWidth() * bmpSig.getHeight();
ByteArrayOutputStream out = new ByteArrayOutputStream(size);
bmpSig.compress(Bitmap.CompressFormat.JPEG, 100, out);
byte[] bytSig = out.toByteArray();
I am then trying to display the image in an Android ImageView from the byte array. When I do this I get an image that is completely black image.
ImageView myImg = (ImageView) findViewById(R.id.img_view);
myImg.setImageBitmap(BitmapFactory.decodeByteArray(bytSig, 0, bytSig.length));
I'm assuming it's because I am missing a step before BitmapFactory.decodeByteArray() to reverse the jpeg compression. Or have I misunderstood how the compression works?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有意识到我的位图(来自 Canvas 对象)的背景是透明的。由于该位图只是白色背景上的黑色线条,因此黑色图像是由于压缩为 JPEG 而给图像提供了黑色背景。
我已经更改
为
并且它正在按预期工作。
I didn't realise that the background of my bitmap (from a Canvas object) was transparent. Since this bitmap is just black lines on a white background the black image is due to compressing to JPEG giving the image a black background.
I have changed
to
And it is working as expected.