Article View: borland.public.jbuilder.java.api
Article #1409creating a thumbnail image
From: PeterH
Date: Fri, 04 Jan 2002 22:17
Date: Fri, 04 Jan 2002 22:17
95 lines
4172 bytes
4172 bytes
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:
<3c3629fb$1_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!dnewsShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1089d0,9d5844dabb7093fe,start X-Google-Attributes: gid1089d0,public X-Google-ArrivalTime: 2002-01-04 14:18:07 PST From: "PeterH" <!REMOVEBeforeSending!peter@pchenry.com> Newsgroups: borland.public.jbuilder.java.api Subject: creating a thumbnail image Date: Fri, 4 Jan 2002 17:17:35 -0500 Lines: 98 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: <3c3629fb$1_1@dnews> X-Trace: dnews 1010182651 206.191.61.68 (4 Jan 2002 14:17:31 -0800) 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!dnews