1 25 26 package org.jresearch.gossip.util; 27 28 import java.awt.Color ; 29 import java.awt.Font ; 30 import java.awt.GradientPaint ; 31 import java.awt.Graphics2D ; 32 import java.awt.RenderingHints ; 33 import java.awt.geom.AffineTransform ; 34 import java.awt.image.AffineTransformOp ; 35 import java.awt.image.BufferedImage ; 36 import java.awt.image.RenderedImage ; 37 import java.io.File ; 38 import java.io.FileNotFoundException ; 39 import java.io.FileOutputStream ; 40 import java.io.IOException ; 41 import java.io.OutputStream ; 42 43 import javax.imageio.ImageIO ; 44 import javax.servlet.ServletOutputStream ; 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 60 public class PictureGenerator { 61 62 private static PictureGenerator instance = null; 63 64 private BufferedImage image; 65 66 private Graphics2D g; 67 68 private GradientPaint 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 lock = new Object (); 85 86 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 e) { 131 132 } 133 134 image = new BufferedImage (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 ("Dialog", Font.BOLD, (int) (height * 0.9))); 139 gradient = new GradientPaint (0, 0, Color.WHITE, width - 1, height - 1, 140 Color.GRAY); 141 } 142 143 148 public int getHeight() { 149 return height; 150 } 151 152 157 public int getWidth() { 158 return width; 159 } 160 161 169 public void generatePicture(String label, OutputStream out) 170 throws IOException { 171 172 173 g.setPaint(gradient); 174 g.fillRect(0, 0, width - 1, height - 1); 175 176 g.setColor(Color.BLACK); 177 g.drawString(label, 178 (width - g.getFontMetrics().stringWidth(label)) / 2, 179 (int) (height * 0.85)); 180 181 182 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 189 for (int x = (int) (grid / 2); x < width; x += grid) { 190 g.drawLine(x, 0, x, height); 191 } 192 ImageIO.write((RenderedImage ) image, "jpeg", out); 193 } 194 195 200 public void prepareImage(BufferedImage img, OutputStream out) 201 throws IOException { 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 xform = AffineTransform.getScaleInstance((double) h 221 / (double) fullH, (double) w / (double) fullW); 222 AffineTransformOp op = new AffineTransformOp (xform, 223 AffineTransformOp.TYPE_NEAREST_NEIGHBOR); 224 BufferedImage newImage = new BufferedImage (w, h, 225 BufferedImage.TYPE_INT_RGB); 226 Graphics2D gr = newImage.createGraphics(); 227 int top = 0; 228 int left = 0; 229 gr.drawImage(img, op, left, top); 230 ImageIO.write((RenderedImage ) newImage, "jpeg", out); 231 } else { 232 ImageIO.write((RenderedImage ) img, "jpeg", out); 233 } 234 235 } 236 237 242 public void prepareImageThumbnail(BufferedImage img, ServletOutputStream out) 243 throws IOException { 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 xform = AffineTransform.getScaleInstance((double) h 262 / (double) fullH, (double) w / (double) fullW); 263 AffineTransformOp op = new AffineTransformOp (xform, 264 AffineTransformOp.TYPE_NEAREST_NEIGHBOR); 265 BufferedImage newImage = new BufferedImage (this.thumbW, this.thumbH, 266 BufferedImage.TYPE_INT_RGB); 267 Graphics2D 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 ) newImage, "jpeg", out); 274 275 } 276 277 public static void main(String [] args) { String label = "PictureGeneratorTest" + System.currentTimeMillis() 279 + ".jpg"; 280 File out = new File (label); 281 try { 282 getInstance().generatePicture(label, new FileOutputStream (out)); 283 } catch (SystemException e) { 284 e.printStackTrace(); 285 } catch (FileNotFoundException e) { 286 e.printStackTrace(); 287 } catch (IOException e) { 288 e.printStackTrace(); 289 } 290 } 291 } 292 | Popular Tags |