Hi everyone,
I am trying to automatically create small JPEG thumbnails from existing JPEG
images. The basic code I am using is as shown below, which works but gives
the most horrendous, unusable results. I don't know how to control the
quality of the thumbnail because I haven't seen any examples of how to do
it. I have displayed the intermediate scaled buffered image on the screen
and it looks great, so it must be possible to get similar results in a file.
How do you do it?
Cheers
David
p.s. to run the code example below use the following syntax (which creates a
thumbnail file called "thumbnail.jpg" of height 150 from the image in the
file "original.jpg"):
java Example original.jpg 150
import java.io.FileOutputStream;
import java.io.IOException;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
import com.sun.image.codec.jpeg.JPEGCodec;
import java.awt.image.AffineTransformOp;
import java.awt.geom.AffineTransform;
import java.awt.RenderingHints;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class Example
public static void main(String[] arguments){
String fileName = arguments[0];
int thumbnailHeight = Integer.valueOf(arguments[1]).intValue();
Example example = new Example();
try {
example.execute(fileName, thumbnailHeight);
} catch(Exception e){
System.err.println("oops");
e.printStackTrace();
}
}
public void execute(String fileName, int thumbnailHeight) throws
Exception
// load the original JPEG image as a buffered image
FileInputStream fis = new FileInputStream(fileName);
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(fis);
BufferedImage image = decoder.decodeAsBufferedImage();
fis.close();
// work out what the scaling factor is, assuming it the thumnail has
the given height
double scalingFactor = thumbnailHeight / (double)image.getHeight();
// prepare to rescale the image
AffineTransform affineTransform =
AffineTransform.getScaleInstance(scalingFactor, scalingFactor);
// sort out some rendering hints for the graphics engine
RenderingHints hints = new RenderingHints(null);
hints.put(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
hints.put(RenderingHints.KEY_COLOR_RENDERING,
RenderingHints.VALUE_COLOR_RENDER_QUALITY);
hints.put(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
// rescale
AffineTransformOp op = new AffineTransformOp(affineTransform,
hints);
BufferedImage scaledImage = op.filter(image, null);
// create the output file
FileOutputStream fos = new FileOutputStream("thumbnail.jpg");
//JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos, encp);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
encoder.encode(scaledImage);
fos.close();
}
}
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".