001: /*
002: * MeshCMS - A simple CMS based on SiteMesh
003: * Copyright (C) 2004-2007 Luciano Vernaschi
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * as published by the Free Software Foundation; either version 2
008: * of the License, or (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: *
019: * You can contact the author at http://www.cromoteca.com
020: * and at info@cromoteca.com
021: */
022:
023: package org.meshcms.webui;
024:
025: import java.awt.*;
026: import java.awt.image.*;
027: import java.io.*;
028: import javax.imageio.*;
029: import org.meshcms.core.*;
030:
031: public class FileManagerThumbnail extends AbstractThumbnail {
032: /**
033: * Width of the thumbnail.
034: */
035: public static final int THUMB_WIDTH = 108;
036:
037: /**
038: * Height of the thumbnail.
039: */
040: public static final int THUMB_HEIGHT = 120;
041:
042: /**
043: * Width and height of the image in the thumbnail.
044: */
045: public static final int THUMB_SIZE = 100;
046:
047: private boolean highQuality;
048:
049: public String getSuggestedFileName() {
050: return highQuality ? "filemanager_hq.jpg" : "filemanager.jpg";
051: }
052:
053: protected boolean createThumbnail(File imageFile, File thumbnailFile) {
054: BufferedImage image;
055:
056: try {
057: image = ImageIO.read(imageFile);
058: } catch (Exception ex) {
059: return false;
060: }
061:
062: if (image == null || image.getWidth() < 1) {
063: return false;
064: }
065:
066: BufferedImage thumb = new BufferedImage(THUMB_WIDTH,
067: THUMB_HEIGHT, BufferedImage.TYPE_INT_RGB);
068: Graphics2D g = (Graphics2D) thumb.getGraphics();
069: g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
070: RenderingHints.VALUE_ANTIALIAS_ON);
071:
072: g.setColor(Color.WHITE);
073: g.fillRect(0, 0, THUMB_WIDTH, THUMB_HEIGHT);
074: g.setColor(DEFAULT_BORDER_COLOR);
075: g.drawRect(0, 0, THUMB_WIDTH - 1, THUMB_WIDTH - 1);
076: g.fillRect(0, THUMB_WIDTH, THUMB_WIDTH, THUMB_HEIGHT
077: - THUMB_WIDTH);
078:
079: int w = image.getWidth();
080: int h = image.getHeight();
081: int kb = (int) (imageFile.length() / 1024L) + 1;
082: String label = w + "x" + h + ", " + kb + "KB";
083:
084: g.setColor(Color.BLACK);
085: int fontSize = 12;
086: int labelSize;
087: Font font;
088: FontMetrics fm;
089:
090: do {
091: font = new Font("sansserif", Font.PLAIN, --fontSize);
092: fm = g.getFontMetrics(font);
093: labelSize = fm.stringWidth(label);
094: } while (labelSize > THUMB_SIZE);
095:
096: g.setFont(font);
097:
098: int ix, iy, w0, h0;
099: ix = iy = (THUMB_WIDTH - THUMB_SIZE) / 2;
100:
101: if (w <= THUMB_SIZE && h <= THUMB_SIZE) {
102: w0 = w;
103: h0 = h;
104: ix += (THUMB_SIZE - w0) / 2;
105: iy += (THUMB_SIZE - h0) / 2;
106: } else if (w > h) {
107: w0 = THUMB_SIZE;
108: h0 = w0 * h / w;
109: iy += (THUMB_SIZE - h0) / 2;
110: } else {
111: h0 = THUMB_SIZE;
112: w0 = h0 * w / h;
113: ix += (THUMB_SIZE - w0) / 2;
114: }
115:
116: w0 = Math.max(w0, 1);
117: h0 = Math.max(h0, 1);
118:
119: AbstractThumbnail.drawResizedImage(g, image, ix, iy, w0, h0,
120: highQuality);
121: image.flush();
122:
123: ix = (THUMB_WIDTH - labelSize) / 2;
124: iy = THUMB_HEIGHT - fm.getDescent();
125: g.drawString(label, ix, iy);
126:
127: OutputStream os = null;
128:
129: try {
130: os = new BufferedOutputStream(new FileOutputStream(
131: thumbnailFile));
132: ImageIO.write(thumb, "jpeg", os);
133: } catch (IOException ex) {
134: ex.printStackTrace();
135: return false;
136: } finally {
137: thumb.flush();
138: g.dispose();
139:
140: if (os != null) {
141: try {
142: os.close();
143: } catch (IOException ex) {
144: }
145: }
146: }
147:
148: return true;
149: }
150:
151: /**
152: * Returns the quality setting.
153: */
154: public boolean isHighQuality() {
155: return highQuality;
156: }
157:
158: /**
159: * Enables or disables better quality for image resizing.
160: */
161: public void setHighQuality(boolean highQuality) {
162: this.highQuality = highQuality;
163: }
164: }
|