Can bufferedimage be converted into img data URI in Java?
•
Java
I created a graphic image with the following example code
BufferedImage bi = new BufferedImage(50,50,BufferedImage.TYPE_BYTE_BINARY); Graphics2D g2d = bi.createGraphics(); // Draw graphics. g2d.dispose(); // BufferedImage Now has my image I want.
At this time, I have bufferedimage, and I want to convert it to img data URI Is that possible? For example
<IMG SRC="data:image/png;base64,[BufferedImage%20data%20here]"/>
Solution
There is no test, but such things should be done:
ByteArrayOutputStream out = new ByteArrayOutputStream(); ImageIO.write(bi,"PNG",out); byte[] bytes = out.toByteArray(); String base64bytes = Base64.encode(bytes); String src = "data:image/png;base64," + base64bytes;
Lots of different Base64 codec implementations for Java I achieved good results in migbase 64
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码