在 Java 中是否可以将 BufferedImage 转换为 IMG 数据 URI?
我使用以下示例代码创建了一个图形图像。
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.
此时我有 BufferedImage,我想将其转换为 IMG 数据 URI。这可能吗?例如..
<IMG SRC="data:image/png;base64,[BufferedImage data here]"/>
I have created a graphical image with the following sample 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 point I have BufferedImage which I want to convert into an IMG Data URI. Is this possible? For example..
<IMG SRC="data:image/png;base64,[BufferedImage data here]"/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尚未测试,但类似的东西应该可以做到:
Java 有很多不同的 base64 编解码器实现。我使用 MigBase64 取得了很好的结果。
Not tested, but something like this ought to do it:
There are lots of different base64 codec implementations for Java. I've had good results with MigBase64.
您可以使用此解决方案,它不使用任何外部库。又短又干净!它使用 Java 6 库 (
DatatypeConverter
)。为我工作!You could use this solution which doesn't use any external libraries. Short and clean! It uses a Java 6 library (
DatatypeConverter
). Worked for me!我使用 Webdriver,获取验证码,如下所示:
I use Webdriver, get captcha, like this below: