我正在尝试使用 Play 输出生成的图像。我不确定我的问题是否是特定于 Play 的。我正在尝试做与此 PHP 代码相同的事情:
header("Content-type: Image/png");
$map = imagecreatefrompng("$_SESSION[ROOT]/it/cabling/maps/${building}_$floor.png");
... // add annotations
imagepng($map);
看起来我需要使用 renderBinary
,但我不确定如何从 BufferedImage
到 InputStream
renderBinary
想要作为它的参数。
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);
}
最佳答案
有许多 renderBinary 方法,其中一个只是将 File 作为参数。 参见 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)));
}
关于java - 使用 Play 框架将生成的图像发送到浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4127876/