我正在尝试使用Play输出生成的图像。我不确定我的问题是不是特定于游戏。我正在尝试做这段PHP代码做的相同的事情:
header("Content-type: Image/png");
$map = imagecreatefrompng("$_SESSION[ROOT]/it/cabling/maps/${building}_$floor.png");
... // add annotations
imagepng($map);
看起来我需要使用renderBinary
,但是我不确定如何从BufferedImage
转换到renderBinary
想要作为其参数的InputStream
。
Application.map
操作:
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);
}
发布于 2010-11-09 12:20:45
我在Images.Captcha
的源代码中找到了一个示例,它导致了这个解决方案:
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="map" src="@{Application.map(ts.building.code, ts.code)}" width="100%">
引用。
由于某些原因,即使没有指定内容类型,它也可以工作,但我不确定如何工作。Images.Captcha
中的代码有它,所以我保留了它,至少在我找出为什么没有它的情况下它可以工作之前。
发布于 2010-11-09 05:38:06
有许多renderBinary方法,其中一个简单地接受文件作为参数。请参阅http://www.playframework.org/documentation/api/1.1/play/mvc/Controller.html#renderBinary(java.io.File
因此,您的代码需要像下面这样简单
public static void map(String building_code, String ts_code) throws IOException {
renderBinary(new File("public/images/maps/" + building_code + "_" + ts_code.charAt(0)));
}
https://stackoverflow.com/questions/4127876
复制相似问题