KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > app > dbimage > Test


1 /* Copyright 2003 Quadcap Software. All rights reserved.
2  *
3  * This software is distributed under the Quadcap Free Software License.
4  * This software may be used or modified for any purpose, personal or
5  * commercial. Open Source redistributions are permitted. Commercial
6  * redistribution of larger works derived from, or works which bundle
7  * this software requires a "Commercial Redistribution License"; see
8  * http://www.quadcap.com/purchase.
9  *
10  * Redistributions qualify as "Open Source" under one of the following terms:
11  *
12  * Redistributions are made at no charge beyond the reasonable cost of
13  * materials and delivery.
14  *
15  * Redistributions are accompanied by a copy of the Source Code or by an
16  * irrevocable offer to provide a copy of the Source Code for up to three
17  * years at the cost of materials and delivery. Such redistributions
18  * must allow further use, modification, and redistribution of the Source
19  * Code under substantially the same terms as this license.
20  *
21  * Redistributions of source code must retain the copyright notices as they
22  * appear in each source code file, these license terms, and the
23  * disclaimer/limitation of liability set forth as paragraph 6 below.
24  *
25  * Redistributions in binary form must reproduce this Copyright Notice,
26  * these license terms, and the disclaimer/limitation of liability set
27  * forth as paragraph 6 below, in the documentation and/or other materials
28  * provided with the distribution.
29  *
30  * The Software is provided on an "AS IS" basis. No warranty is
31  * provided that the Software is free of defects, or fit for a
32  * particular purpose.
33  *
34  * Limitation of Liability. Quadcap Software shall not be liable
35  * for any damages suffered by the Licensee or any third party resulting
36  * from use of the Software.
37  */

38
39 package com.quadcap.app.dbimage;
40
41 import java.io.BufferedOutputStream JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.io.File JavaDoc;
44 import java.io.OutputStream JavaDoc;
45 import java.io.FileOutputStream JavaDoc;
46 import java.io.PrintStream JavaDoc;
47 import java.util.Vector JavaDoc;
48
49 import java.awt.Image JavaDoc;
50 import java.awt.Graphics2D JavaDoc;
51 import java.awt.geom.AffineTransform JavaDoc;
52 import java.awt.image.BufferedImage JavaDoc;
53
54 import javax.swing.ImageIcon JavaDoc;
55 import com.sun.image.codec.jpeg.JPEGCodec;
56 import com.sun.image.codec.jpeg.JPEGImageEncoder;
57
58 public class Test {
59     int maxDim = 400;
60
61     public Test(int m) { maxDim = m; }
62     
63     public static void main(String JavaDoc[] args) {
64         try {
65             int maxDim = Integer.parseInt(args[2]);
66             new Test(maxDim).handleDir(new File JavaDoc(args[0]), new File JavaDoc(args[1]));
67         } catch (Throwable JavaDoc t) {
68             t.printStackTrace(System.err);
69         }
70         System.exit(0);
71     }
72
73     public void error(String JavaDoc s) {
74         System.err.println(s);
75     }
76
77     public void handleDir(File JavaDoc s, File JavaDoc d) throws Exception JavaDoc {
78         if (!s.exists()) {
79             error("Can't open source file: " + s.getAbsolutePath());
80             return;
81         }
82         if (!s.isDirectory()) {
83             error("Source isn't a directory: " + s.getAbsolutePath());
84             return;
85         }
86         
87         if (d.exists() && !d.isDirectory()) {
88             if (!d.delete()) {
89                 error("Can't delete previous non-directory: " +
90                       d.getAbsolutePath());
91                 return;
92             }
93         }
94         if (!d.isDirectory() && !d.mkdirs()) {
95             error("Can't create destination directory: " +
96                   d.getAbsolutePath());
97             return;
98         }
99
100         FileOutputStream JavaDoc os = new FileOutputStream JavaDoc(new File JavaDoc(d, "index.html"));
101         BufferedOutputStream JavaDoc bos = new BufferedOutputStream JavaDoc(os);
102         PrintStream JavaDoc ps = new PrintStream JavaDoc(bos);
103         ps.println("<html><head></head><body>");
104
105         Vector JavaDoc v = new Vector JavaDoc();
106         File JavaDoc[] files = s.listFiles();
107         if (files != null) for (int i = 0; i < files.length; i++) {
108             File JavaDoc sf = files[i];
109             File JavaDoc df = new File JavaDoc(d, sf.getName());
110             if (sf.isDirectory()) {
111                 handleDir(sf, df);
112                 ps.println("<a HREF=\"" + sf.getName() + "/index.html\">" +
113                            sf.getName() + "</a>");
114             } else {
115                 v.addElement(sf.getName());
116                 doResize(sf.getAbsolutePath(), df.getAbsolutePath());
117             }
118         }
119         for (int i = 0; i < v.size(); i++) {
120             ps.println("<img SRC=\"" + v.elementAt(i) + "\">");
121         }
122         ps.println("</body></html>");
123         ps.flush();
124         os.close();
125     }
126
127     void doResize(String JavaDoc inFile, String JavaDoc outFile) throws Exception JavaDoc {
128         System.out.println(" " + inFile);
129         Image JavaDoc inImage = new ImageIcon JavaDoc(inFile).getImage();
130
131         
132          // Determine the scale.
133
double scale = (double)maxDim/(double)inImage.getHeight(null);
134
135         if (inImage.getWidth(null) > inImage.getHeight(null)) {
136             scale = (double)maxDim/(double)inImage.getWidth(null);
137         }
138
139         // Determine size of new image.
140
// One of them hould equal maxDim.
141
int scaledW = (int)(scale*inImage.getWidth(null));
142         int scaledH = (int)(scale*inImage.getHeight(null));
143
144         // Create an image buffer in which to paint on.
145
BufferedImage JavaDoc outImage = new BufferedImage JavaDoc(scaledW, scaledH,
146                                                    BufferedImage.TYPE_INT_RGB);
147             
148         // Set the scale.
149
AffineTransform JavaDoc tx = new AffineTransform JavaDoc();
150
151         // If the image is smaller than the desired image size,
152
// don't bother scaling.
153
if (scale < 1.0d) {
154             tx.scale(scale, scale);
155         }
156
157         // Paint image.
158
Graphics2D JavaDoc g2d = outImage.createGraphics();
159         g2d.drawImage(inImage, tx, null);
160         g2d.dispose();
161
162         // JPEG-encode the image and write the response
163
FileOutputStream JavaDoc os = new FileOutputStream JavaDoc(outFile);
164         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
165         encoder.encode(outImage);
166         os.close();
167     }
168 }
169
Popular Tags