KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > util > graphics > ThumbnailGenerator


1 /* ===============================================================================
2 *
3 * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4 *
5 * ===============================================================================
6 *
7 * Copyright (C)
8 *
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License version 2, as published by the
11 * Free Software Foundation. See the file LICENSE.html for more information.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19 * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20 *
21 * ===============================================================================
22 */

23
24 package org.infoglue.cms.util.graphics;
25
26 import java.awt.Color JavaDoc;
27 import java.awt.Graphics2D JavaDoc;
28 import java.awt.Image JavaDoc;
29 import java.awt.RenderingHints JavaDoc;
30 import java.awt.image.BufferedImage JavaDoc;
31 import java.io.BufferedReader JavaDoc;
32 import java.io.File JavaDoc;
33 import java.io.InputStreamReader JavaDoc;
34
35 import org.apache.log4j.Logger;
36 import org.infoglue.cms.util.CmsPropertyHandler;
37
38 public class ThumbnailGenerator
39 {
40     private final static Logger logger = Logger.getLogger(ThumbnailGenerator.class.getName());
41
42     public ThumbnailGenerator()
43     {
44     }
45    
46     private void execCmd(String JavaDoc command) throws Exception JavaDoc
47     {
48         logger.error(command);
49         String JavaDoc line;
50         Process JavaDoc p = Runtime.getRuntime().exec(command);
51         BufferedReader JavaDoc input = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(p.getInputStream()));
52         
53         while ((line = input.readLine()) != null)
54         {
55             logger.error(line);
56         }
57         input.close();
58     }
59    
60     public void transform(String JavaDoc originalFile, String JavaDoc thumbnailFile, int thumbWidth, int thumbHeight, int quality) throws Exception JavaDoc
61     {
62         Image JavaDoc image = javax.imageio.ImageIO.read(new File JavaDoc(originalFile));
63         
64         double thumbRatio = (double)thumbWidth / (double)thumbHeight;
65         int imageWidth = image.getWidth(null);
66         int imageHeight = image.getHeight(null);
67         double imageRatio = (double)imageWidth / (double)imageHeight;
68         if (thumbRatio < imageRatio)
69         {
70             thumbHeight = (int)(thumbWidth / imageRatio);
71         }
72         else
73         {
74             thumbWidth = (int)(thumbHeight * imageRatio);
75         }
76         
77         if(imageWidth < thumbWidth && imageHeight < thumbHeight)
78         {
79             thumbWidth = imageWidth;
80             thumbHeight = imageHeight;
81         }
82         else if(imageWidth < thumbWidth)
83             thumbWidth = imageWidth;
84         else if(imageHeight < thumbHeight)
85             thumbHeight = imageHeight;
86         
87         if(thumbWidth < 1)
88             thumbWidth = 1;
89         if(thumbHeight < 1)
90             thumbHeight = 1;
91             
92         if(CmsPropertyHandler.getExternalThumbnailGeneration() != null && !CmsPropertyHandler.getExternalThumbnailGeneration().equalsIgnoreCase("") && !CmsPropertyHandler.getExternalThumbnailGeneration().equalsIgnoreCase("@externalThumbnailGeneration@"))
93         {
94             String JavaDoc[] args = new String JavaDoc[5];
95             
96             args[0] = CmsPropertyHandler.getExternalThumbnailGeneration();
97             args[1] = "-resize";
98             args[2] = String.valueOf(thumbWidth) + "x" + String.valueOf(thumbHeight);
99             args[3] = originalFile;
100             args[4] = thumbnailFile;
101             
102             try
103             {
104                 Process JavaDoc p = Runtime.getRuntime().exec(args);
105                 p.waitFor();
106             }
107             catch(InterruptedException JavaDoc e)
108             {
109                 new Exception JavaDoc("Error resizing image for thumbnail", e);
110             }
111         }
112         else
113         {
114             BufferedImage JavaDoc thumbImage = new BufferedImage JavaDoc(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
115             Graphics2D JavaDoc graphics2D = thumbImage.createGraphics();
116             graphics2D.setBackground(Color.WHITE);
117             graphics2D.setPaint(Color.WHITE);
118             graphics2D.fillRect(0, 0, thumbWidth, thumbHeight);
119             graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
120             graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
121             
122             javax.imageio.ImageIO.write(thumbImage, "JPG", new File JavaDoc(thumbnailFile));
123         }
124     }
125 }
126
Popular Tags