1 4 package com.openedit.modules.image; 5 6 import java.awt.Dimension ; 7 import java.awt.Graphics2D ; 8 import java.awt.RenderingHints ; 9 import java.awt.image.BufferedImage ; 10 import java.io.File ; 11 import java.io.FileNotFoundException ; 12 import java.io.IOException ; 13 14 import javax.imageio.ImageIO ; 15 16 import org.apache.commons.logging.Log; 17 import org.apache.commons.logging.LogFactory; 18 19 25 public class ImageResizer 26 { 27 private static final Log log = LogFactory.getLog(ImageResizer.class); 28 29 protected Dimension fieldMaxScaledSize = new Dimension ( 150, Integer.MAX_VALUE ); 30 31 35 public static void main(String [] args) 36 { 37 try 38 { 39 if ( args.length < 2) 40 { 41 log.error("Usage: dir|file in out"); 42 return; 43 } 44 ImageResizer resizer = new ImageResizer(); 45 51 if ( "dir".equals( args[0] ) ) 52 { 53 int numImages = resizer.resizeImagesInDirectory( 54 new File ( args[1] ), 55 new File ( args[2] ), 56 true ); 57 log.info( numImages + " image(s) scaled" ); 58 59 } 60 else 61 { 62 File in = new File ( args[1]); 63 File out = new File ( args[2]); 64 log.info("Converting " + in.getAbsolutePath() + " to " + out.getPath() ); 65 66 resizer.resizeImage(in, out); 67 } 68 69 } 70 catch ( Exception e ) 71 { 72 System.err.println( "Fatal exception: " + e ); 73 e.printStackTrace(); 74 } 75 } 76 77 public ImageResizer() 78 { 79 } 80 81 92 public int resizeImagesInDirectory( File inInDirectory, File inOutDirectory, 93 boolean inRecurse ) throws Exception 94 { 95 if ( !inInDirectory.exists() ) 97 { 98 throw new FileNotFoundException ( inInDirectory + " does not exist" ); 99 } 100 if ( !inOutDirectory.exists() && !inOutDirectory.mkdirs() ) 101 { 102 throw new FileNotFoundException ( inOutDirectory + " does not exist and cannot be created" ); 103 } 104 if ( !inInDirectory.isDirectory() ) 105 { 106 throw new IllegalArgumentException ( inInDirectory + " is not a directory" ); 107 } 108 if ( !inOutDirectory.isDirectory() ) 109 { 110 throw new IllegalArgumentException ( inOutDirectory + " is not a directory" ); 111 } 112 if ( inInDirectory.equals( inOutDirectory ) ) 113 { 114 throw new IllegalArgumentException ( "directory arguments are the same" ); 115 } 116 117 int numImagesResized = 0; 119 File [] inputFiles = inInDirectory.listFiles(); 120 for ( int n = 0; n < inputFiles.length; n++ ) 121 { 122 File inputFile = inputFiles[ n ]; 123 String filename = inputFile.getName(); 124 if ( inputFile.isDirectory() ) 125 { 126 if ( inRecurse ) 127 { 128 File outputDir = new File ( inOutDirectory, filename ); 129 outputDir.mkdir(); 130 numImagesResized += resizeImagesInDirectory( 131 inputFile, outputDir, inRecurse ); 132 } 133 } 134 else 135 { 136 if ( filename.toLowerCase().endsWith( "jpg" ) || 137 filename.toLowerCase().endsWith( "jpeg" ) ) 138 { 139 File outputFile = new File ( inOutDirectory, filename ); 140 if ( outputFile.exists() ) 141 { 142 log.info( "Skipping resizing " + filename ); 143 } 144 else 145 { 146 try 147 { 148 resizeImage( inputFile, outputFile ); 149 numImagesResized++; 150 } 151 catch ( IOException e ) 152 { 153 log.error( "Image " + inputFile + " could not be resized" ); 154 log.error( e ); 155 } 156 } 157 } 158 } 159 } 160 161 return numImagesResized; 162 } 163 164 171 public void resizeImage( File inInImageFile, File inOutImageFile ) 172 throws Exception 173 { 174 BufferedImage origImage = ImageIO.read( inInImageFile ); 175 if ( origImage == null ) 176 { 177 throw new IllegalArgumentException ("Input file is not valid " + inInImageFile.getPath()); 178 } 179 Dimension scaledSize = getScaledSize( origImage ); 180 BufferedImage scaledImage = new BufferedImage ( scaledSize.width, 181 scaledSize.height, BufferedImage.TYPE_INT_RGB ); 182 Graphics2D scaledGraphics = scaledImage.createGraphics(); 183 scaledGraphics.setRenderingHint( RenderingHints.KEY_INTERPOLATION, 184 RenderingHints.VALUE_INTERPOLATION_BICUBIC); 185 scaledGraphics.drawImage( origImage, 0, 0, scaledSize.width, 186 scaledSize.height, null ); 187 inOutImageFile.getParentFile().mkdirs(); 188 ImageIO.write( scaledImage, "jpeg", inOutImageFile ); 189 log.info("Resize complete " + inInImageFile.getPath()); 190 } 191 192 protected Dimension getScaledSize( BufferedImage inOrigImage ) 193 { 194 Dimension maxSize = getMaxScaledSize(); 195 double heightScale = (double)maxSize.height / (double)inOrigImage.getHeight(); 196 heightScale = Math.min( heightScale, 1.0 ); 197 double widthScale = (double)maxSize.width / (double)inOrigImage.getWidth(); 198 widthScale = Math.min( widthScale, 1.0 ); 199 double scale = Math.min( heightScale, widthScale ); 200 return new Dimension ( 201 (int)Math.round( scale * inOrigImage.getWidth() ), 202 (int)Math.round( scale * inOrigImage.getHeight() ) ); 203 } 204 205 public Dimension getMaxScaledSize() 206 { 207 if ( fieldMaxScaledSize == null ) 208 { 209 fieldMaxScaledSize = new Dimension (); 210 } 211 return fieldMaxScaledSize; 212 } 213 214 public void setMaxScaledSize(Dimension inMaxScaledSize) 215 { 216 fieldMaxScaledSize = inMaxScaledSize; 217 } 218 } | Popular Tags |