How to display a binary image in a webpage?
Hi,
I want to display a binary image in a webage. The image has been stored in a DB as binary format. I am using servlet. How could I retrieve it and display it on a webpage? Could you give me some suggestions?
I have read some metrial about JAI, the method 'createImageEncoder( )', which seems very helpful! I am on the right way, right? Thanks for your any comments!
Best Regards,
Hai
[419 byte] By [
hai.rena] at [2007-11-27 10:38:12]

What do you mean "binary format"? There are many binary formats for images--gif, tiff, jpeg, bmp, ...
You don't "display it in a web page." Your servlet simply sends the bytes and the encoding information and the browser displays it.
jverda at 2007-7-28 18:52:59 >

For the formats, gif bmp and jpeg should be supported
Yes, you are right, my servlet just sending the bytes.
Regarding to my question, what should I do? Thanks! : )
Best Regards,
> For the formats, gif bmp and jpeg should be
> supported
>
no, they should not.
Most browsers (in fact no browser I'm aware of) can't display bmp (and which version anyway? There are several).
response.reset();
response.setContentType("image/jpeg");
BufferedImage bim = createImage(); // get and create the image from DB data
OutputStream os = response.getOutputStream();
try{
ImageIO.write(bim, "jpg", os);
}
catch (IOException e({
e.printStackTrace();
}
os.flush();
hiwaa at 2007-7-28 18:52:59 >

Here is my idea to display the pic on the webpage:
The pic is stored in DB as binary, after retrieving it, the binary data will be converted to JPEG pic. Then, the pic will be saved to the local machine automaticly. After that, I get the pic's SRC, and use html display it.
My code is as follow (not completed), some errors in it. public static String getPayoffSRC(Services serv, long fileId)
throws FileNotFoundException,
DBException,
DbFileNotFoundException,
IOException {
BufferedImage buffy = new BufferedImage(300, 400,
java.awt.image.BufferedImage.TYPE_INT_RGB);
FileOutputStream out = new FileOutputStream(serv.getFileManager().getFileDb(fileId).getFileName());
JPEGImageEncoder jencoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam enParam = jencoder.getDefaultJPEGEncodeParam(buffy);
enParam.setQuality(1.0F, true);
jencoder.setJPEGEncodeParam(enParam);
jencoder.encode(buffy);
out.close();
return Pic's SRC,
}
> The pic is stored in DB as binary
In what format?
Anyway I believe these days no one won't use/write such esoteric code as yours.
hiwaa at 2007-7-28 18:52:59 >

You have to handle two transactions. The first (typically a JSP) creates the HTML of the page, and includes an <IMG> tag for the graphics part. If graphics is generated on the fly, then the src in the tag will typically invoke a servlet that returns the image data, in a standard image format like gif or png.
You'll normally pass some kind of query string in the IMG src which specifies which image to deliver. The first transaction might, for example place the data required for the image in a session attribute with a generated key, and the key is then passed in the src uri so that the image delivery servlet can pick up the data.
I've generally found that the various image file generators (the most common would be ImageIO) insist on writing to a file, so the servlet usually winds up creating a temporary file on the server, then copying it to the client.
The tiresome part is cleaning up. You can't assume, for example, that the image gets retrieved only once.
the pic is stored as blob type in DB
But what format is the binary data?
"Binary data" isn't a format onto itself.
Is it the same binary data that you get from reading an image file from the disk?
Where does the data come from, anyway?
The easiest thing to do is just store the data in a standard image format -- say, JPEG. So the bytes in the BLOB are the same bytes that would exist if the file were on disk. If you can't standardize on a single format (or don't want to -- maybe you want to do content negotiation), then have another column in the DB listing the MIME type of the data.
Then, have a servlet that simply spews that binary data to the client in the response object (basically such code has already been shown to you; the only diff is that the code posted earlier lets you create the image in Java). Set the content-type first.
Then your HTML just has an <IMG> tag whose SRC attribute points to this servlet.