001: /* Copyright 2003 Quadcap Software. All rights reserved.
002: *
003: * This software is distributed under the Quadcap Free Software License.
004: * This software may be used or modified for any purpose, personal or
005: * commercial. Open Source redistributions are permitted. Commercial
006: * redistribution of larger works derived from, or works which bundle
007: * this software requires a "Commercial Redistribution License"; see
008: * http://www.quadcap.com/purchase.
009: *
010: * Redistributions qualify as "Open Source" under one of the following terms:
011: *
012: * Redistributions are made at no charge beyond the reasonable cost of
013: * materials and delivery.
014: *
015: * Redistributions are accompanied by a copy of the Source Code or by an
016: * irrevocable offer to provide a copy of the Source Code for up to three
017: * years at the cost of materials and delivery. Such redistributions
018: * must allow further use, modification, and redistribution of the Source
019: * Code under substantially the same terms as this license.
020: *
021: * Redistributions of source code must retain the copyright notices as they
022: * appear in each source code file, these license terms, and the
023: * disclaimer/limitation of liability set forth as paragraph 6 below.
024: *
025: * Redistributions in binary form must reproduce this Copyright Notice,
026: * these license terms, and the disclaimer/limitation of liability set
027: * forth as paragraph 6 below, in the documentation and/or other materials
028: * provided with the distribution.
029: *
030: * The Software is provided on an "AS IS" basis. No warranty is
031: * provided that the Software is free of defects, or fit for a
032: * particular purpose.
033: *
034: * Limitation of Liability. Quadcap Software shall not be liable
035: * for any damages suffered by the Licensee or any third party resulting
036: * from use of the Software.
037: */
038:
039: package com.quadcap.app.dbimage;
040:
041: import java.io.BufferedOutputStream;
042: import java.io.IOException;
043: import java.io.File;
044: import java.io.OutputStream;
045: import java.io.FileOutputStream;
046: import java.io.PrintStream;
047: import java.util.Vector;
048:
049: import java.awt.Image;
050: import java.awt.Graphics2D;
051: import java.awt.geom.AffineTransform;
052: import java.awt.image.BufferedImage;
053:
054: import javax.swing.ImageIcon;
055: import com.sun.image.codec.jpeg.JPEGCodec;
056: import com.sun.image.codec.jpeg.JPEGImageEncoder;
057:
058: public class Test {
059: int maxDim = 400;
060:
061: public Test(int m) {
062: maxDim = m;
063: }
064:
065: public static void main(String[] args) {
066: try {
067: int maxDim = Integer.parseInt(args[2]);
068: new Test(maxDim).handleDir(new File(args[0]), new File(
069: args[1]));
070: } catch (Throwable t) {
071: t.printStackTrace(System.err);
072: }
073: System.exit(0);
074: }
075:
076: public void error(String s) {
077: System.err.println(s);
078: }
079:
080: public void handleDir(File s, File d) throws Exception {
081: if (!s.exists()) {
082: error("Can't open source file: " + s.getAbsolutePath());
083: return;
084: }
085: if (!s.isDirectory()) {
086: error("Source isn't a directory: " + s.getAbsolutePath());
087: return;
088: }
089:
090: if (d.exists() && !d.isDirectory()) {
091: if (!d.delete()) {
092: error("Can't delete previous non-directory: "
093: + d.getAbsolutePath());
094: return;
095: }
096: }
097: if (!d.isDirectory() && !d.mkdirs()) {
098: error("Can't create destination directory: "
099: + d.getAbsolutePath());
100: return;
101: }
102:
103: FileOutputStream os = new FileOutputStream(new File(d,
104: "index.html"));
105: BufferedOutputStream bos = new BufferedOutputStream(os);
106: PrintStream ps = new PrintStream(bos);
107: ps.println("<html><head></head><body>");
108:
109: Vector v = new Vector();
110: File[] files = s.listFiles();
111: if (files != null)
112: for (int i = 0; i < files.length; i++) {
113: File sf = files[i];
114: File df = new File(d, sf.getName());
115: if (sf.isDirectory()) {
116: handleDir(sf, df);
117: ps.println("<a href=\"" + sf.getName()
118: + "/index.html\">" + sf.getName() + "</a>");
119: } else {
120: v.addElement(sf.getName());
121: doResize(sf.getAbsolutePath(), df.getAbsolutePath());
122: }
123: }
124: for (int i = 0; i < v.size(); i++) {
125: ps.println("<img src=\"" + v.elementAt(i) + "\">");
126: }
127: ps.println("</body></html>");
128: ps.flush();
129: os.close();
130: }
131:
132: void doResize(String inFile, String outFile) throws Exception {
133: System.out.println(" " + inFile);
134: Image inImage = new ImageIcon(inFile).getImage();
135:
136: // Determine the scale.
137: double scale = (double) maxDim
138: / (double) inImage.getHeight(null);
139:
140: if (inImage.getWidth(null) > inImage.getHeight(null)) {
141: scale = (double) maxDim / (double) inImage.getWidth(null);
142: }
143:
144: // Determine size of new image.
145: // One of them hould equal maxDim.
146: int scaledW = (int) (scale * inImage.getWidth(null));
147: int scaledH = (int) (scale * inImage.getHeight(null));
148:
149: // Create an image buffer in which to paint on.
150: BufferedImage outImage = new BufferedImage(scaledW, scaledH,
151: BufferedImage.TYPE_INT_RGB);
152:
153: // Set the scale.
154: AffineTransform tx = new AffineTransform();
155:
156: // If the image is smaller than the desired image size,
157: // don't bother scaling.
158: if (scale < 1.0d) {
159: tx.scale(scale, scale);
160: }
161:
162: // Paint image.
163: Graphics2D g2d = outImage.createGraphics();
164: g2d.drawImage(inImage, tx, null);
165: g2d.dispose();
166:
167: // JPEG-encode the image and write the response
168: FileOutputStream os = new FileOutputStream(outFile);
169: JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
170: encoder.encode(outImage);
171: os.close();
172: }
173: }
|