Question

Je suis en train de produire une image générée en utilisant le jeu. Je ne sais pas si ma question est Play-spécifique ou non. Je suis en train de faire la même chose ce code PHP fait:

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

On dirait que je dois utiliser renderBinary, mais je ne suis pas sûr de savoir comment obtenir d'un BufferedImage au InputStream qui veut renderBinary comme argument.

action 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);
}
Était-ce utile?

La solution 2

Je trouve un exemple dans le code source pour Images.Captcha qui a conduit à cette 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);
}

référencé à l'aide <img id="map" src="@{Application.map(ts.building.code, ts.code)}" width="100%"> dans le modèle de vue.

Pour une raison quelconque, il fonctionne même sans spécifier le type de contenu, mais je ne sais pas comment. Le code Images.Captcha avait si je l'ai gardé, au moins jusqu'à ce que je trouve pourquoi cela fonctionne sans elle.

Autres conseils

Il y a un certain nombre de méthodes renderBinary, l'une qui prend simplement un fichier en tant que paramètre. Voir http: // www. playframework.org/documentation/api/1.1/play/mvc/Controller.html#renderBinary(java.io.File )

Alors, vos besoins de code soit aussi simple que

public static void map(String building_code, String ts_code) throws IOException {
    renderBinary(new File("public/images/maps/" + building_code + "_" + ts_code.charAt(0)));
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top