KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > image > ImageIOThumbnailer


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.common.image;
25
26 import java.awt.Graphics2D JavaDoc;
27 import java.awt.RenderingHints JavaDoc;
28 import java.awt.image.BufferedImage JavaDoc;
29 import java.awt.image.RenderedImage JavaDoc;
30 import java.io.File JavaDoc;
31 import java.io.FileInputStream JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.InputStream JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.Locale JavaDoc;
36
37 import javax.imageio.IIOImage JavaDoc;
38 import javax.imageio.ImageIO JavaDoc;
39 import javax.imageio.ImageWriteParam JavaDoc;
40 import javax.imageio.ImageWriter JavaDoc;
41 import javax.imageio.plugins.jpeg.JPEGImageWriteParam JavaDoc;
42 import javax.imageio.stream.ImageOutputStream JavaDoc;
43
44 import org.riotfamily.common.util.FormatUtils;
45 import org.springframework.util.Assert;
46
47 /**
48  * Thumbnailer that uses the Java ImageIO API.
49  *
50  * @author Felix Gnass [fgnass at neteye dot de]
51  */

52 public class ImageIOThumbnailer implements Thumbnailer {
53     
54     static {
55         System.setProperty("java.awt.headless", "true");
56     }
57     
58     private static final String JavaDoc FORMAT_JPG = "jpg";
59     
60     private static final String JavaDoc FORMAT_PNG = "png";
61     
62     private int maxCrop = 0;
63         
64     public void setMaxCrop(int maxCrop) {
65         this.maxCrop = maxCrop;
66     }
67
68     public void renderThumbnail(File JavaDoc source, File JavaDoc dest, int width, int height)
69             throws IOException JavaDoc {
70         
71         BufferedImage JavaDoc originalImage = readImage(new FileInputStream JavaDoc(source));
72         boolean alpha = originalImage.getColorModel().hasAlpha();
73         
74         int imageWidth = originalImage.getWidth(null);
75         int imageHeight = originalImage.getHeight(null);
76         
77         int thumbWidth;
78         int thumbHeight;
79         
80         double scaleX = (double) width / (double) imageWidth;
81         double scaleY = (double) height / (double) imageHeight;
82         
83         double scale = Math.min(Math.max(scaleX, scaleY), 1);
84         thumbWidth = (int) (imageWidth * scale);
85         thumbHeight = (int) (imageHeight * scale);
86         double cropFactor = (double) maxCrop / 100;
87
88         if ((thumbWidth - width > cropFactor * thumbWidth)
89                 || (thumbHeight - height > cropFactor * thumbHeight)) {
90             
91             scaleX = width / (imageWidth - cropFactor * imageWidth);
92             scaleY = height / (imageHeight - cropFactor * imageHeight);
93             scale = Math.min(Math.min(scaleX, scaleY), 1);
94         }
95     
96         thumbWidth = (int) (imageWidth * scale);
97         thumbHeight = (int) (imageHeight * scale);
98         
99         BufferedImage JavaDoc thumbImage = new BufferedImage JavaDoc(thumbWidth,
100                 thumbHeight, alpha
101                 ? BufferedImage.TYPE_INT_ARGB
102                 : BufferedImage.TYPE_INT_RGB);
103
104         Graphics2D JavaDoc graphics2D = thumbImage.createGraphics();
105         graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
106             RenderingHints.VALUE_INTERPOLATION_BICUBIC);
107
108         graphics2D.drawImage(originalImage, 0, 0, thumbWidth, thumbHeight, null);
109         
110         int x = Math.max((thumbWidth - width) / 2, 0);
111         int y = Math.max((thumbHeight - height) / 2, 0);
112         if (x > 0 || y > 0) {
113             thumbWidth = Math.min(thumbWidth, width);
114             thumbHeight = Math.min(thumbHeight, height);
115             thumbImage = thumbImage.getSubimage(x, y, thumbWidth, thumbHeight);
116         }
117         
118         writeImage(thumbImage, dest);
119     }
120
121     private BufferedImage JavaDoc readImage(InputStream JavaDoc in) throws IOException JavaDoc {
122         try {
123             return ImageIO.read(in);
124         }
125         finally {
126             try {
127                 in.close();
128             }
129             catch (Exception JavaDoc e) {
130             }
131         }
132     }
133     
134     private void writeImage(RenderedImage JavaDoc im, File JavaDoc dest) throws IOException JavaDoc {
135         ImageWriter JavaDoc writer = null;
136         ImageOutputStream JavaDoc ios = null;
137         try {
138             String JavaDoc formatName = FormatUtils.getExtension(dest.getName()).toLowerCase();
139             if (!formatName.equals(FORMAT_JPG) && !formatName.equals(FORMAT_PNG)) {
140                 formatName = FORMAT_JPG;
141             }
142             Iterator JavaDoc it = ImageIO.getImageWritersByFormatName(formatName);
143             if (it.hasNext()) {
144                 writer = (ImageWriter JavaDoc) it.next();
145             }
146             Assert.notNull(writer, "No ImageWriter available for format "
147                     + formatName);
148             
149             ios = ImageIO.createImageOutputStream(dest);
150             writer.setOutput(ios);
151             
152             ImageWriteParam JavaDoc iwparam = null;
153             
154             if (formatName.equals(FORMAT_JPG)) {
155                 iwparam = new JPEGImageWriteParam JavaDoc(Locale.getDefault());
156                 iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
157                 iwparam.setCompressionQuality(1);
158             }
159             
160             writer.write(null, new IIOImage JavaDoc(im, null, null), iwparam);
161             ios.flush();
162         }
163         finally {
164             if (writer != null) {
165                 writer.dispose();
166             }
167             if (ios != null) {
168                 ios.close();
169             }
170         }
171     }
172
173 }
174
Popular Tags