views:

212

answers:

1

Hi All

I want to generate image on server side. My Image is stored in server side's database in blob format and I am able to convert it into string_image; Then How to convert that string_image into actual .jpg or .png format.

Actually I am posting attachment as image on users facebook' wall. How to generate Image at server side in Java? Is there any sample code to do it?

A: 

If you have the image in Blob format then you're best bet it to use ImageIO and read the blob input stream directly.

This will give you a java.awt.BufferedImage object which you can then render to any type of image using ImageIO.

BufferedImage image = ImageIO.read( blob.getInputStream() );
// Feel free to modify the image here using image.createGraphics()
// add a water mark if you're feeling adventurous
ImageIO.write( image, "png", someOutputStream );

I'm not intimately familiar with how facebook uses images. I think it differs depending on what you're doing. For facebook applications, I think you're still responsible for hosting the image, but facebook will act as a caching proxy and request the image just once, then serve it to all the client that need to see it. If it's a user posted image, then I think facebook stores the image completely (obviously a user uploading a picture does not have it on another server usually).

If you're looking to just display the image "as is" in response to a a web request, then you can pipe the output of the Blob directly the client, although this keeps database connections open for a while, you might be better copying to a disk cache then serving to the client.

I have to confess, I couldn't find anything useful about what a stirng_image actually is? Perhaps you could clarify if this answer doesn't meet your needs.

Geoff
Thanks for Replay. I am geting bolb from database pass it following function then It returns Image in string format.Means Blob is converted into string_image. public String returnBase64Code(Blob blob) { String string_image = null; try { byte[] bytes = blob.getBytes((long) 1, (int) blob.length()); byte[] bytes64 = Base64.encode(bytes); string_image = new String(bytes64); } catch (Exception e) { e.printStackTrace(); } return string_image; }Above code written in jAVA
Vaibhav Bhalke
Vaibhav Bhalke
Like I said, don't convert to a String. This is wasteful and unhelpful towards your goal of getting the image file. Blob has the getBinaryStream() method to get it's data. If you just want to write the image directly to file, then just pipe this input stream to a FileOutputStream. If you want to resize it or change the image encoding, then use ImageIO.read and ImageIO.write. If you really must start from a Base64 encoding of the image, then you'll just have to run it through a Base64 decoder first. Then you can use a ByteArrayInputStream as the source for ImageIO.read (or to write to file).
Geoff
THANkS !!!!!!!!
Vaibhav Bhalke