Click to See Complete Forum and Search --> : Image Object Displayed in HTML


yellabuff
May 14th, 2010, 09:53 AM
I am trying to display an image in the browser from the backend. The code I have is...

response.setContentType("image/jpeg");
OutputStream osResponse = response.getOutputStream();
osResponse = response.getOutputStream();
BufferedImage biNew = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = biNew.createGraphics();
g.drawImage(form.getEmpImage(), null, null);
ImageIO.write(biNew, "jpg", osResponse);
osResponse.flush();
osResponse.close();
return null;

form.getEmplmage() is defined as....

private Image empImage;

I don't get any errors but nothing displays in the browser.

This is the first time I've had to do anything like that so I'm at a total loss....

Please help!

Norm
May 14th, 2010, 10:11 AM
Not sure what you are doing here. Is this server side code? Then why use drawImage()?
Why not write an HTML <IMG statement and let the browser read the image from the server?

Could you explain what you are doing in more detail?

yellabuff
May 14th, 2010, 10:19 AM
Yes, it's server code. I actually found that code and tried to implement it but as you can see I really don't know what I'm doing.

In the browser/jsp, form.getEmpImage().getSource() displays something like sun.awt.byteArrayImageSource@2022020...something like that. So my <img> tag is just a broken graphic. I don't have an actual file name to put in the img src.

Norm
May 14th, 2010, 08:02 PM
Sorry, I'll have to do some research.

Meanwhile, write a short simple app that uses your code to write a jpg file to disk. That code should be the same as what you'll need in the servlet.

What do you see in the browser with your code?
What does the HTML statement look like that calls your servlet to get the image?

Not sure what "form.getEmpImage().getSource()" is or where lives/is executed.


Ideas to find the problem:
Try replacing the drawImage() method call with siomething simple like drawString() or drawLine() to see if everything else is working properly.

Write the image to a local disk file(in addition to returning it as a response) and see if its OK.