Java BufferedImage 转 PNG 格式 Base64 字符串
我正在尝试将屏幕截图输出作为 Base64 编码字符串,但没有走得太远。到目前为止,我的代码使用 Base64 库( http://iharder.sourceforge.net/current/java/base64/ ):
Robot robot = new Robot();
Rectangle r = new Rectangle( Toolkit.getDefaultToolkit().getScreenSize() );
BufferedImage bi = robot.createScreenCapture(r);
ByteArrayOutputStream os = new ByteArrayOutputStream();
OutputStream b64 = new Base64.OutputStream(os);
ImageIO.write(bi, "png", os);
ByteArrayOutputStream out = new ByteArrayOutputStream();
out.writeTo(b64);
String result = out.toString("UTF-8");
每次运行此命令时,“结果”始终是一个空字符串,但我不明白为什么。有什么想法吗?
注意:我不想将 png 写入磁盘上的文件。
I'm trying to get a screenshot output as a base64 encoded string but not getting very far. The code I have so far uses a Base64 library ( http://iharder.sourceforge.net/current/java/base64/ ):
Robot robot = new Robot();
Rectangle r = new Rectangle( Toolkit.getDefaultToolkit().getScreenSize() );
BufferedImage bi = robot.createScreenCapture(r);
ByteArrayOutputStream os = new ByteArrayOutputStream();
OutputStream b64 = new Base64.OutputStream(os);
ImageIO.write(bi, "png", os);
ByteArrayOutputStream out = new ByteArrayOutputStream();
out.writeTo(b64);
String result = out.toString("UTF-8");
Each time I run this, "result" is always an empty string but I don't understand why. Any ideas?
Note: I don't want to have to write the png to a file on disk.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我遵循了 xehpuk's 的答案,但在某些浏览器中通过数据渲染时,某些图像的最后几行像素丢失了问题url(Chrome 和 Firefox、Safari 似乎可以很好地渲染它们)。我怀疑这是因为浏览器正在尽最大努力解释数据,但最后几个字节的数据丢失了,所以它显示了它可以做什么。
输出流的包装似乎是此问题的原因。
Base64.wrap(OutputStream os)
的文档解释道:因此,根据数据的长度,最后几个字节可能不会从流中刷新,因为没有调用
close()
。我的解决方案是不费心包装流,而直接对流进行编码:这解决了在浏览器中渲染时丢失像素行的问题。
I followed xehpuk's answer but had issues with certain images having the last few rows of pixels missing when rendered in certain browsers via a data url (Chrome and Firefox, Safari seemed to render them fine). I suspect this is because the browser is doing it's best to interpret the data but the last few bytes of data was missing so it shows what it can.
The wrapping of the output stream seems to be the cause of this problem. The documentation for
Base64.wrap(OutputStream os)
explains:So depending on the length of the data, it's possible the last few bytes are not flushed from the stream because
close()
isn't called on it. My solution to this was to not bother wrapping the stream and just encode the stream directly:This resolved the issues with the missing rows of pixels when rendered in a browser.
使用 Java 8 对图像进行 Base64 编码和解码:
将其用于屏幕截图场景:
Base64 encoding and decoding of images using Java 8:
Use it like this for your screenshot scenario:
以下语句的工作方向是错误的:
它用
out
的空字节数组覆盖 Base 64 数据。out
的目的到底是什么?我认为你不需要它。更新:
并且您将图像直接写入
os
,而不是通过 Base 64 编码器写入。以下代码应该可以工作:
The following statement works in the wrong direction:
It overwrites the Base 64 data with the empty byte array of
out
.What's the purpose of
out
anyway? I don't think you need it.Update:
And you write the image directly to
os
instead of writing through the Base 64 encoder.The following code should work:
这对我有用:
将图像编码为 Base64 字符串
将 Base64 字符串解码为图像
This works for me:
Encode Image to Base64 String
Decode Base64 String to Image
事实上,两种不同解决方案的结合对我来说很有效。我的用例是从 zip 文件中读取图像。这是对我有用的代码:
我通过将结果转换为实际图像来确认结果。效果奇妙。
Actually, the combination of two different solutions worked for me. My use case is to read images from a zip file. Here is the code that worked for me :
I Confirmed the result by converting the result to actual image. It works wonder.