KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > Thumbnail


1 package test;
2
3 /*
4  * Thumbnail.java (requires Java 1.2+)
5  */

6
7 import java.awt.Container JavaDoc;
8 import java.awt.Graphics2D JavaDoc;
9 import java.awt.Image JavaDoc;
10 import java.awt.MediaTracker JavaDoc;
11 import java.awt.RenderingHints JavaDoc;
12 import java.awt.Toolkit JavaDoc;
13 import java.awt.image.BufferedImage JavaDoc;
14 import java.io.BufferedOutputStream JavaDoc;
15 import java.io.FileOutputStream JavaDoc;
16
17 import com.sun.image.codec.jpeg.JPEGCodec;
18 import com.sun.image.codec.jpeg.JPEGEncodeParam;
19 import com.sun.image.codec.jpeg.JPEGImageEncoder;
20
21 public class Thumbnail
22 {
23
24     public static void main(String JavaDoc[] args) throws Exception JavaDoc
25     {
26         if (args.length != 5)
27         {
28             System.err.println("Usage: java Thumbnail INFILE "
29                     + "OUTFILE WIDTH HEIGHT QUALITY");
30             System.exit(1);
31         }
32         // load image from INFILE
33
Image JavaDoc image = Toolkit.getDefaultToolkit().getImage(args[0]);
34         MediaTracker JavaDoc mediaTracker = new MediaTracker JavaDoc(new Container JavaDoc());
35         mediaTracker.addImage(image, 0);
36         mediaTracker.waitForID(0);
37         // determine thumbnail size from WIDTH and HEIGHT
38
int thumbWidth = Integer.parseInt(args[2]);
39         int thumbHeight = Integer.parseInt(args[3]);
40         double thumbRatio = (double) thumbWidth / (double) thumbHeight;
41         int imageWidth = image.getWidth(null);
42         int imageHeight = image.getHeight(null);
43         double imageRatio = (double) imageWidth / (double) imageHeight;
44         if (thumbRatio < imageRatio)
45         {
46             thumbHeight = (int) (thumbWidth / imageRatio);
47         }
48         else
49         {
50             thumbWidth = (int) (thumbHeight * imageRatio);
51         }
52         // draw original image to thumbnail image object and
53
// scale it to the new size on-the-fly
54
BufferedImage JavaDoc thumbImage = new BufferedImage JavaDoc(thumbWidth, thumbHeight,
55                 BufferedImage.TYPE_INT_RGB);
56         Graphics2D JavaDoc graphics2D = thumbImage.createGraphics();
57         graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
58                 RenderingHints.VALUE_INTERPOLATION_BILINEAR);
59         graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
60         // save thumbnail image to OUTFILE
61
BufferedOutputStream JavaDoc out = new BufferedOutputStream JavaDoc(
62                 new FileOutputStream JavaDoc(args[1]));
63         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
64         JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);
65         int quality = Integer.parseInt(args[4]);
66         quality = Math.max(0, Math.min(quality, 100));
67         param.setQuality((float) quality / 100.0f, false);
68         encoder.setJPEGEncodeParam(param);
69         encoder.encode(thumbImage);
70         System.out.println("Done.");
71         System.exit(0);
72     }
73 }
Popular Tags