Article View: borland.public.jbuilder.java.api
Article #1410Re: creating a thumbnail image
From: Joi Ellis [TeamB]
Date: Sat, 05 Jan 2002 14:55
Date: Sat, 05 Jan 2002 14:55
109 lines
4765 bytes
4765 bytes
PeterH <!REMOVEBeforeSending!peter@pchenry.com> wrote:
> 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:
Could you please post the complete text of the error message?
> -------------------------------------------------------------------
> 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.
--
Joi Ellis (TeamB) http://www.teamb.com/
No direct email, please.
For best results, post on news server newsgroups.borland.com!
Newsgroup Guidelines: http://www.borland.com/newsgroups/guide.html
Only posts which violate the rules in guide.html are subject to cancelation.
Technical Details
Message-ID:
<3c3713cf_1@dnews>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!supernews.com!newsfeed.direct.ca!look.ca!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!KWeb!dnews!not-for-mailReferences:
<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-05 07:02:23 PST
Sender: Joi Ellis <joi@artoo.gyles19.visi.com>
Message-ID: <3c3713cf_1@dnews>
From: "Joi Ellis [TeamB]" <joi@aravox.com>
Subject: Re: creating a thumbnail image
Newsgroups: borland.public.jbuilder.java.api
References: <3c3629fb$1_1@dnews>
User-Agent: tin/1.4.2-20000205 ("Possession") (UNIX) (Linux/2.2.19-6.2.7 (i686))
NNTP-Posting-Host: 209.98.6.107
Date: 5 Jan 2002 06:55:12 -0800
X-Trace: dnews 1010242512 209.98.6.107 (5 Jan 2002 06:55:12 -0800)
Lines: 109
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!supernews.com!newsfeed.direct.ca!look.ca!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!KWeb!dnews!not-for-mail