KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > meshcms > webui > FileManagerThumbnail


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.webui;
24
25 import java.awt.*;
26 import java.awt.image.*;
27 import java.io.*;
28 import javax.imageio.*;
29 import org.meshcms.core.*;
30
31 public class FileManagerThumbnail extends AbstractThumbnail {
32   /**
33    * Width of the thumbnail.
34    */

35   public static final int THUMB_WIDTH = 108;
36
37   /**
38    * Height of the thumbnail.
39    */

40   public static final int THUMB_HEIGHT = 120;
41
42   /**
43    * Width and height of the image in the thumbnail.
44    */

45   public static final int THUMB_SIZE = 100;
46
47   private boolean highQuality;
48
49   public String JavaDoc getSuggestedFileName() {
50     return highQuality ? "filemanager_hq.jpg" : "filemanager.jpg";
51   }
52
53   protected boolean createThumbnail(File imageFile, File thumbnailFile) {
54     BufferedImage image;
55
56     try {
57       image = ImageIO.read(imageFile);
58     } catch (Exception JavaDoc ex) {
59       return false;
60     }
61
62     if (image == null || image.getWidth() < 1) {
63       return false;
64     }
65
66     BufferedImage thumb = new BufferedImage(THUMB_WIDTH, THUMB_HEIGHT,
67       BufferedImage.TYPE_INT_RGB);
68     Graphics2D g = (Graphics2D) thumb.getGraphics();
69     g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
70                        RenderingHints.VALUE_ANTIALIAS_ON);
71
72     g.setColor(Color.WHITE);
73     g.fillRect(0, 0, THUMB_WIDTH, THUMB_HEIGHT);
74     g.setColor(DEFAULT_BORDER_COLOR);
75     g.drawRect(0, 0, THUMB_WIDTH - 1, THUMB_WIDTH - 1);
76     g.fillRect(0, THUMB_WIDTH, THUMB_WIDTH, THUMB_HEIGHT - THUMB_WIDTH);
77
78     int w = image.getWidth();
79     int h = image.getHeight();
80     int kb = (int) (imageFile.length() / 1024L) + 1;
81     String JavaDoc label = w + "x" + h + ", " + kb + "KB";
82
83     g.setColor(Color.BLACK);
84     int fontSize = 12;
85     int labelSize;
86     Font font;
87     FontMetrics fm;
88
89     do {
90       font = new Font("sansserif", Font.PLAIN, --fontSize);
91       fm = g.getFontMetrics(font);
92       labelSize = fm.stringWidth(label);
93     } while (labelSize > THUMB_SIZE);
94
95     g.setFont(font);
96
97     int ix, iy, w0, h0;
98     ix = iy = (THUMB_WIDTH - THUMB_SIZE) / 2;
99
100     if (w <= THUMB_SIZE && h <= THUMB_SIZE) {
101       w0 = w;
102       h0 = h;
103       ix += (THUMB_SIZE - w0) / 2;
104       iy += (THUMB_SIZE - h0) / 2;
105     } else if (w > h) {
106       w0 = THUMB_SIZE;
107       h0 = w0 * h / w;
108       iy += (THUMB_SIZE - h0) / 2;
109     } else {
110       h0 = THUMB_SIZE;
111       w0 = h0 * w / h;
112       ix += (THUMB_SIZE - w0) / 2;
113     }
114
115     w0 = Math.max(w0, 1);
116     h0 = Math.max(h0, 1);
117
118     AbstractThumbnail.drawResizedImage(g, image, ix, iy, w0, h0, highQuality);
119     image.flush();
120
121     ix = (THUMB_WIDTH - labelSize) / 2;
122     iy = THUMB_HEIGHT - fm.getDescent();
123     g.drawString(label, ix, iy);
124
125     OutputStream os = null;
126
127     try {
128       os = new BufferedOutputStream(new FileOutputStream(thumbnailFile));
129       ImageIO.write(thumb, "jpeg", os);
130     } catch (IOException ex) {
131       ex.printStackTrace();
132       return false;
133     } finally {
134       thumb.flush();
135       g.dispose();
136
137       if (os != null) {
138         try {
139           os.close();
140         } catch (IOException ex) {}
141       }
142     }
143
144     return true;
145   }
146
147   /**
148    * @return the quality setting.
149    */

150   public boolean isHighQuality() {
151     return highQuality;
152   }
153
154   /**
155    * Enables or disables better quality for image resizing.
156    */

157   public void setHighQuality(boolean highQuality) {
158     this.highQuality = highQuality;
159   }
160 }
161
Popular Tags