View Javadoc

1   package org.springbyexample.util.image;
2   
3   import java.awt.Graphics2D;
4   import java.awt.geom.AffineTransform;
5   import java.awt.image.BufferedImage;
6   import java.io.File;
7   import java.io.IOException;
8   import java.io.InputStream;
9   import java.io.OutputStream;
10  
11  import javax.imageio.ImageIO;
12  
13  import org.slf4j.Logger;
14  import org.slf4j.LoggerFactory;
15  import org.springframework.util.Assert;
16  
17  /**
18   * Image processor.
19   * 
20   * @author David Winterfeldt
21   */
22  public class ImageProcessorImpl implements ImageProcessor{
23  
24  	final Logger logger = LoggerFactory.getLogger(ImageProcessorImpl.class);
25  	
26  	protected int imageScaleWidth = 200;
27  
28  	/**
29       * Gets image width for scaling.
30       */
31      public int getImageScaleWidth() {
32          return imageScaleWidth;
33      }
34      
35  	/**
36       * Sets image width for scaling.
37       */
38      public void setImageScaleWidth(int imageScaleWidth) {
39          this.imageScaleWidth = imageScaleWidth;
40      }
41  
42      /**
43       * Creates a scaled new file.
44       */
45  	public void scaleImage(File imageFile, File newImageFile) 
46  			throws IOException {
47          Assert.notNull(imageFile, "Image file can not be null.");
48          Assert.notNull(newImageFile, "New image file can not be null.");
49  
50  	    String imageFileName = imageFile.getName();
51          String formatName = imageFileName.substring(imageFileName.lastIndexOf("."));
52  
53  	    BufferedImage image = ImageIO.read(imageFile);
54  	    
55  	    BufferedImage destImage = processImageScaling(image);
56  
57  	    ImageIO.write(destImage, formatName, newImageFile);
58  
59          logger.debug("Created '{}' from orginal file '{}'.", newImageFile.getAbsolutePath(), imageFile.getAbsolutePath());
60  	}
61  
62      /**
63       * Creates a scaled new file.
64       */
65      public void scaleImage(InputStream in, OutputStream out, String formatName) 
66              throws IOException {
67          Assert.notNull(in, "InputStream can not be null.");
68          Assert.notNull(out, "OutStream can not be null.");
69          Assert.hasText(formatName, "Format name can not be blank.");
70          
71          BufferedImage image = ImageIO.read(in);
72          
73          BufferedImage destImage = processImageScaling(image);
74  
75          ImageIO.write(destImage, formatName, out);
76          
77          logger.debug("Created scaled from orginal file.");
78      }
79      
80      /**
81       * Creates a scaled new file.
82       */
83      protected BufferedImage processImageScaling(BufferedImage image) 
84              throws IOException {
85  		double d = (double) imageScaleWidth / (double) image.getHeight(null);
86  		
87  		if (image.getWidth(null) > image.getHeight(null)) {
88  			d = (double) imageScaleWidth / (double) image.getWidth(null);
89  		}
90  		
91  		int j = (int) (d * (double) image.getWidth(null));
92  		int k = (int) (d * (double) image.getHeight(null));
93  		
94  		BufferedImage destImage = new BufferedImage(j, k, 1);
95  
96  		AffineTransform at = new AffineTransform();
97  		
98  		if (d < 1.0D) {
99  			at.scale(d, d);
100 		}
101 			
102 		Graphics2D graphics2d = destImage.createGraphics();
103 		graphics2d.drawImage(image, at, null);
104 		graphics2d.dispose();
105 		
106 		return destImage;
107 	}
108 	   
109 }