3

Playを使用して生成された画像を出力しようとしています。私の問題がPlay固有のものであるかどうかはわかりません。私はこのPHPコードと同じことをしようとしています:

header("Content-type: Image/png");
$map = imagecreatefrompng("$_SESSION[ROOT]/it/cabling/maps/${building}_$floor.png");
... // add annotations
imagepng($map);

を使用する必要があるようですが、引数として必要なaからにrenderBinary到達する方法がわかりません。BufferedImageInputStreamrenderBinary

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);
}
4

2 に答える 2

3

多くの renderBinary メソッドがあり、そのうちの 1 つは単純に 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)));
}
于 2010-11-08T21:38:06.303 に答える
3

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-09T04:20:44.713 に答える