KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > module > admininterface > dialogpages > FileThumbnailDialogPage


1 package info.magnolia.module.admininterface.dialogpages;
2
3 import info.magnolia.cms.core.HierarchyManager;
4 import info.magnolia.cms.core.NodeData;
5 import info.magnolia.cms.security.SessionAccessControl;
6
7 import java.awt.Graphics2D JavaDoc;
8 import java.awt.Image JavaDoc;
9 import java.awt.image.BufferedImage JavaDoc;
10 import java.io.BufferedOutputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.InputStream JavaDoc;
13
14 import javax.imageio.ImageIO JavaDoc;
15 import javax.servlet.ServletOutputStream JavaDoc;
16 import javax.servlet.http.HttpServlet JavaDoc;
17 import javax.servlet.http.HttpServletRequest JavaDoc;
18 import javax.servlet.http.HttpServletResponse JavaDoc;
19
20 import org.apache.log4j.Logger;
21
22 import com.sun.image.codec.jpeg.JPEGCodec;
23 import com.sun.image.codec.jpeg.JPEGEncodeParam;
24 import com.sun.image.codec.jpeg.JPEGImageEncoder;
25
26
27 /**
28  * @author Fabrizio Giustina
29  * @version $Id: $
30  */

31 public class FileThumbnailDialogPage extends HttpServlet JavaDoc {
32
33     /**
34      * Stable serialVersionUID.
35      */

36     private static final long serialVersionUID = 222L;
37
38     /**
39      * Logger.
40      */

41     private static Logger log = Logger.getLogger(FileThumbnailDialogPage.class);
42
43     /**
44      * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest, HttpServletResponse)
45      */

46     public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc {
47         response.setContentType("image/jpeg"); //$NON-NLS-1$
48

49         String JavaDoc src = request.getParameter("src"); //$NON-NLS-1$
50
String JavaDoc size = request.getParameter("size"); //$NON-NLS-1$
51

52         HierarchyManager hm = SessionAccessControl.getHierarchyManager(request);
53
54         InputStream JavaDoc in = null;
55
56         NodeData data;
57         try {
58             data = hm.getNodeData(src);
59             in = data.getValue().getStream();
60         }
61         catch (Exception JavaDoc e) {
62             log.error(e.getMessage(), e);
63             return;
64         }
65
66         Image JavaDoc image = ImageIO.read(in);
67
68         int thumbHeight;
69         int thumbWidth;
70         if (size != null) {
71             thumbHeight = image.getHeight(null);
72             thumbWidth = image.getWidth(null);
73         }
74         else {
75             thumbWidth = 150;
76             int w = image.getWidth(null);
77             int h = image.getHeight(null);
78             if (w == 0) {
79                 w = 1;
80             }
81             if (h == 0) {
82                 h = 1;
83             }
84
85             if (w > thumbWidth) {
86                 thumbHeight = thumbWidth * h / w;
87             }
88             else {
89                 thumbWidth = w;
90                 thumbHeight = h;
91             }
92
93             if (thumbHeight > 120) {
94                 thumbHeight = 100;
95                 thumbWidth = thumbHeight * w / h;
96             }
97
98         }
99
100         // draw original image to thumbnail image object and scale it to the new size on-the-fly
101
BufferedImage JavaDoc thumbImage = new BufferedImage JavaDoc(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
102         Graphics2D JavaDoc graphics2D = thumbImage.createGraphics();
103         // graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
104
graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
105
106         ServletOutputStream JavaDoc sout = response.getOutputStream();
107         BufferedOutputStream JavaDoc output = new BufferedOutputStream JavaDoc(sout);
108         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);
109         JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);
110         param.setQuality(0.8f, false);
111
112         encoder.setJPEGEncodeParam(param);
113         encoder.encode(thumbImage);
114
115     }
116
117 }
118
Popular Tags