KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > util > Thumbnail


1 /**
2  * Inspired from http://www.geocities.com/marcoschmidt.geo/java-save-jpeg-thumbnail.html
3  */

4
5 package org.objectweb.jac.util;
6
7 import java.awt.Frame 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.ByteArrayOutputStream JavaDoc;
15 import java.io.OutputStream JavaDoc;
16 import javax.imageio.ImageIO JavaDoc;
17
18 /**
19  * This class creates thumbnails from bigger images. Useful to have a
20  * preview of a photo for example.
21  */

22
23 public class Thumbnail {
24
25     /**
26      * Create a reduced jpeg version of an image. The width/height
27      * ratio is preserved.
28      *
29      * @param data raw data of the image
30      * @param thumbWidth maximum width of the reduced image
31      * @param thumbHeight maximum heigth of the reduced image
32      * @param quality jpeg quality of the reduced image
33      * @return a reduced jpeg image if the image represented by data is
34      * bigger than the maximum dimensions of the reduced image,
35      * otherwise data is returned
36      */

37     public static byte[] createThumbArray(byte[] data,
38                                           int thumbWidth, int thumbHeight,
39                                           int quality)
40         throws Exception JavaDoc
41     {
42         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
43         createThumb(data,thumbWidth,thumbHeight,quality,result);
44         return result.toByteArray();
45     }
46
47     /**
48      * Create a reduced jpeg version of an image. The width/height
49      * ratio is preserved.
50      *
51      * @param data raw data of the image
52      * @param thumbWidth maximum width of the reduced image
53      * @param thumbHeight maximum heigth of the reduced image
54      * @param quality jpeg quality of the reduced image
55      * @param out produce a reduced jpeg image if the image represented
56      * by data is bigger than the maximum dimensions of the reduced
57      * image, otherwise data is written to this stream
58      */

59     public static void createThumb(byte[] data,
60                                    int thumbWidth, int thumbHeight,
61                                    int quality,
62                                    OutputStream JavaDoc out)
63         throws Exception JavaDoc
64     {
65         Image JavaDoc image = Toolkit.getDefaultToolkit().createImage(data);
66         MediaTracker JavaDoc mediaTracker = new MediaTracker JavaDoc(new Frame JavaDoc());
67         int trackID = 0;
68         mediaTracker.addImage(image,trackID);
69         mediaTracker.waitForID(trackID);
70         if (image.getWidth(null)<=thumbWidth &&
71             image.getHeight(null)<=thumbHeight)
72             out.write(data);
73         else
74             createThumb(image,thumbWidth,thumbHeight,quality,out);
75     }
76
77     /**
78      * Create a scaled jpeg of an image. The width/height ratio is
79      * preserved.
80      *
81      * <p>If image is smaller than thumbWidth x thumbHeight, it will be
82      * magnified, otherwise it will be scaled down.</p>
83      *
84      * @param image the image to reduce
85      * @param thumbWidth the maximum width of the thumbnail
86      * @param thumbHeight the maximum heigth of the thumbnail
87      * @param quality the jpeg quality ot the thumbnail
88      * @param out a stream where the thumbnail data is written to
89      */

90     public static void createThumb(Image JavaDoc image,
91                                    int thumbWidth, int thumbHeight,
92                                    int quality,
93                                    OutputStream JavaDoc out)
94         throws Exception JavaDoc
95     {
96         int imageWidth = image.getWidth(null);
97         int imageHeight = image.getHeight(null);
98         double thumbRatio = (double)thumbWidth / (double)thumbHeight;
99         double imageRatio = (double)imageWidth / (double)imageHeight;
100         if (thumbRatio < imageRatio) {
101             thumbHeight = (int)(thumbWidth / imageRatio);
102         } else {
103             thumbWidth = (int)(thumbHeight * imageRatio);
104         }
105         // draw original image to thumbnail image object and
106
// scale it to the new size on-the-fly
107
BufferedImage JavaDoc thumbImage =
108             new BufferedImage JavaDoc(thumbWidth,
109                               thumbHeight, BufferedImage.TYPE_INT_RGB);
110         Graphics2D JavaDoc graphics2D = thumbImage.createGraphics();
111         graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
112                                     RenderingHints.VALUE_INTERPOLATION_BILINEAR);
113         graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
114         // save thumbnail image to out stream
115
ImageIO.write(thumbImage,"jpeg",out);
116     }
117 }
118
119
Popular Tags