• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trying to display java.awt.Image in a JSP page.

 
Susan Smith
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm sorry if this is a newbie question...

I have a 3rd party SDK that will return me java.awt.Image object.
I'm trying to display the images on a JSP pages. How can I do this?

I can't seem find related method in the javadoc to do this.
I do some research but I can only find methods using BufferedImage instead of Image object.
For example, this one: http://today.java.net/pub/a/today/2004/04/22/images.html
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSP just produces HTML that is sent to the browser. So you need to use HTML mechanisms, right? Use an <img> tag just as in other HTML pages.

The src attribute of the tag can reference a servlet that returns the image data, along with setting the appropriate response headers. You'll need to find out how to generate a supported image type (gif, jpg, png, etc) from the java.awt.Image instance.
 
Susan Smith
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear,

Thanks for your reply.

I have been researching this on Google actualy:
You'll need to find out how to generate a supported image type (gif, jpg, png, etc) from the java.awt.Image instance.

But can't seem to find anything.
 
Susan Smith
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just found something.....

http://forums.sun.com/thread.jspa?threadID=367004

Let me test this.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saving the image to a file in a publicly accessible directory is one way to achieve this.

But ImageIO can also write the image contents into a byte[], and then you could use a servlet (which would be referenced in the "src" attribute of an HTML img tag) to stream the image to the browser. That way you can avoid file I/O.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:That way you can avoid file I/O.
 
Susan Smith
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the following code to convert Image object to "GIF" file.
The example is from: http://forums.sun.com/thread.jspa?threadID=367004



This line doesn't seem to compile:


I tried changing the first argument to the BufferedImage object instead. But the GIF file generated seems to be empty.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!