Images Uploaded in Seam
tony.herstell1 Aug 19, 2007 11:20 PMApart from not being able to make the accept on file upload work...
(accept ? a comma-separated list of content types to accept, may not be supported
by the browser. E.g. "images/png,images/jpg", "images/*".)
I use this to create a thumbnail for uploded images... (yo can only set one restriction per project for upload file size and elsewhere I want to upload video!)
Anyhow... using this:
/* (non-Javadoc) * @see nz.co.selwynequestriancentre.action.user.UserDetailsController#processAvatarUpload() */ @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) public String processAvatarUpload() { // Seam does all the work for us... if (user.getPicture().getName() != null && !user.getPicture().getName().equals("")) { log.info("Avatar was supplied so adding the Thumbnail for it"); ImageIcon icon = new ImageIcon(user.getPicture().getImage()); user.getPicture().setThumbnail(getRescaledImageAsBytes(user.getPicture().getType(), 70, icon)); } return "null"; } @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) private byte[] getRescaledImageAsBytes(String contentType , int width, ImageIcon icon) { double ratio = (double) width / icon.getIconWidth(); int height = (int) (icon.getIconHeight() * ratio); int imageType = "image/png".equals(contentType) ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB; BufferedImage bImg = new BufferedImage(width, height, imageType); Graphics2D g2d = bImg.createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2d.drawImage(icon.getImage(), 0, 0, width, height, null); g2d.dispose(); String formatName = ""; if ("image/png".equalsIgnoreCase(contentType)) formatName = "png"; else if ("image/jpeg".equalsIgnoreCase(contentType)) formatName = "jpeg"; else if ("image/jpg".equalsIgnoreCase(contentType)) formatName = "jpg"; else if ("image/gif".equalsIgnoreCase(contentType)) formatName = "gif"; ByteArrayOutputStream baos = null; OutputStream out = null; try { baos = new ByteArrayOutputStream(); out = new BufferedOutputStream(baos); try { ImageIO.write(bImg, formatName, out); } catch (IOException e) { e.printStackTrace(); } } finally { // Try to release any resources. try { if (baos != null) { baos.close(); } } catch (IOException ignored) {} try { if (out != null) { out.close(); } } catch (IOException ignored) {} } return baos.toByteArray(); }
I get a problem...
1. Images with a transparent background come out with background as black :(
Does anyone have a piece of code I can crib?