我正在动态地创建一个BufferedImage,并尝试将其编码为Base64字符串,以便在模板中显示如下所示的图像:
<img src="data:image/gif;base64, [base 65 string]>我有一个BufferedImage变量,我试图将它写到磁盘上进行测试,然后成功地编写映像:
ImageIO.write(@img, "gif", Java::JavaIo::File.new(filename))如何从@img变量(不写入磁盘)获得字节字符串,以便将其编码到Base64中并显示在模板中?
发布于 2014-06-15 18:40:20
您可以用StringIO方法在org.jruby.util.IOOutputStream中包装一个to_outputstream,并将其写入,然后用StringIO#string获取字节
sio = StringIO.new
outputstream = sio.to_outputstream
ImageIO.write(@img, "gif", outputstream)
encoded = Base64.encode64(sio.string)https://stackoverflow.com/questions/24231335
复制相似问题