Posted By:
Dan_J
Posted On:
Thursday, December 5, 2002 09:13 AM
You can write an easy(ish) tool to load the file, resize it and then save it as a thumbnail, will look something like the code below. The only thing that you'll have to look up is how to make sure that you have waited for the Image to completely load from file before you try to use it.
import java.io.File;
import java.awt.*;
import java.awt.image.BufferedImage;
// Load the JPEG from file
Image thisImage = YourTool.getImage(fileName);
// Scale the thumbnail dimensions so that the thumbnail will
// fit into a thumbnailMaxSize by thumbnailMaxSize square.
double scaleFactor;
if (xDim>yDim)
{
// Scale x then y
scaleFactor = (double)(thumbnailMaxSize) / xDim;
}
else
{
// Scale y first
scaleFactor = (double)(thumbnailMaxSize) / yDim;
}
// Make the thumbnail
BufferedImage bufferedImage = new BufferedImage(
(int)(xDim * scaleFactor),
(int)(yDim * scaleFactor),
BufferedImage.TYPE_INT_RGB);
Graphics g = bufferedImage.getGraphics();
g.drawImage(thisImage, 0, 0, (int)(xDim * scaleFactor), (int)(yDim * scaleFactor), null);
// Now save it
YourTool.saveToJPG(bufferedImage, fileNameWithoutSuffix + THUMBNAIL_SUFFIX);