8 Replies Latest reply on Jan 18, 2007 4:12 AM by Stephen Williams

    Displaying Picture from blob

    Vincent Wong Newbie

      I was able to upload picture to database using file upload and tomahawk.jar. But I don't know how to display the picture in jsf using seam.

      If I just use HttpServlet I can write directly using:
      response.setContentType("image/jpeg");
      response.getOutputStream().write(productImage);

      and in the xhtml I use:


      How to get do this in seam? Anyone can help?

        • 1. Re: Displaying Picture from blob
          Stephen Williams Newbie

          I would also be very interested in an answer to this question as I am trying to do the same thing. I have already implimented this with a servlet but I would like to do using a statefull session bean.

          If anyone could please explain how this can be done I would be very greatfull indead.

          Regards,
          Stephen

          • 2. Re: Displaying Picture from blob
            Pete Muir Master

            I wrote a sample solution http://jroller.com/page/pmuir?entry=dynamicimagecomponent_for_jsf, also, I believe Ajax4JSF contains the 'media' component which does this and more.

            • 3. Re: Displaying Picture from blob
              Stephen Williams Newbie

              Hi

              Thanks for the answer petemuir!

              I would like to get he Ajax4jsf solution working as everywhere I read it should just work with seam 1.1 GA but I am have a problem...

              I get the error...

              javax.el.ELException: /home.xhtml: Bean: demo.PaintBean$$EnhancerByCGLIB$$6f0837bd, property: paint
               at com.sun.facelets.compiler.AttributeInstruction.write(AttributeInstruction.java:53)
               at com.sun.facelets.compiler.UIInstructions.encodeBegin(UIInstructions.java:39)
               at com.sun.facelets.tag.jsf.ComponentSupport.encodeRecursive(ComponentSupport.java:232)
               at com.sun.facelets.tag.jsf.ComponentSupport.encodeRecursive(ComponentSupport.java:239)
              





              I added the following in my facelet...
              <a4j:mediaOutput createContent="#{hello.renderImage}" value="#{hello.value}.jpg" element="img" mimeType="image/jpeg" />
              



              the following to my bean...


              public void renderImage(OutputStream out, Object data) throws IOException {
               BufferedImage img = new BufferedImage(100,
               50, BufferedImage.TYPE_INT_RGB);
               Graphics2D graphics2D = img.createGraphics();
               graphics2D.setBackground(Color.BLACK);
               graphics2D.setColor(Color.WHITE);
               graphics2D.clearRect(0, 0, 100, 50);
               graphics2D.drawLine(5, 5, 100 - 5, 50 - 5);
               graphics2D.drawChars(new String("Ajax4JSF").toCharArray(), 0, 8,
               40, 15);
               graphics2D.drawChars(new String("mediaOutput").toCharArray(), 0,
               11, 5, 45);
               ImageIO.write(img, "jpeg", out);
               }
              
              



              the following to my interface...
              public void renderImage(OutputStream out, Object data) throws IOException;
              
              


              and then deployed it.


              If anyone has allready done this I would really appreciate a small example.


              • 4. Re: Displaying Picture from blob
                Pete Muir Master

                Probably you are better off asking this on the ajax4jsf forum/mailing list (whichever they use) as it's relevant to that than to Seam.

                • 5. Re: Displaying Picture from blob
                  Stephen Williams Newbie

                  I have gotten one step further, hurray! My problem was that the ajax4jsf jars (ajax4jsf.jar and oscache-2.3.2.jar were not being deployed.


                  I now have the same problem that has been posted at

                  http://jboss.com/index.html?module=bb&op=viewtopic&p=3999655


                  I will continue there.

                  • 6. Re: Displaying Picture from blob
                    None None Master

                    I haven't looked at Peter's solution but I know it would be quite easy and efficient to write a plain old Servlet to do this. It is perfectly possible to use EntityManagers within Servlets, and serving an image from a Servlet is a good way to go. You avoid any overhead of Seam, JSF or anything else, and it can be done with just a few lines of code in a servlet.

                    • 7. Re: Displaying Picture from blob
                      Shane Bryzak Master

                      The Seamspace example in CVS serves images from a servlet, and provides an image rescaling feature also. Take a look at ContentServlet.java, it's pretty straight forward - and it uses SeamServletFilter to set up the Seam contexts.

                      • 8. Re: Displaying Picture from blob
                        Stephen Williams Newbie

                        Thanks guys!

                        I will check it out. Ideally I would like to have a statefull bean that handels all the attributes and generates the image. Of couse if I don't manage this I will also try out peters solution or just a plain servlet.