Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate and show thumbnail (byte[]) in JSF
    text
    copied!<p>I'm uploading image to server and when image is uploaded it should show me a thumb of uploaded image. Thumbnail is not saved on hard disc I use InputStream and OutputStream. For upload i'm ustig tomahawk.</p> <p>my index.jsp:</p> <pre class="lang-xml prettyprint-override"><code>&lt;h:form id="uploadForm" enctype="multipart/form-data"&gt; &lt;t:inputFileUpload id="fileupload" accept="image/*" storage="file" value="#{fileUpload.uploadedFile}" styleClass="fileUploadInput" required="true" validator="epacient.FileUploadValidator" requiredMessage="Obvezna izbira datoteke." /&gt; &lt;br /&gt; &lt;h:message for="fileupload" infoStyle="color: green;" errorStyle="color: red;" /&gt; &lt;br /&gt; &lt;h:commandButton value="Upload" id="fileUploadButton" action="#{fileUpload.upload}" /&gt; &lt;h:message for="uploadForm" style="color: red;" /&gt; &lt;h:graphicImage value="#{fileUpload.thumb}" rendered="#{fileUpload.uploaded}" /&gt; &lt;/h:form&gt; </code></pre> <p>fileUpload.upload calls function <code>String preview()</code></p> <pre class="lang-java prettyprint-override"><code>private String thumb ; public String preview() throws IOException{ HttpServletResponse response = (HttpServletResponse)FacesContext .getCurrentInstance().getExternalContext().getResponse(); try { FacesContext context = FacesContext.getCurrentInstance(); Map requestMap = context.getExternalContext().getApplicationMap(); byte[] bytes = (byte[])(requestMap.get("fileupload_bytes")); // returns byte[] byte[] testByte = createThumbnail(bytes, 200); // here thumbnail is created } catch (Exception ex) { ex.printStackTrace(); } } </code></pre> <p>createThumbnail:</p> <pre class="lang-java prettyprint-override"><code>public static byte[] createThumbnail( byte[] orig, int maxDim) { try { ImageIcon imageIcon = new ImageIcon(orig); Image inImage = imageIcon.getImage(); double scale = (double) maxDim / (double) inImage.getWidth(null); int scaledW = (int) (scale * inImage.getWidth(null)); int scaledH = (int) (scale * inImage.getHeight(null)); BufferedImage outImage = new BufferedImage(scaledW, scaledH, BufferedImage.TYPE_INT_RGB); AffineTransform tx = new AffineTransform(); if (scale &lt; 1.0d) { tx.scale(scale, scale); } Graphics2D g2d = outImage.createGraphics(); g2d.drawImage(inImage, tx, null); g2d.dispose(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(outImage, "JPG", baos); byte[] bytesOut = baos.toByteArray(); return bytesOut; } catch (IOException e) { System.out.println("Erro: " + e.getMessage()); e.printStackTrace(); } return null; } </code></pre> <p>Now I have my thumbnail but it is in <code>byte[]</code> can any body tell me how to show my thumb with <code>&lt;h:graphicImage&gt;</code> tag? Or any other way.</p> <p>Thank you !</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload