我正在尝试使用Play输出生成的图像。我不确定我的问题是否是Play特定的。我试图做这个PHP代码做同样的事情:
I'm trying to output a generated image using Play. I'm not sure if my issue is Play-specific or not. I'm trying to do the same thing this PHP code does:
header("Content-type: Image/png"); $map = imagecreatefrompng("$_SESSION[ROOT]/it/cabling/maps/${building}_$floor.png"); ... // add annotations imagepng($map);看起来我需要使用 renderBinary ,但我不知道如何从 BufferedImage 到 InputStream 那个 renderBinary 想要作为其参数。
It looks like I need to use renderBinary, but I'm not sure how to get from a BufferedImage to the InputStream that renderBinary wants as its argument.
Application.map action:
public static void map(String building_code, String ts_code) throws IOException { BufferedImage image = ImageIO.read(new File("public/images/maps/" + building_code + "_" + ts_code.charAt(0))); ... // Overlay some additional information on the image // do some sort of conversion renderBinary(inputStream); }推荐答案
我在导致此解决方案的 Images.Captcha 的源代码:
I found an example in the source code for Images.Captcha which led to this solution:
public static void map(String building_code, String ts_code) throws IOException { BufferedImage image = ImageIO.read(new File("public/images/maps/" + building_code + "_" + ts_code.charAt(0) + ".png")); ... // add annotations ImageInputStream is = ImageIO.createImageInputStream(image); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(image, "png", baos); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); Response.current().contentType = "image/png"; renderBinary(bais); }使用< img id =引用在视图模板中映射src =@ {Application.map(ts.building.code,ts.code)}width =100%> 。
由于某种原因,它甚至没有指定内容类型也能正常工作,但我不确定如何。 Images.Captcha 中的代码已经保留了它,所以我保留了它,至少在我找到它没有它的原因之前。
For some reason it works even without specifying the content type but I'm not sure how. The code in Images.Captcha had it so I kept it, at least until I find out why it works without it.
更多推荐
使用Play框架将生成的图像发送到浏览器
发布评论