KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > wingset > FaceGenerator


1 /*
2  * $Id: FaceGenerator.java,v 1.5 2005/05/20 10:27:04 blueshift Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

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 JavaDoc;
24 import java.awt.image.AffineTransformOp JavaDoc;
25 import java.awt.image.BufferedImage JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29
30 /**
31  * FaceGenerator.java
32  * <p/>
33  * <p/>
34  * Created: Mon Mar 18 15:36:32 2002
35  *
36  * @author <a HREF="mailto:armin.haaf@mercatis.de">Armin Haaf</a>
37  * @version $Revision: 1.5 $
38  */

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 JavaDoc dir, String JavaDoc name) throws IOException JavaDoc {
52         ImageIcon i = new ImageIcon(f.getURL().toString());
53         return generateFace(i, dir, name);
54     }
55
56     Faces.Face generateFace(ImageIcon f, File JavaDoc dir, String JavaDoc name) throws IOException JavaDoc {
57         int width = f.getIconWidth();
58         int height = f.getIconHeight();
59
60         BufferedImage JavaDoc bi = new BufferedImage JavaDoc(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 JavaDoc f, File JavaDoc dir, String JavaDoc name) throws IOException JavaDoc {
70         Faces.Face result = new Faces.Face();
71
72
73         AffineTransformOp JavaDoc scaler =
74                 new AffineTransformOp JavaDoc(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 JavaDoc storeImage(BufferedImage JavaDoc image, File JavaDoc dir, String JavaDoc name) throws IOException JavaDoc {
99
100         if (!dir.exists()) {
101             throw new IOException JavaDoc("Directory " + dir + " does not exist, cannot write file");
102         }
103
104         if (!dir.canWrite()) {
105             throw new IOException JavaDoc("Directory " + dir + " is not writable, cannot write file");
106         }
107
108         FileOutputStream JavaDoc out = null;
109
110         File JavaDoc file = new File JavaDoc(dir, name + ".jpeg");
111
112         out = new FileOutputStream JavaDoc(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 JavaDoc[] args) throws IOException JavaDoc {
123
124         FaceGenerator gen = new FaceGenerator();
125
126
127         gen.generateFace(new ImageIcon(args[0]), new File JavaDoc(args[1]), args[2]);
128     }
129
130 }// FaceGenerator
131

132
Popular Tags