9 Replies Latest reply on Oct 8, 2010 1:21 AM by Tim Evers

    Displaying blob images

    Charls Cross Newbie

      I have an image in longblob on the database, anyone knows how to display it in xhtml page??

        • 1. Re: Displaying blob images
          Damian Harvey Apprentice

          Assuming that your image is retrieved as a byte[] in your bean:


          <s:graphicImage rendered="#{image != null}" value="#{image}" fileName="myimage.jpg" />
          

          • 2. Re: Displaying blob images
            Charls Cross Newbie

            Thank you!!
            I have one problem, I do this:


            imagen=(byte[]) entityManager.createNativeQuery(select image from images where imageid=43).getSingleResult();


            And I get this exception:


            No Dialect mapping for JDBC type -4


            I'm running Mysql, with driver 5.0.8, the field's type is longblob, do I have to do something different to retrieve the image?


            • 3. Re: Displaying blob images
              Daniel Roth Apprentice

              Ask on the Hibernate or mysql forum. Getting a byte[] from a database is not a Seam related.

              • 5. Re: Displaying blob images
                Charls Cross Newbie

                Ok, perfect, I've got it, just mapping the entity...
                I could display it with s:graphicImage tag.
                But I need to display this image as a panel's  background-image with Css style.


                <rich:panel style="background-image: #{imagesBean.image};position:absolute;top:180px;left:350px;
                                                width:600px;height:600px;">

                • 6. Re: Displaying blob images
                  Charls Cross Newbie

                  Charls Cross wrote on Oct 06, 2008 12:19:


                  Ok, perfect, I've got it, just mapping the entity...
                  I could display it with s:graphicImage tag.
                  But I need to display this image as a panel's  background-image with Css style.

                  <rich:panel style="background-image: #{imagesBean.image};position:absolute;top:180px;left:350px;
                                                  width:600px;height:600px;">


                  Is this possible?? If I do this way I could not get the image...
                  Click HELP for text formatting instructions. Then edit this text and check the preview.

                  • 7. Re: Displaying blob images
                    Ronald Cano Newbie

                    Well, its too late for my answer but I think that many people like me are spending their time looking for this clue. I am using Oracle and Seam and I used the next code for get the image in my BackingBean:




                    public byte[] fixImage(oracle.sql.BLOB image){
                              byte[] imageBytes = null;
                              
                              try{
                                   if(image != null){
                                        imageBytes = image.getBytes(1,(int)image.length());
                                   }
                              }
                              catch(SQLException ex){
                                   
                              }
                                   
                              return imageBytes;  
                         }



                    And in my xhtml, I used this:



                    <s:graphicImage value="#{photoBean.fixImage(entity.image)}" >
                          <s:transformImageType contentType="image/jpeg"/>
                          <s:transformImageSize height="200" maintainRatio="true"/>
                    </s:graphicImage>









                    • 8. Re: Displaying blob images
                      sai burgula Novice

                      I have a BLOB data in oracle which i want to display through seam.  And the above code is very helpful.  But I am having a problem that I am running into.


                      So I create my entity from the database through reverse engineering.  The BLOB column data is converted to java.sql.Blob when the entity is created.  So now when I try to pass the blob data into the method, I need to convert the Blob to oracle.sql.BLOB before passing it to the fixImage method.


                      Any suggestions what I can do.


                      thanks,
                      Sai

                      • 9. Re: Displaying blob images
                        Tim Evers Master

                        You should be using your own custom UserTypes to deal with the blobs so that you don't end up with hacky work arounds like described above.


                        This is my UserType for our blobs in an Ingres database. It isn't finished yet but reading and writing works and so I haven't had the need to fill in all the other methods (or may not need to anyways). Oh and my nullsafeGet is probably wrong now that I take a look at it :P But you get the idea.


                        package gekko.persistence.usertypes.binary;
                        
                        import java.io.Serializable;
                        import java.sql.Blob;
                        import java.sql.PreparedStatement;
                        import java.sql.ResultSet;
                        import java.sql.SQLException;
                        import java.sql.Types;
                        
                        import org.hibernate.Hibernate;
                        import org.hibernate.HibernateException;
                        import org.hibernate.usertype.UserType;
                        
                        import gekko.type.GekkoBlob;
                        
                        public class GekkoBlobUserType implements UserType {
                        
                            public int[] sqlTypes() {
                                return new int[] { Types.LONGVARBINARY };
                            }
                        
                            public Class<?> returnedClass() {
                                return GekkoBlob.class;
                            }
                        
                            public boolean equals(Object x, Object y) {
                                return 
                                    (x == y) || //NOPMD - I want instance equality check here (I think :P)
                                    (   x != null && 
                                        y != null && 
                                        java.util.Arrays.equals(
                                            ((GekkoBlob)x).getData(),
                                            ((GekkoBlob)y).getData()));
                            }
                        
                            public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
                                throws HibernateException, SQLException
                            {
                                Blob blob = rs.getBlob(names[0]);
                                byte[] bytes = blob.getBytes(1, (int) blob.length());
                                return new GekkoBlob(bytes);
                            }
                        
                            public void nullSafeSet(PreparedStatement st, Object value, int index)
                                throws HibernateException, SQLException
                            {
                                st.setBlob(index, Hibernate.createBlob(((GekkoBlob)value).getData()));
                            }
                        
                            public Object deepCopy(Object value) {
                                if (value == null) {
                                    return null;
                                }
                                GekkoBlob blob = (GekkoBlob)value;
                                byte[] bytes = blob.getData();
                                byte[] result = new byte[bytes.length];
                                System.arraycopy(bytes, 0, result, 0, bytes.length);
                                
                                return new GekkoBlob(result);
                            }
                        
                            public boolean isMutable() {
                                return true;
                            }
                        
                            @Override
                            public Object assemble(Serializable cached, Object owner)
                                throws HibernateException
                            {
                                return cached; //TODO: Work out what is supposed to happen here
                            }
                        
                            @Override
                            public Serializable disassemble(Object value) throws HibernateException {
                                return (GekkoBlob)value; //TODO: Work out what is supposed to happen here
                            }
                        
                            @Override
                            public int hashCode(Object x) throws HibernateException {
                                return super.hashCode();
                            }
                        
                            @Override
                            public Object replace(Object original, Object target, Object owner)
                                throws HibernateException
                            {
                                return original; //TODO: Work out what is supposed to happen here
                            }
                        
                        }
                        



                        And the actual Type is:


                        package gekko.type;
                        
                        import java.io.Serializable;
                        import java.util.Arrays;
                        
                        import gekko.util.FormatUtils;
                        
                        public class GekkoBlob implements Serializable {
                            
                            private final byte[] data;
                            
                            public GekkoBlob(byte[] data) {
                                this.data = Arrays.copyOf(data, data.length);
                            }
                        
                            public byte[] getData() {
                                return Arrays.copyOf(this.data, this.data.length);
                            }
                            
                            @Override
                            public String toString() {
                                return FormatUtils.formatObject(this);
                            }
                            
                        }
                        



                        In our entities we declare our columns then using GekkoBlob as the type


                        @Column(name="t_document", nullable=false, length=I_BLOB_MAX_LENGTH)
                        public GekkoBlob getContent() {
                            return content;
                        }
                        



                        How you get hibernate to use this type etc etc I'll leave to your googeling skills. :)