KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.imageio.*;
29
30 public class GalleryThumbnail extends AbstractThumbnail {
31   /**
32    * Width and height of the thumbnail.
33    */

34   public static final int THUMB_SIZE = 96;
35
36   /**
37    * Width and height of the image in the thumbnail.
38    */

39   public static final int THUMB_IMAGE_SIZE = 94;
40
41   private boolean highQuality;
42
43   public String JavaDoc getSuggestedFileName() {
44     return highQuality ? "meshcms_hq_gallery.jpg" : "meshcms_gallery.jpg";
45   }
46
47   protected boolean createThumbnail(File imageFile, File thumbnailFile) {
48     BufferedImage image;
49
50     try {
51       image = ImageIO.read(imageFile);
52     } catch (Exception JavaDoc ex) {
53       return false;
54     }
55
56     if (image == null || image.getWidth() < 1) {
57       return false;
58     }
59
60     BufferedImage thumb = new BufferedImage(THUMB_SIZE, THUMB_SIZE,
61       BufferedImage.TYPE_INT_RGB);
62     Graphics2D g = (Graphics2D) thumb.getGraphics();
63     g.setColor(Color.WHITE);
64     g.fillRect(0, 0, THUMB_SIZE, THUMB_SIZE);
65
66     int w = image.getWidth();
67     int h = image.getHeight();
68     int ix, iy, w0, h0;
69     ix = iy = (THUMB_SIZE - THUMB_IMAGE_SIZE) / 2;
70
71     if (w <= THUMB_IMAGE_SIZE && h <= THUMB_IMAGE_SIZE) {
72       w0 = w;
73       h0 = h;
74       ix += (THUMB_IMAGE_SIZE - w0) / 2;
75       iy += (THUMB_IMAGE_SIZE - h0) / 2;
76     } else if (w > h) {
77       w0 = THUMB_IMAGE_SIZE;
78       h0 = w0 * h / w;
79       iy += (THUMB_IMAGE_SIZE - h0) / 2;
80     } else {
81       h0 = THUMB_IMAGE_SIZE;
82       w0 = h0 * w / h;
83       ix += (THUMB_IMAGE_SIZE - w0) / 2;
84     }
85
86     w0 = Math.max(w0, 1);
87     h0 = Math.max(h0, 1);
88
89     AbstractThumbnail.drawResizedImage(g, image, ix, iy, w0, h0, highQuality);
90     image.flush();
91     g.setColor(DEFAULT_BORDER_COLOR);
92     g.drawRect(0, 0, THUMB_SIZE - 1, THUMB_SIZE - 1);
93
94     OutputStream os = null;
95
96     try {
97       os = new BufferedOutputStream(new FileOutputStream(thumbnailFile));
98       ImageIO.write(thumb, "jpeg", os);
99     } catch (IOException ex) {
100       ex.printStackTrace();
101       return false;
102     } finally {
103       thumb.flush();
104       g.dispose();
105
106       if (os != null) {
107         try {
108           os.close();
109         } catch (IOException ex) {}
110       }
111     }
112
113     return true;
114   }
115
116   /**
117    * @return the quality setting.
118    */

119   public boolean isHighQuality() {
120     return highQuality;
121   }
122
123   /**
124    * Enables or disables better quality for image resizing.
125    */

126   public void setHighQuality(boolean highQuality) {
127     this.highQuality = highQuality;
128   }
129 }
130
Popular Tags