Home arrow static arrow Java Programming [Archive] - Generating Image Thumbnails from InputStream
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Generating Image Thumbnails from InputStream
This topic has 9 replies on 1 page.

Posts:67
Registered: 2/27/02
Generating Image Thumbnails from InputStream  
Jul 10, 2004 1:45 PM



 
My application has image data stored in a database.

I already accomplished extracting the data from the database into an InputStream.

But now via a servlet I want to create an thumbnail of that image to send back to the user.

I already know how to send the raw image data back to the user, but I'm interested in this case in shrinking the data to thumbnail size and then sending that data back to the browser.

How do I accomplish this?

Most of the thumbnail code I've seen uses java.awt.Image, but I don't see any methods to create a java.awt.Image from an InputStream, nor do I see anything to get a new InputStream from a resized java.awt.Image
 

Posts:11,200
Registered: 7/22/99
Re: Generating Image Thumbnails from InputStream  
Jul 10, 2004 2:58 PM (reply 1 of 9)



 
you're problem can be divided in three parts: 1) decode the stream to an image 2) rescale the image 3) encode the scaled image to a stream

parts 1 & 3 depend on the file format you are using: is it jpeg, gif, bmp, ppm, pcx, wmb, png, xbm, what? If the format is supported by the ImageIO API, encoding and decoding is quite simple. See example here: http://java.sun.com/j2se/1.4.2/docs/guide/imageio/spec/apps.fm1.html
Note that instead of files you can pass streams, see the API documentation or the rest of the image i/o guide linked above for details.

for part 2 there are several options, one is using an AffineTransformOp to filter the image to a smaller one. Here's an untested piece of code that might not work but should give you the general idea:
BufferedImage src = ...
BufferedImage thumbnail = new BufferedImage(src.getWidth()/3, src.getHeight()/3, src.getType());
AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(1/3., 1/3.),
AffineTransformOp.TYPE_BILINEAR);
op.filter(src, thumbnail);
 

Posts:11,200
Registered: 7/22/99
Re: Generating Image Thumbnails from InputStream  
Jul 10, 2004 2:59 PM (reply 2 of 9)



 
you're problem can be divided in three parts: 1) decode the stream to an image 2) rescale the image 3) encode the scaled image to a stream

parts 1 & 3 depend on the file format you are using: is it jpeg, gif, bmp, ppm, pcx, wmb, png, xbm, what? If the format is supported by the ImageIO API, encoding and decoding is quite simple. See example here: http://java.sun.com/j2se/1.4.2/docs/guide/imageio/spec/apps.fm1.html
Note that instead of files you can pass streams, see the API documentation or the rest of the image i/o guide linked above for details.

for part 2 there are several options, one is using an AffineTransformOp to filter the image to a smaller one. Here's an untested piece of code that might not work but should give you the general idea:
BufferedImage src = ...
BufferedImage thumbnail = new BufferedImage(src.getWidth()/3, src.getHeight()/3, src.getType());
AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(1/3., 1/3.),
AffineTransformOp.TYPE_BILINEAR);
op.filter(src, thumbnail);
 

Posts:11,200
Registered: 7/22/99
Re: Generating Image Thumbnails from InputStream  
Jul 10, 2004 2:59 PM (reply 3 of 9)



 
oops

sorry for the double post, i guess i'm more sleepy than i thought
 

Posts:67
Registered: 2/27/02
Re: Generating Image Thumbnails from InputStream  
Jul 10, 2004 3:22 PM (reply 4 of 9)



 
In that code example how are you creating the BuffereredImage src.

What I have is a stream, but the API doesn't show a method of getting a BufferedImage from a stream.
 

Posts:67
Registered: 2/27/02
Re: Generating Image Thumbnails from InputStream  
Jul 10, 2004 3:23 PM (reply 5 of 9)



 
Never mind, I missed the method on the ImageIO class.

Thank you for your assistance.
 

Posts:11,200
Registered: 7/22/99
Re: Generating Image Thumbnails from InputStream  
Jul 10, 2004 3:24 PM (reply 6 of 9)



 
the image io class has such a method, "static BufferedImage read(InputStream input)" please check the api documentation on this site.
 

Posts:67
Registered: 2/27/02
Re: Generating Image Thumbnails from InputStream  
Jul 10, 2004 10:08 PM (reply 7 of 9)



 
Ok, I'm able to do the transformations on the object but now I'm having problems displaying the thumbnail to the browser.

I use the ImageIO.write() method, and pass in the thumbnail, it's type, and response.getOutputStream().

However a broken image appears when I access the servlet.
 

Posts:11,200
Registered: 7/22/99
Re: Generating Image Thumbnails from InputStream  
Jul 10, 2004 10:35 PM (reply 8 of 9)



 
Do you have the servlet online somewhere so we can see?

Try right-clicking on the image, and select "display image" or whatever the equivalent is called in your browser. If you don't get an error message (like 404 Not Found or 500 Internal Server Error) try downloading the image (File->Save) and see what's in it.
 

Posts:67
Registered: 2/27/02
Re: Generating Image Thumbnails from InputStream  
Jul 12, 2004 4:28 PM (reply 9 of 9)



 
I got everything working, thanks for your help.
 
This topic has 9 replies on 1 page.