1 20 21 package com.methodhead.shim; 22 23 import com.sun.image.codec.jpeg.JPEGEncodeParam; 24 import com.sun.image.codec.jpeg.JPEGCodec; 25 import com.sun.image.codec.jpeg.JPEGImageEncoder; 26 import java.io.BufferedOutputStream ; 27 import java.awt.image.BufferedImage ; 28 import java.awt.Image ; 29 import java.awt.Container ; 30 import java.awt.Graphics2D ; 31 import java.awt.RenderingHints ; 32 import java.awt.MediaTracker ; 33 import java.awt.Toolkit ; 34 import java.awt.Color ; 35 import java.io.FileOutputStream ; 36 import java.io.File ; 37 import org.apache.log4j.Logger; 38 import org.apache.commons.lang.exception.ExceptionUtils; 39 40 43 public class Thumbnailer { 44 45 47 49 52 public static final int THUMB_HEIGHT = 50; 53 54 57 public static final int THUMB_WIDTH = 50; 58 59 62 public static final float THUMB_QUALITY = 1.0f; 63 64 66 68 74 public static void thumbnail( 75 File src, 76 File dest ) { 77 78 logger_.debug( "thumbnail(): Thumbnailing " + src + " to " + dest ); 79 80 try { 81 if ( !src.exists() || !src.isFile() ) 82 throw new ShimException( 83 "File does not exist or is not a file \"" + src + "\"" ); 84 85 Image image = Toolkit.getDefaultToolkit().getImage( src.getAbsolutePath() ); 89 MediaTracker mediaTracker = new MediaTracker ( new Container () ); 90 mediaTracker.addImage( image, 0 ); 91 mediaTracker.waitForID(0); 92 93 int thumbWidth = THUMB_HEIGHT; 97 int thumbHeight = THUMB_WIDTH; 98 double thumbRatio = (double)thumbWidth / (double)thumbHeight; 99 100 int imageWidth = image.getWidth(null); 101 int imageHeight = image.getHeight(null); 102 103 if ( ( imageHeight <= THUMB_HEIGHT ) && ( imageWidth <= THUMB_WIDTH ) ) { 107 thumbHeight = imageHeight; 108 thumbWidth = imageWidth; 109 } 110 111 else { 115 double imageRatio = (double)imageWidth / (double)imageHeight; 116 117 if (thumbRatio < imageRatio) { 118 thumbHeight = (int)(thumbWidth / imageRatio); 119 } 120 else { 121 thumbWidth = (int)(thumbHeight * imageRatio); 122 } 123 } 124 125 if ( thumbHeight == 0 ) 129 thumbHeight = 1; 130 131 if ( thumbWidth == 0 ) 132 thumbWidth = 1; 133 134 BufferedImage thumbImage = new BufferedImage (thumbWidth, 139 thumbHeight, BufferedImage.TYPE_INT_RGB); 140 141 Graphics2D graphics2D = thumbImage.createGraphics(); 142 graphics2D.setRenderingHint( 143 RenderingHints.KEY_INTERPOLATION, 144 RenderingHints.VALUE_INTERPOLATION_BILINEAR); 145 graphics2D.setRenderingHint( 146 RenderingHints.KEY_ALPHA_INTERPOLATION, 147 RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); 148 graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, Color.WHITE, null); 149 150 BufferedOutputStream out = new BufferedOutputStream (new 154 FileOutputStream ( dest )); 155 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 156 JPEGEncodeParam param = encoder. 157 getDefaultJPEGEncodeParam(thumbImage); 158 param.setQuality(THUMB_QUALITY, false); 159 encoder.setJPEGEncodeParam(param); 160 encoder.encode(thumbImage); 161 out.close(); 162 } 163 164 catch ( java.lang.InterruptedException e ) { 168 throw new ShimException( "Thumbnailing " + src + " to " + dest + ". " + ExceptionUtils.getStackTrace( e ) ); 169 } 170 catch ( java.io.FileNotFoundException e ) { 171 throw new ShimException( "Thumbnailing " + src + " to " + dest + ". " + ExceptionUtils.getStackTrace( e ) ); 172 } 173 catch ( java.io.IOException e ) { 174 throw new ShimException( "Thumbnailing " + src + " to " + dest + ". " + ExceptionUtils.getStackTrace( e ) ); 175 } 176 } 177 178 187 public void syncDir( 188 File src, 189 File dest ) { 190 191 logger_.debug( "syncDir(): Syncing " + src + " to " + dest ); 192 193 if ( !src.exists() || !src.isDirectory() ) 194 throw new ShimException( "Invalid source directory \"" + src + "\"" ); 195 196 if ( !dest.exists() || !dest.isDirectory() ) 197 throw new ShimException( "Invalid dest directory \"" + dest + "\"" ); 198 199 File [] files = src.listFiles(); 200 for ( int i = 0; i < files.length; i++ ) { 201 String fileName = files[ i ].getName().toLowerCase(); 202 203 if ( !fileName.endsWith( ".jpg" ) && 204 !fileName.endsWith( ".jpeg" ) && 205 !fileName.endsWith( ".png" ) && 206 !fileName.endsWith( ".gif" ) ) 207 continue; 208 209 File f = new File ( dest, files[ i ].getName() + ".jpg" ); 210 211 if ( f.exists() && 212 f.isFile() && 213 ( files[ i ].lastModified() < f.lastModified() ) ) 214 continue; 215 216 try { 217 thumbnail( files[ i ], f ); 218 } 219 catch ( IllegalArgumentException e ) { 220 221 logger_.warn( "Couldn't thumbnail " + files[ i ] + ". " + e.getMessage() ); 226 } 227 } 228 } 229 230 232 234 private static Logger logger_ = Logger.getLogger( Thumbnailer.class ); 238 } 239 | Popular Tags |