KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > meshcms > core > ResizedThumbnail


1 /*
2  * MeshCMS - A simple CMS based on SiteMesh
3  * Copyright (C) 2004-2007 Luciano Vernaschi
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * You can contact the author at http://www.cromoteca.com
20  * and at info@cromoteca.com
21  */

22
23 package org.meshcms.core;
24
25 import java.awt.*;
26 import java.awt.image.*;
27 import java.io.*;
28 import java.util.regex.*;
29 import javax.imageio.*;
30 import org.meshcms.util.*;
31
32 /**
33  * Creates a thumbnail by simply resizing the image. The way the thumbnail is
34  * created can be controlled with some parameters (see the various setters
35  * for details).
36  */

37 public class ResizedThumbnail extends AbstractThumbnail {
38   /**
39    * Used to scale the image maintaining proportions.
40    */

41   public static final String JavaDoc MODE_SCALE = "scale";
42
43   /**
44    * Used to scale the image and crop it to have a thumbnail of the required size.
45    */

46   public static final String JavaDoc MODE_CROP = "crop";
47
48   /**
49    * Like {@link #MODE_SCALE}, but adds a padding to reach the required size.
50    */

51   public static final String JavaDoc MODE_PADDING = "padding";
52
53   /**
54    * Used to resize the image without maintaining proportions.
55    */

56   public static final String JavaDoc MODE_STRETCH = "stretch";
57
58   /**
59    * Default padding color.
60    */

61   public static final String JavaDoc WHITE = "ffffff";
62
63   /**
64    * Default thumbnail size.
65    */

66   public static final int DEFAULT_SIZE = 100;
67
68   private static Matcher hexColorMatcher =
69       Pattern.compile("[ABCDEFabcdef\\d]{6}").matcher("");
70
71   private int width = -1;
72   private int height = -1;
73   private boolean highQuality = false;
74   private String JavaDoc mode = MODE_SCALE;
75   private String JavaDoc color = WHITE;
76
77   /**
78    * Creates a new instance with a default configuration. By default, the image
79    * is scaled to fit into a square of 100x100 pixels.
80    */

81   public ResizedThumbnail() {
82   }
83
84   /**
85    * @deprecated use default constructor and setters instead
86    */

87   public ResizedThumbnail(String JavaDoc width, String JavaDoc height) {
88     this.width = Utils.parseInt(width, -1);
89     this.height = Utils.parseInt(height, -1);
90   }
91
92   public String JavaDoc getSuggestedFileName() {
93     return "resized" + ("" + width + height + highQuality + mode +
94         color).hashCode() + ".jpg";
95   }
96
97   protected boolean createThumbnail(File imageFile, File thumbnailFile) {
98     BufferedImage image;
99
100     try {
101       image = ImageIO.read(imageFile);
102     } catch (Exception JavaDoc ex) {
103       return false;
104     }
105
106     if (image == null) {
107       return false;
108     }
109
110     int w = image.getWidth();
111     int h = image.getHeight();
112
113     if (w <= 0 || h <= 0) {
114       return false;
115     }
116
117     int reqW = width;
118     int reqH = height;
119     int w0 = w;
120     int h0 = h;
121
122     if (reqW < 1 && reqH < 1) {
123       reqW = reqH = DEFAULT_SIZE;
124     } else if (reqW < 1) {
125       reqW = w * reqH / h;
126     } else if (reqH < 1) {
127       reqH = h * reqW / w;
128     }
129
130     if (reqW > w && reqH > h) {
131       reqW = w;
132       reqH = h;
133     }
134
135     if (mode.equals(MODE_CROP)) {
136       if (w * reqH < h * reqW) {
137         w0 = reqW;
138         h0 = w0 * h / w;
139       } else {
140         h0 = reqH;
141         w0 = w * h0 / h;
142       }
143     } else if (mode.equals(MODE_STRETCH)) {
144       w0 = reqW;
145       h0 = reqH;
146     } else {
147       if (w * reqH < h * reqW) {
148         h0 = reqH;
149         w0 = w * h0 / h;
150       } else {
151         w0 = reqW;
152         h0 = w0 * h / w;
153       }
154
155       if (!mode.equals(MODE_PADDING)) {
156         reqW = w0;
157         reqH = h0;
158       }
159     }
160
161     BufferedImage thumb = new BufferedImage(reqW, reqH, BufferedImage.TYPE_INT_RGB);
162     Graphics g = thumb.getGraphics();
163     g.setColor(new Color(Integer.parseInt(color, 16)));
164     g.fillRect(0, 0, reqW, reqH);
165     AbstractThumbnail.drawResizedImage(g, image, (reqW - w0) / 2,
166         (reqH - h0) / 2, w0, h0, highQuality);
167     image.flush();
168
169     OutputStream os = null;
170
171     try {
172       os = new BufferedOutputStream(new FileOutputStream(thumbnailFile));
173       ImageIO.write(thumb, "jpeg", os);
174     } catch (IOException ex) {
175       ex.printStackTrace();
176       return false;
177     } finally {
178       thumb.flush();
179       g.dispose();
180
181       if (os != null) {
182         try {
183           os.close();
184         } catch (IOException ex) {}
185       }
186     }
187
188     return true;
189   }
190
191   /**
192    * @return the quality setting.
193    */

194   public boolean isHighQuality() {
195     return highQuality;
196   }
197
198   /**
199    * Enables or disables better quality for image resizing.
200    */

201   public void setHighQuality(boolean highQuality) {
202     this.highQuality = highQuality;
203   }
204
205   public int getWidth() {
206     return width;
207   }
208
209   /**
210    * Sets the maximum image width.
211    */

212   public void setWidth(int width) {
213     this.width = width;
214   }
215
216   public int getHeight() {
217     return height;
218   }
219
220   /**
221    * Sets the maximum image height.
222    */

223   public void setHeight(int height) {
224     this.height = height;
225   }
226
227   public String JavaDoc getMode() {
228     return mode;
229   }
230
231   /**
232    * Sets the scaling mode. Possible values are {@link #MODE_SCALE} (default),
233    * {@link #MODE_CROP}, {@link #MODE_PADDING} and {@link #MODE_STRETCH}.
234    */

235   public void setMode(String JavaDoc mode) {
236     if (mode == null) {
237       this.mode = MODE_SCALE;
238     } else {
239       mode = mode.trim().toLowerCase();
240
241       if (mode.equals(MODE_CROP) || mode.equals(MODE_PADDING) ||
242           mode.equals(MODE_SCALE) || mode.equals(MODE_STRETCH)) {
243         this.mode = mode;
244       } else {
245         throw new IllegalArgumentException JavaDoc("Unknown mode: " + mode);
246       }
247     }
248   }
249
250   public String JavaDoc getColor() {
251     return color;
252   }
253
254   /**
255    * Sets the padding color (used only when mode is {@link #MODE_PADDING}).
256    * The color must be supplied in hexadecimal format, with or without a #
257    * sign (e.g. #ffcc00 or 123ABC).
258    */

259   public void setColor(String JavaDoc color) {
260     if (color == null) {
261       this.color = WHITE;
262     } else {
263       if (hexColorMatcher.reset(color).find()) {
264         this.color = hexColorMatcher.group();
265       } else {
266         throw new IllegalArgumentException JavaDoc("Unknown color: " + color);
267       }
268     }
269   }
270 }
271
Popular Tags