9 Replies Latest reply on Aug 21, 2007 7:19 AM by tony.herstell1

    Images Uploaded in Seam

    tony.herstell1

      Apart 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?

        • 1. Re: Images Uploaded in Seam

          Why not draw a white rectangle on the Graphics2D before you draw the image onto it?

          • 2. Re: Images Uploaded in Seam
            tony.herstell1

            Thanks..
            Will that help?
            With normal .gif and .jpg it works fine.
            Well, unless they have transparanceies when the transparent colour comes out in black.
            I feel like ditching the idea of reducing it and just limit by size in code.
            :(

            • 3. Re: Images Uploaded in Seam

              Yes it will help! Try it. Make a Rectange that is the same size as the Graphics2D. Fill the rectangle with white. Then draw the image. It adds about three lines of code.

              • 4. Re: Images Uploaded in Seam
                tony.herstell1

                Ok, but surely this will mean the image will no longer be transparent.
                Is there no way to retain transparency?
                Is this a limitation of Java2D?

                • 5. Re: Images Uploaded in Seam

                  Ah, right, if you do that then the images will no longer be transparent and that's not what you want. But, if the image is transparent, does it have a background color? Sometimes you have to experiment with the options on this one. Make it a four-byte image format, and try using transparent colors for the BG. I can't remember the exact combination of parameters to use unfortunately.

                  • 6. Re: Images Uploaded in Seam
                    tony.herstell1

                    Ok, Thanks for your support. I am looking into the varios parameters now.

                    • 7. Re: Images Uploaded in Seam
                      tony.herstell1

                      I have chatted with a GFX expert and to produce the code that can handle animated gifs and transparancies through a re-szie is not a small job.
                      I think I will bow out on this for now and jsut restrict the user from uploading an image that is too big (therefore making THEM do it).
                      Shame really as thats pretty unfriendly...
                      I could just allow ANY image and display it to a given size but this means I may be passing round MASSIVE images AND small images may get scaled up.
                      Grrr...
                      Nice feature to add to seam perhaps... (well it has everything else!)
                      :/

                      • 8. Re: Images Uploaded in Seam
                        obfuscator

                         

                        "tony.herstell@gmail.com" wrote:
                        I have chatted with a GFX expert and to produce the code that can handle animated gifs and transparancies through a re-szie is not a small job.
                        I think I will bow out on this for now and jsut restrict the user from uploading an image that is too big (therefore making THEM do it).
                        Shame really as thats pretty unfriendly...
                        I could just allow ANY image and display it to a given size but this means I may be passing round MASSIVE images AND small images may get scaled up.
                        Grrr...
                        Nice feature to add to seam perhaps... (well it has everything else!)
                        :/


                        If you want your images to support transparency, you need to make them of type TYPE_INT_ARGB, not RGB. If you are dealing with hard stuff, such as
                        animated gifs etc, try using imagemagick, there is a java wrapper called
                        jmagick, but it's not very actively supported, just so you know.

                        • 9. Re: Images Uploaded in Seam
                          tony.herstell1

                          Thx