Article View: borland.public.jbuilder.java.api
Article #1411Re: creating a thumbnail image
From: PeterH
Date: Mon, 07 Jan 2002 19:17
Date: Mon, 07 Jan 2002 19:17
110 lines
4671 bytes
4671 bytes
I think I fixed it myself. The problem was I was passing in the wrong value
into the maxDim param. :<
Thanks for your time.
"PeterH" <!REMOVEBeforeSending!peter@pchenry.com> wrote in message
news:3c3629fb$1_1@dnews...
> I've been trying for the life of me to create a thumbnail of a jpg, but I
> keep getting an out of memory error. I took the code from one of Sun's
> websites:
> -------------------------------------------------------------------
> import java.awt.Image;
> import java.awt.Graphics2D;
> import java.awt.geom.AffineTransform;
> import java.awt.image.BufferedImage;
> import java.io.IOException;
> import java.io.OutputStream;
> import java.io.FileOutputStream;
> import javax.swing.ImageIcon;
> import com.sun.image.codec.jpeg.JPEGCodec;
> import com.sun.image.codec.jpeg.JPEGImageEncoder;
>
> class Thumbnail {
> public static void main(String[] args) {
> createThumbnail(args[0], args[1], Integer.parseInt(args[2]));
> }
>
> /**
> * Reads an image in a file and creates
> * a thumbnail in another file.
> * @param orig The name of image file.
> * @param thumb The name of thumbnail file.
> * Will be created if necessary.
> * @param maxDim The width and height of
> * the thumbnail must
> * be maxDim pixels or less.
> */
> public static void createThumbnail(
> String orig, String thumb, int maxDim) {
> System.err.println("orig=" + orig);
> System.err.println("thumb=" + thumb);
> System.err.println("maxDim=" + maxDim);
>
> try {
> // Get the image from a file.
> Image inImage = new ImageIcon(orig).getImage();
>
> // Determine the scale.
> double scale = (double)maxDim/(double)inImage.getHeight(null);
> if (inImage.getWidth(null) > inImage.getHeight(null)) {
> scale = (double)maxDim/(double)inImage.getWidth(null);
> }
>
> // Determine size of new image.
> //One of them
> // should equal maxDim.
> int scaledW = (int)(scale*inImage.getWidth(null));
> int scaledH = (int)(scale*inImage.getHeight(null));
>
> // Create an image buffer in
> //which to paint on.
> BufferedImage outImage = new BufferedImage( scaledW, scaledH,
> BufferedImage.TYPE_INT_RGB);
> //BufferedImage outImage = new BufferedImage(scaledW, scaledH,
> BufferedImage.TYPE_INT_RGB);
>
> // Set the scale.
> AffineTransform tx = new AffineTransform();
>
> // If the image is smaller than
> //the desired image size,
> // don't bother scaling.
> if (scale < 1.0d) {
> tx.scale(scale, scale);
> }
>
> // Paint image.
> Graphics2D g2d = outImage.createGraphics();
> g2d.drawImage(inImage, tx, null);
> g2d.dispose();
>
> // JPEG-encode the image
> //and write to file.
> OutputStream os = new FileOutputStream(thumb);
> JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
> encoder.encode(outImage);
> os.close();
> } catch (IOException e) {
> e.printStackTrace();
> }
> System.exit(0);
> }
> }
>
> -------------------------------------------------------------------
>
> If you have any ideas on this, it would be greatly appreciated. Or if you
> know of a better way to do this? I thought I could use the rescale image
> with a dimension (640, 480) type of parameter, but that doesn't seem to
> work. :<
>
> Thanks for any ideas.
>
>
>
Technical Details
Message-ID:
<3c3a1aa1$1_1@dnews>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!supernews.com!news.tele.dk!small.news.tele.dk!128.230.129.106!news.maxwell.syr.edu!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!dnewsReferences:
<3c3629fb$1_1@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1089d0,9d5844dabb7093fe X-Google-Attributes: gid1089d0,public X-Google-ArrivalTime: 2002-01-07 14:02:45 PST From: "PeterH" <!REMOVEBeforeSending!peter@pchenry.com> Newsgroups: borland.public.jbuilder.java.api References: <3c3629fb$1_1@dnews> Subject: Re: creating a thumbnail image Date: Mon, 7 Jan 2002 14:17:54 -0500 Lines: 112 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2600.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 NNTP-Posting-Host: 206.191.61.68 Message-ID: <3c3a1aa1$1_1@dnews> X-Trace: dnews 1010440865 206.191.61.68 (7 Jan 2002 14:01:05 -0800) Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!supernews.com!news.tele.dk!small.news.tele.dk!128.230.129.106!news.maxwell.syr.edu!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!dnews