当前位置: 动力学知识库 > 问答 > 编程问答 >

java - How to convert BufferedImage to bytes correctly

问题描述:

I use this code to convert it.

ByteArrayOutputStream simpan = new ByteArrayOutputStream();

ImageIO.write((BufferedImage)image, "JPG", simpan);

But when I try to show the simpan.toString, the result was this.

When I convert it to PNG like via

ImageIO.write((BufferedImage)image, "PNG", simpan);

I got this.

Is this condition correct? And when I send those bytes to an android phone via socket, no image appeared on the phone.

网友答案:

You need to write the content of the ByteArrayOutputStream to a file with the proper extension, then open it with your file explorer. What you're doing right now is outputing the bytes of the JPEG or PNG images in a text console.

分享给朋友:
您可能感兴趣的文章:
随机阅读: