0

I have a SQL database that contains images stored as a byte array. I have done some searching and have not found much on how to convert these into a useable Image type for a JSF page. Any help would be greatly appreciated!

Thanks in advance!

(using JSF 2.0)

cdub
  • 3
  • 1
  • 2
  • It would depend on how the image has been converted into a ByteArray. – Nivas Aug 12 '11 at 13:39
  • Would it work for you to write a piece of code that creates a java.awt.image.BufferedImage of an appropriate size, converts groups of bytes into java.awt.Color objects, and uses the getRGB() method of the Color objects to get an RGB value to give the the BufferedImage's setRGB() method? – compman Aug 12 '11 at 13:42
  • i tried something like ' image logo; EventDTO event; logo = toolkit.createImage(event.getLogo());' – cdub Aug 12 '11 at 14:14

3 Answers3

1

Just a create a controller that output the right media type (image/*) and output the bytes. There is no need to convert anything. If you want to manipulate the images then yes you can convert it using ImageIO.read. But from your question it sounds like you just want to display the image.

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    byte[] bytes = ...  
    resp.setContentType(mimeType);    
    OutputStream out = resp.getOutputStream();
    out.write(bytes);
}
Amir Raminfar
  • 33,777
  • 7
  • 93
  • 123
  • OK I apologize for the confusion, yes I would just like to display it, any pointers to any material would be helpful. Thanks so much for your time! – cdub Aug 12 '11 at 13:54
  • Or maybe some additional explanation on "just a create a controller that output the right media type (image/*) and output the bytes. " – cdub Aug 12 '11 at 14:15
  • Look at this example http://www.exampledepot.com/egs/javax.servlet/GetImage.html. Instead of reading from a file, you should read from the db. – Amir Raminfar Aug 12 '11 at 14:23
0

Try doing something similar to this.

Bitmap
  • 12,402
  • 16
  • 64
  • 91
0

This is not a working example but it outlines the approach on how to dynamically display an image stored in database. You have to create a servlet (depending on the framework you use it may be a struts action or so) that behaves like an image:

@WebServlet("/images/*")
public class ImageServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        /* this is pseudo code, retrieve the image and its metadata from database.
         * Maybe you do not want to use a parameter but a RESTful approach, e.g. '/images/image1234'.
         */
        MyImage myimg = MyDatabase.getImage(request.getParameter("imageID"));

        /* you may want to support different encodings (e.g. JPG, PNG, TIFF) */
        response.setContentType(myimg.getContentType());

        /* obtain output stream and stream the bytes back to the client */
        OutputStream out = response.getOutputStream();

        /* stream it, here you have different options, finally close the stream */
        out.write(myimg.getBytes());
    }

}

In your JSF page you have to reference the servlet accordingly:

<img src=".../images/image1234" />

Hope this helps.

home
  • 12,468
  • 5
  • 46
  • 54