1 38 39 package com.quadcap.app.dbimage; 40 41 import java.io.BufferedOutputStream ; 42 import java.io.IOException ; 43 import java.io.File ; 44 import java.io.OutputStream ; 45 import java.io.FileOutputStream ; 46 import java.io.PrintStream ; 47 import java.util.Vector ; 48 49 import java.awt.Image ; 50 import java.awt.Graphics2D ; 51 import java.awt.geom.AffineTransform ; 52 import java.awt.image.BufferedImage ; 53 54 import javax.swing.ImageIcon ; 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 [] args) { 64 try { 65 int maxDim = Integer.parseInt(args[2]); 66 new Test(maxDim).handleDir(new File (args[0]), new File (args[1])); 67 } catch (Throwable t) { 68 t.printStackTrace(System.err); 69 } 70 System.exit(0); 71 } 72 73 public void error(String s) { 74 System.err.println(s); 75 } 76 77 public void handleDir(File s, File d) throws Exception { 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 os = new FileOutputStream (new File (d, "index.html")); 101 BufferedOutputStream bos = new BufferedOutputStream (os); 102 PrintStream ps = new PrintStream (bos); 103 ps.println("<html><head></head><body>"); 104 105 Vector v = new Vector (); 106 File [] files = s.listFiles(); 107 if (files != null) for (int i = 0; i < files.length; i++) { 108 File sf = files[i]; 109 File df = new File (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 inFile, String outFile) throws Exception { 128 System.out.println(" " + inFile); 129 Image inImage = new ImageIcon (inFile).getImage(); 130 131 132 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 int scaledW = (int)(scale*inImage.getWidth(null)); 142 int scaledH = (int)(scale*inImage.getHeight(null)); 143 144 BufferedImage outImage = new BufferedImage (scaledW, scaledH, 146 BufferedImage.TYPE_INT_RGB); 147 148 AffineTransform tx = new AffineTransform (); 150 151 if (scale < 1.0d) { 154 tx.scale(scale, scale); 155 } 156 157 Graphics2D g2d = outImage.createGraphics(); 159 g2d.drawImage(inImage, tx, null); 160 g2d.dispose(); 161 162 FileOutputStream os = new FileOutputStream (outFile); 164 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os); 165 encoder.encode(outImage); 166 os.close(); 167 } 168 } 169 | Popular Tags |