使用Play框架将生成的图像发送到浏览器

编程入门 行业动态 更新时间:2024-10-28 22:24:26
本文介绍了使用Play框架将生成的图像发送到浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用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框架将生成的图像发送到浏览器

本文发布于:2023-07-13 22:17:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1102469.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发送到   框架   图像   浏览器   Play

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!