1 19 package org.openharmonise.rm.resources.content.utils; 20 21 import java.awt.image.*; 22 import java.awt.image.renderable.ParameterBlock ; 23 import java.io.File ; 24 import java.util.logging.*; 25 import java.util.logging.Level ; 26 27 import javax.imageio.ImageIO ; 28 import javax.media.jai.*; 29 import javax.media.jai.operator.OverlayDescriptor; 30 31 39 public class ImageConverter { 40 41 44 private static final Logger m_logger = Logger.getLogger(ImageConverter.class.getName()); 45 46 52 static public int getImageHeight(File file) { 53 int nHeight = -1; 54 55 PlanarImage image = readImage(file.getAbsolutePath()); 56 57 if (image != null) { 58 nHeight = image.getHeight(); 59 } 60 61 return nHeight; 62 63 } 64 65 71 static public int getImageWidth(File file) { 72 int nHeight = -1; 73 74 PlanarImage image = readImage(file.getAbsolutePath()); 75 76 if (image != null) { 77 nHeight = image.getWidth(); 78 } 79 80 return nHeight; 81 82 } 83 84 96 public static File convertImage(File file, String sEndType, String sNewName, int nHeight) 97 throws Exception { 98 99 return convertImage(file.getAbsolutePath(), sEndType, sNewName, nHeight); 100 } 101 102 114 public static File convertImage( 115 String sFilename, 116 String sEndType, 117 String sNewName, 118 int nHeight) 119 throws Exception { 120 121 PlanarImage croppedImage = cropImage(readImage(sFilename)); 122 RenderedOp image = scaleImage(croppedImage, nHeight); 123 124 if(sNewName.equals(sFilename)){ 125 sNewName = sNewName + "_copy"; 126 } 127 sNewName = sNewName + "." + sEndType; 128 129 File newFile = saveImage(image, sEndType, sNewName); 130 131 return newFile; 132 } 133 134 144 public static File convertImage(File file, String sEndType, String sNewName) 145 throws Exception { 146 return convertImage(file.getAbsolutePath(), sEndType, sNewName); 147 } 148 149 159 public static File convertImage(String sFilename, String sEndType, String sNewName) 160 throws Exception { 161 162 PlanarImage croppedImage = cropImage(readImage(sFilename)); 163 RenderedOp image = scaleImage(croppedImage); 164 165 if(sNewName.equals(sFilename)){ 166 sNewName = sNewName + "_copy"; 167 } 168 sNewName = sNewName + "." + sEndType; 169 170 File newFile = saveImage(image, sEndType, sNewName); 171 172 return newFile; 173 } 174 175 185 protected static File saveImage( 186 RenderedImage image, 187 String sType, 188 String sAddress) 189 throws Exception { 190 File file = new File (sAddress); 191 ImageIO.write(image, sType, file); 192 193 return file; 194 } 195 196 204 protected static RenderedOp scaleImage(PlanarImage im, int nHeight) { 205 206 int nCurrentHeight = im.getHeight(); 207 float fScale = (float) nHeight / (float) nCurrentHeight; 208 209 return scaleImage(im, fScale); 210 } 211 212 219 protected static RenderedOp scaleImage(PlanarImage im) { 220 return scaleImage(im, 1F); 221 } 222 223 230 protected static RenderedOp scaleImage(PlanarImage im, float fScale) { 231 ParameterBlock pb = new ParameterBlock (); 232 233 pb.addSource(im); 234 pb.add(fScale); pb.add(fScale); pb.add(0.0F); pb.add(0.0F); Interpolation interp = 239 Interpolation.getInstance(Interpolation.INTERP_NEAREST); 240 pb.add(interp); 241 242 RenderedOp rop = JAI.create("scale", pb, null); 243 return rop; 244 } 245 246 253 protected static PlanarImage readImage(String filename) { 254 PlanarImage image = null; 255 256 image = JAI.create("fileload", filename); 257 258 if (image.getColorModel() instanceof IndexColorModel) { 260 IndexColorModel icm = (IndexColorModel) image.getColorModel(); 262 263 int mapSize = icm.getMapSize(); 265 266 byte[][] lutData = new byte[3][mapSize]; 268 269 icm.getReds(lutData[0]); 271 icm.getGreens(lutData[1]); 272 icm.getBlues(lutData[2]); 273 274 LookupTableJAI lut = new LookupTableJAI(lutData); 276 277 image = JAI.create("lookup", image, lut); 279 } 280 281 return image; 282 } 283 284 292 protected static File getSquareImage(File file, int nSize) { 293 File sqrImage = null; 294 295 PlanarImage oldImage = readImage(file.getAbsolutePath()); 296 297 if (oldImage.getHeight() != nSize && oldImage.getWidth() != nSize) { 298 299 ParameterBlock params = new ParameterBlock (); 300 params.addSource(oldImage); 301 302 PlanarImage whiteImage = readImage("C:\\Temp\\white.jpg"); 303 304 int nHeight = whiteImage.getHeight(); 305 306 RenderedOp scaledWhile = scaleImage(whiteImage, nSize); 307 308 RenderedOp op = OverlayDescriptor.create(scaledWhile,oldImage, null); 309 310 try { 311 sqrImage = saveImage(op, "jpg", "C:\\Temp\\square.jpg"); 312 } catch (Exception e) { 313 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 314 } 315 316 } 317 318 return sqrImage; 319 } 320 321 protected static PlanarImage cropImage(PlanarImage image){ 322 int width = image.getWidth(); 323 int height = image.getHeight(); 324 if(width != height){ 325 float size = height; 326 if(width < height){ 327 size = width; 328 } 329 ParameterBlock pb = new ParameterBlock (); 330 pb.addSource(image); 331 pb.add(0.0F); pb.add(0.0F); pb.add(size); pb.add(size); return JAI.create("crop",pb,null); 337 } else { 338 return image; 339 } 340 } 341 342 348 public static void main(String [] args) { 349 350 PlanarImage image = cropImage(readImage("C:\\projects\\sim site\\new html\\simulacra_html\\images\\content\\info_architecture_image.jpg")); 351 352 try { 353 saveImage(image, "jpg", "C:\\Temp\\test.jpg"); 354 } 355 catch (Exception e) { 356 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 357 } 358 } 359 360 } 361 | Popular Tags |