1 14 package wingset; 15 16 import com.sun.image.codec.jpeg.JPEGCodec; 17 import com.sun.image.codec.jpeg.JPEGImageEncoder; 18 import org.wings.SFileIcon; 19 import org.wings.SIcon; 20 21 import javax.swing.*; 22 import java.awt.*; 23 import java.awt.geom.AffineTransform ; 24 import java.awt.image.AffineTransformOp ; 25 import java.awt.image.BufferedImage ; 26 import java.io.File ; 27 import java.io.FileOutputStream ; 28 import java.io.IOException ; 29 30 39 public class FaceGenerator { 40 41 int width = 340; 42 43 int height = 450; 44 45 46 public FaceGenerator() { 47 48 } 49 50 51 Faces.Face generateFace(SIcon f, File dir, String name) throws IOException { 52 ImageIcon i = new ImageIcon(f.getURL().toString()); 53 return generateFace(i, dir, name); 54 } 55 56 Faces.Face generateFace(ImageIcon f, File dir, String name) throws IOException { 57 int width = f.getIconWidth(); 58 int height = f.getIconHeight(); 59 60 BufferedImage bi = new BufferedImage (width, height, 61 BufferedImage.TYPE_INT_RGB); 62 63 Graphics2D biContext = bi.createGraphics(); 64 biContext.drawImage(f.getImage(), 0, 0, null); 65 66 return generateFace(bi, dir, name); 67 } 68 69 Faces.Face generateFace(BufferedImage f, File dir, String name) throws IOException { 70 Faces.Face result = new Faces.Face(); 71 72 73 AffineTransformOp scaler = 74 new AffineTransformOp (AffineTransform.getScaleInstance(width / (double) f.getWidth(), 75 height / (double) f.getHeight()), 76 null); 77 78 f = scaler.filter(f, null); 79 80 result.hair = 81 new SFileIcon(storeImage(f.getSubimage(0, 0, width, height / 3), 82 dir, 83 name + "_hair")); 84 85 result.eyes = 86 new SFileIcon(storeImage(f.getSubimage(0, height / 3, width, height / 3), 87 dir, 88 name + "_eyes")); 89 90 result.mouth = 91 new SFileIcon(storeImage(f.getSubimage(0, 2 * height / 3, width, height / 3), 92 dir, 93 name + "_mouth")); 94 95 return result; 96 } 97 98 public File storeImage(BufferedImage image, File dir, String name) throws IOException { 99 100 if (!dir.exists()) { 101 throw new IOException ("Directory " + dir + " does not exist, cannot write file"); 102 } 103 104 if (!dir.canWrite()) { 105 throw new IOException ("Directory " + dir + " is not writable, cannot write file"); 106 } 107 108 FileOutputStream out = null; 109 110 File file = new File (dir, name + ".jpeg"); 111 112 out = new FileOutputStream (file); 113 JPEGImageEncoder encoder = 114 JPEGCodec.createJPEGEncoder(out); 115 encoder.encode(image); 116 117 out.close(); 118 119 return file; 120 } 121 122 public static void main(String [] args) throws IOException { 123 124 FaceGenerator gen = new FaceGenerator(); 125 126 127 gen.generateFace(new ImageIcon(args[0]), new File (args[1]), args[2]); 128 } 129 130 } 132 | Popular Tags |