KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jresearch > gossip > util > PictureGenerator


1 /*
2  * $$Id: PictureGenerator.java,v 1.3 2005/06/07 12:32:27 bel70 Exp $$
3  *
4  * ***** BEGIN LICENSE BLOCK *****
5  * The contents of this file are subject to the Mozilla Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License
8  * at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific language governing rights and
13  * limitations under the License.
14  *
15  * The Original Code is JGossip forum code.
16  *
17  * The Initial Developer of the Original Code is the JResearch, Org.
18  * Portions created by the Initial Developer are Copyright (C) 2004
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Dmitry Belov <bel@jresearch.org>
23  *
24  * ***** END LICENSE BLOCK ***** */

25
26 package org.jresearch.gossip.util;
27
28 import java.awt.Color JavaDoc;
29 import java.awt.Font JavaDoc;
30 import java.awt.GradientPaint JavaDoc;
31 import java.awt.Graphics2D JavaDoc;
32 import java.awt.RenderingHints JavaDoc;
33 import java.awt.geom.AffineTransform JavaDoc;
34 import java.awt.image.AffineTransformOp JavaDoc;
35 import java.awt.image.BufferedImage JavaDoc;
36 import java.awt.image.RenderedImage JavaDoc;
37 import java.io.File JavaDoc;
38 import java.io.FileNotFoundException JavaDoc;
39 import java.io.FileOutputStream JavaDoc;
40 import java.io.IOException JavaDoc;
41 import java.io.OutputStream JavaDoc;
42
43 import javax.imageio.ImageIO JavaDoc;
44 import javax.servlet.ServletOutputStream JavaDoc;
45
46 import org.jresearch.gossip.IConst;
47 import org.jresearch.gossip.configuration.Configurator;
48 import org.jresearch.gossip.exception.SystemException;
49
50 import com.sun.image.codec.jpeg.JPEGCodec;
51 import com.sun.image.codec.jpeg.JPEGImageEncoder;
52
53 /**
54  * Class <code>PictureGenerator</code> singletone for generation picture for
55  * sign up screen.
56  *
57  * @author <code>$Author: bel70 $</code>
58  * @version <code>$Revision: 1.3 $</code>
59  */

60 public class PictureGenerator {
61
62     private static PictureGenerator instance = null;
63
64     private BufferedImage JavaDoc image;
65
66     private Graphics2D JavaDoc g;
67
68     private GradientPaint JavaDoc gradient;
69
70     private int height = 30;
71
72     private int width = 200;
73
74     private int grid = (int) (height / 3.1);
75
76     private int maxH = 150;
77
78     private int maxW = 150;
79
80     private int thumbH = 150;
81
82     private int thumbW = 150;
83
84     private static Object JavaDoc lock = new Object JavaDoc();
85
86     /**
87      * Method <code>getInstance</code> return generator instance
88      *
89      * @return generator instance
90      * @throws <code>SteedException</code>
91      */

92     public static PictureGenerator getInstance() throws SystemException {
93
94         if (instance == null) {
95             synchronized (lock) {
96                 if (instance == null) {
97                     instance = new PictureGenerator();
98                 }
99             }
100         }
101
102         return instance;
103     }
104
105     private PictureGenerator() throws SystemException {
106
107         try {
108             Configurator config = Configurator.getInstance();
109             int newHeight = config
110                     .getInt(IConst.MISC.REGISTRATION_PICTURE_HEIGHT);
111             int newWidth = config
112                     .getInt(IConst.MISC.REGISTRATION_PICTURE_WIDTH);
113             int newGrid = config.getInt(IConst.MISC.REGISTRATION_PICTURE_GRID);
114
115             int newMaxH = config.getInt(IConst.MISC.AVATAR_HEIGHT);
116             int newMaxW = config.getInt(IConst.MISC.AVATAR_WIDTH);
117
118             int newThunbH = config.getInt(IConst.MISC.THUMBNAIL_HEIGHT);
119             int newThunbW = config.getInt(IConst.MISC.THUMBNAIL_WIDTH);
120
121             maxH = newMaxH;
122             maxW = newMaxW;
123
124             thumbH = newThunbH;
125             thumbW = newThunbW;
126
127             height = newHeight;
128             width = newWidth;
129             grid = newGrid;
130         } catch (Throwable JavaDoc e) {
131
132         }
133
134         image = new BufferedImage JavaDoc(width, height, BufferedImage.TYPE_INT_RGB);
135         g = image.createGraphics();
136         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
137                 RenderingHints.VALUE_ANTIALIAS_ON);
138         g.setFont(new Font JavaDoc("Dialog", Font.BOLD, (int) (height * 0.9)));
139         gradient = new GradientPaint JavaDoc(0, 0, Color.WHITE, width - 1, height - 1,
140                 Color.GRAY);
141     }
142
143     /**
144      * Method <code>getHeight</code> return picture height
145      *
146      * @return height
147      */

148     public int getHeight() {
149         return height;
150     }
151
152     /**
153      * Method <code>getWidth</code> return picture width
154      *
155      * @return picture width
156      */

157     public int getWidth() {
158         return width;
159     }
160
161     /**
162      * Method <code>generatePicture</code> write generated picture into given
163      * stream
164      *
165      * @param <code>label</code> label to write on picture
166      * @param <code>os</code> stream to write picture
167      * @throws <code>IOException</code>
168      */

169     public void generatePicture(String JavaDoc label, OutputStream JavaDoc out)
170             throws IOException JavaDoc {
171
172         /* background */
173         g.setPaint(gradient);
174         g.fillRect(0, 0, width - 1, height - 1);
175         /* label */
176         g.setColor(Color.BLACK);
177         g.drawString(label,
178                 (width - g.getFontMetrics().stringWidth(label)) / 2,
179                 (int) (height * 0.85));
180
181         /* grid */
182         /* horizontal */
183         g.setColor(Color.BLACK);
184         for (int y = (int) (grid / 2); y < height; y += grid) {
185             g.drawLine(0, y, width, y);
186         }
187
188         /* vertical */
189         for (int x = (int) (grid / 2); x < width; x += grid) {
190             g.drawLine(x, 0, x, height);
191         }
192         ImageIO.write((RenderedImage JavaDoc) image, "jpeg", out);
193     }
194
195     /**
196      * @param oldImage
197      * @param os
198      * @throws IOException
199      */

200     public void prepareImage(BufferedImage JavaDoc img, OutputStream JavaDoc out)
201             throws IOException JavaDoc {
202         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
203
204         int h = img.getHeight();
205         int w = img.getWidth();
206
207         if (h > this.maxH || w > this.maxW) {
208             int fullH = h;
209             int fullW = w;
210             double d = (double) h / (double) w;
211             if (h > this.maxH) {
212                 h = this.maxH;
213                 w = (int) Math.round(h / d);
214             }
215             if (w > this.maxW) {
216                 w = this.maxW;
217                 h = (int) Math.round(w * d);
218             }
219
220             AffineTransform JavaDoc xform = AffineTransform.getScaleInstance((double) h
221                     / (double) fullH, (double) w / (double) fullW);
222             AffineTransformOp JavaDoc op = new AffineTransformOp JavaDoc(xform,
223                     AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
224             BufferedImage JavaDoc newImage = new BufferedImage JavaDoc(w, h,
225                     BufferedImage.TYPE_INT_RGB);
226             Graphics2D JavaDoc gr = newImage.createGraphics();
227             int top = 0;
228             int left = 0;
229             gr.drawImage(img, op, left, top);
230             ImageIO.write((RenderedImage JavaDoc) newImage, "jpeg", out);
231         } else {
232             ImageIO.write((RenderedImage JavaDoc) img, "jpeg", out);
233         }
234
235     }
236
237     /**
238      * @param img
239      * @param out
240      * @throws IOException
241      */

242     public void prepareImageThumbnail(BufferedImage JavaDoc img, ServletOutputStream JavaDoc out)
243             throws IOException JavaDoc {
244         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
245
246         int h = img.getHeight();
247         int w = img.getWidth();
248
249         int fullH = h;
250         int fullW = w;
251         double d = (double) h / (double) w;
252         if (h > this.thumbH) {
253             h = this.thumbH;
254             w = (int) Math.round(h / d);
255         }
256         if (w > this.thumbW) {
257             w = this.thumbW;
258             h = (int) Math.round(w * d);
259         }
260
261         AffineTransform JavaDoc xform = AffineTransform.getScaleInstance((double) h
262                 / (double) fullH, (double) w / (double) fullW);
263         AffineTransformOp JavaDoc op = new AffineTransformOp JavaDoc(xform,
264                 AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
265         BufferedImage JavaDoc newImage = new BufferedImage JavaDoc(this.thumbW, this.thumbH,
266                 BufferedImage.TYPE_INT_RGB);
267         Graphics2D JavaDoc gr = newImage.createGraphics();
268         gr.setColor(Color.BLACK);
269         g.fillRect(0, 0, this.thumbH - 1, this.thumbW - 1);
270         int top = (this.thumbH - h) / 2;
271         int left = (this.thumbW - w) / 2;
272         gr.drawImage(img, op, left, top);
273         ImageIO.write((RenderedImage JavaDoc) newImage, "jpeg", out);
274
275     }
276
277     public static void main(String JavaDoc[] args) {// for tests only
278
String JavaDoc label = "PictureGeneratorTest" + System.currentTimeMillis()
279                 + ".jpg";
280         File JavaDoc out = new File JavaDoc(label);
281         try {
282             getInstance().generatePicture(label, new FileOutputStream JavaDoc(out));
283         } catch (SystemException e) {
284             e.printStackTrace();
285         } catch (FileNotFoundException JavaDoc e) {
286             e.printStackTrace();
287         } catch (IOException JavaDoc e) {
288             e.printStackTrace();
289         }
290     }
291 }
292
Popular Tags