• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

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

 
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
 
Marshal
Posts: 67365
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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.
 
Rancher
Posts: 43011
76
  • 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
Marshal
Posts: 67365
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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.
 
You can't have everything. Where would you put it?
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!