View Javadoc
1 /* 2 * Created on 2003-5-23 17:06:47 by joel guo 3 * 4 * vTradEx Information Technology Inc. 5 */ 6 package com.cyclops.albumbuilder; 7 import java.awt.Dimension; 8 import java.awt.Image; 9 import java.awt.image.BufferedImage; 10 import java.awt.image.ImageObserver; 11 import java.io.File; 12 import java.util.Iterator; 13 14 import javax.imageio.ImageIO; 15 16 import org.apache.stratum.lifecycle.Executable; 17 /*** 18 * Add description for this class <font color="red">HERE</font>! 19 * <big><font face="黑体">惩前毖后,治病救人</font></big> 20 * 21 * @author <a href="mailto:joeblack.guo@vtradex.com">joel guo</a> 22 * @company <a href="http://www.vtradex.com">vTradEx</a> 23 * @since 2003-5-23 17:06:47 24 */ 25 public class ThumbnailGenerator 26 extends AlbumBuilderComponent 27 implements Executable, ImageObserver { 28 private String targetDirectory; 29 private Dimension maxThumbnailDimension; 30 private boolean renderFinished; 31 private boolean forceOverWrite; 32 /*** Override method execute() in super class 33 * @see org.apache.stratum.lifecycle.Executable#execute() 34 */ 35 public void execute() throws Exception { 36 for (Iterator i = getSourceRepository().values().iterator(); 37 i.hasNext(); 38 ) { 39 SourceResource sourceResource = (SourceResource) i.next(); 40 try { 41 if (sourceResource == null) { 42 continue; 43 } 44 File file = generateThumnail(sourceResource); 45 logger.debug("Thumbnail " + file + " generated"); 46 } catch (Exception e) { 47 logger.error( 48 "Can't generate thumbnail for source image " 49 + sourceResource, 50 e); 51 } 52 } 53 } 54 private synchronized File generateThumnail(SourceResource res) 55 throws Exception { 56 File sourceFile = res.getSourceFile(); 57 File gen = new File(targetDirectory + res.getRelativePath()); 58 File targetFile = 59 new File( 60 gen.getParentFile(), 61 "thumbnail" + File.separator + sourceFile.getName()); 62 if (!targetFile.getParentFile().isDirectory()) { 63 targetFile.getParentFile().mkdirs(); 64 } 65 if (targetFile.isFile() 66 && targetFile.lastModified() > sourceFile.lastModified() 67 && !forceOverWrite) { 68 logger.debug("File " + targetFile + "already exists!"); 69 return targetFile; 70 } 71 BufferedImage sourceImage = ImageIO.read(sourceFile); 72 Dimension sourceSize = 73 new Dimension(sourceImage.getWidth(), sourceImage.getHeight()); 74 res.setSize(sourceSize); 75 Dimension thumbnailSize = getThumbnailSize(sourceSize); 76 Image temp = 77 sourceImage.getScaledInstance( 78 (int) thumbnailSize.getWidth(), 79 (int) thumbnailSize.getHeight(), 80 Image.SCALE_AREA_AVERAGING); 81 BufferedImage targetImage = 82 new BufferedImage( 83 (int) thumbnailSize.getWidth(), 84 (int) thumbnailSize.getHeight(), 85 BufferedImage.TYPE_INT_RGB); 86 renderFinished = false; 87 targetImage.getGraphics().drawImage(temp, 0, 0, this); 88 while (!renderFinished) { 89 Thread.sleep(100); 90 } 91 ImageIO.write(targetImage, "jpeg", targetFile); 92 return targetFile; 93 } 94 private Dimension getThumbnailSize(Dimension d) { 95 double xRatio = maxThumbnailDimension.getWidth() / d.getWidth(); 96 double yRatio = maxThumbnailDimension.getHeight() / d.getHeight(); 97 double ratio = Math.min(xRatio, yRatio); 98 return new Dimension( 99 (int) (ratio * d.getWidth()), 100 (int) (ratio * d.getHeight())); 101 } 102 /*** Override method imageUpdate() in super class 103 * @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int) 104 */ 105 public boolean imageUpdate( 106 Image img, 107 int infoflags, 108 int x, 109 int y, 110 int width, 111 int height) { 112 renderFinished = ((infoflags & FRAMEBITS) > 0); 113 return true; 114 } 115 /*** Override method initialize() of super class 116 * @see org.apache.stratum.lifecycle.Initializable#initialize() 117 */ 118 public void initialize() { 119 super.initialize(); 120 targetDirectory = 121 getConfiguration().getString("target.directory", "target/docs/album"); 122 int maxWidth = 123 getConfiguration().getInt("generation.thumbnail.maxwidth", 100); 124 int maxHeight = 125 getConfiguration().getInt("generation.thumbnail.maxheight", 100); 126 maxThumbnailDimension = new Dimension(maxWidth, maxHeight); 127 forceOverWrite = 128 getConfiguration().getBoolean("generation.forceoverwrite", false); 129 } 130 }

This page was automatically generated by Maven