1 22 23 package org.meshcms.core; 24 25 import java.awt.*; 26 import java.awt.image.*; 27 import java.io.*; 28 import java.util.regex.*; 29 import javax.imageio.*; 30 import org.meshcms.util.*; 31 32 37 public class ResizedThumbnail extends AbstractThumbnail { 38 41 public static final String MODE_SCALE = "scale"; 42 43 46 public static final String MODE_CROP = "crop"; 47 48 51 public static final String MODE_PADDING = "padding"; 52 53 56 public static final String MODE_STRETCH = "stretch"; 57 58 61 public static final String WHITE = "ffffff"; 62 63 66 public static final int DEFAULT_SIZE = 100; 67 68 private static Matcher hexColorMatcher = 69 Pattern.compile("[ABCDEFabcdef\\d]{6}").matcher(""); 70 71 private int width = -1; 72 private int height = -1; 73 private boolean highQuality = false; 74 private String mode = MODE_SCALE; 75 private String color = WHITE; 76 77 81 public ResizedThumbnail() { 82 } 83 84 87 public ResizedThumbnail(String width, String height) { 88 this.width = Utils.parseInt(width, -1); 89 this.height = Utils.parseInt(height, -1); 90 } 91 92 public String getSuggestedFileName() { 93 return "resized" + ("" + width + height + highQuality + mode + 94 color).hashCode() + ".jpg"; 95 } 96 97 protected boolean createThumbnail(File imageFile, File thumbnailFile) { 98 BufferedImage image; 99 100 try { 101 image = ImageIO.read(imageFile); 102 } catch (Exception ex) { 103 return false; 104 } 105 106 if (image == null) { 107 return false; 108 } 109 110 int w = image.getWidth(); 111 int h = image.getHeight(); 112 113 if (w <= 0 || h <= 0) { 114 return false; 115 } 116 117 int reqW = width; 118 int reqH = height; 119 int w0 = w; 120 int h0 = h; 121 122 if (reqW < 1 && reqH < 1) { 123 reqW = reqH = DEFAULT_SIZE; 124 } else if (reqW < 1) { 125 reqW = w * reqH / h; 126 } else if (reqH < 1) { 127 reqH = h * reqW / w; 128 } 129 130 if (reqW > w && reqH > h) { 131 reqW = w; 132 reqH = h; 133 } 134 135 if (mode.equals(MODE_CROP)) { 136 if (w * reqH < h * reqW) { 137 w0 = reqW; 138 h0 = w0 * h / w; 139 } else { 140 h0 = reqH; 141 w0 = w * h0 / h; 142 } 143 } else if (mode.equals(MODE_STRETCH)) { 144 w0 = reqW; 145 h0 = reqH; 146 } else { 147 if (w * reqH < h * reqW) { 148 h0 = reqH; 149 w0 = w * h0 / h; 150 } else { 151 w0 = reqW; 152 h0 = w0 * h / w; 153 } 154 155 if (!mode.equals(MODE_PADDING)) { 156 reqW = w0; 157 reqH = h0; 158 } 159 } 160 161 BufferedImage thumb = new BufferedImage(reqW, reqH, BufferedImage.TYPE_INT_RGB); 162 Graphics g = thumb.getGraphics(); 163 g.setColor(new Color(Integer.parseInt(color, 16))); 164 g.fillRect(0, 0, reqW, reqH); 165 AbstractThumbnail.drawResizedImage(g, image, (reqW - w0) / 2, 166 (reqH - h0) / 2, w0, h0, highQuality); 167 image.flush(); 168 169 OutputStream os = null; 170 171 try { 172 os = new BufferedOutputStream(new FileOutputStream(thumbnailFile)); 173 ImageIO.write(thumb, "jpeg", os); 174 } catch (IOException ex) { 175 ex.printStackTrace(); 176 return false; 177 } finally { 178 thumb.flush(); 179 g.dispose(); 180 181 if (os != null) { 182 try { 183 os.close(); 184 } catch (IOException ex) {} 185 } 186 } 187 188 return true; 189 } 190 191 194 public boolean isHighQuality() { 195 return highQuality; 196 } 197 198 201 public void setHighQuality(boolean highQuality) { 202 this.highQuality = highQuality; 203 } 204 205 public int getWidth() { 206 return width; 207 } 208 209 212 public void setWidth(int width) { 213 this.width = width; 214 } 215 216 public int getHeight() { 217 return height; 218 } 219 220 223 public void setHeight(int height) { 224 this.height = height; 225 } 226 227 public String getMode() { 228 return mode; 229 } 230 231 235 public void setMode(String mode) { 236 if (mode == null) { 237 this.mode = MODE_SCALE; 238 } else { 239 mode = mode.trim().toLowerCase(); 240 241 if (mode.equals(MODE_CROP) || mode.equals(MODE_PADDING) || 242 mode.equals(MODE_SCALE) || mode.equals(MODE_STRETCH)) { 243 this.mode = mode; 244 } else { 245 throw new IllegalArgumentException ("Unknown mode: " + mode); 246 } 247 } 248 } 249 250 public String getColor() { 251 return color; 252 } 253 254 259 public void setColor(String color) { 260 if (color == null) { 261 this.color = WHITE; 262 } else { 263 if (hexColorMatcher.reset(color).find()) { 264 this.color = hexColorMatcher.group(); 265 } else { 266 throw new IllegalArgumentException ("Unknown color: " + color); 267 } 268 } 269 } 270 } 271 | Popular Tags |