KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > jwi > jgallery > toolkit > ToolkitThumbnailWriter


1 /*
2  * jGallery - Java web application to display image galleries
3  *
4  * Copyright (C) 2004 Juergen Weber
5  *
6  * This file is part of jGallery.
7  *
8  * jGallery is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * jGallery is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with jGallery; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston
21  */

22
23 package de.jwi.jgallery.toolkit;
24
25 import java.awt.Container JavaDoc;
26 import java.awt.Graphics2D JavaDoc;
27 import java.awt.Image JavaDoc;
28 import java.awt.MediaTracker JavaDoc;
29 import java.awt.RenderingHints JavaDoc;
30 import java.awt.Toolkit JavaDoc;
31 import java.awt.image.BufferedImage JavaDoc;
32 import java.io.BufferedOutputStream JavaDoc;
33 import java.io.File JavaDoc;
34 import java.io.FileOutputStream JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.Serializable JavaDoc;
37
38 import com.sun.image.codec.jpeg.JPEGCodec;
39 import com.sun.image.codec.jpeg.JPEGEncodeParam;
40 import com.sun.image.codec.jpeg.JPEGImageEncoder;
41
42 import de.jwi.jgallery.IThumbnailWriter;
43
44 /**
45  * @author Juergen Weber Created on 28.07.2004
46  *
47  */

48 public class ToolkitThumbnailWriter implements IThumbnailWriter, Serializable JavaDoc {
49
50     public void write(File JavaDoc infile, File JavaDoc outfile, float compressionQuality, int thumbBounds) throws IOException JavaDoc {
51         Image JavaDoc image = Toolkit.getDefaultToolkit().getImage(infile.toURL());
52         MediaTracker JavaDoc mediaTracker = new MediaTracker JavaDoc(new Container JavaDoc());
53         mediaTracker.addImage(image, 0);
54         try {
55             mediaTracker.waitForID(0);
56         } catch (InterruptedException JavaDoc e) {
57             return;
58         }
59         
60         int imageWidth = image.getWidth(null);
61         int imageHeight = image.getHeight(null);
62
63         float thumbRatio = (float) thumbBounds
64                 / Math.max(imageWidth, imageHeight);
65
66         int thumbWidth = (int) (imageWidth * thumbRatio);
67         int thumbHeight = (int) (imageHeight * thumbRatio);
68
69         // draw original image to thumbnail image object and
70
// scale it to the new size on-the-fly
71
BufferedImage JavaDoc thumbImage = new BufferedImage JavaDoc(thumbWidth,
72           thumbHeight, BufferedImage.TYPE_INT_RGB);
73         Graphics2D JavaDoc graphics2D = thumbImage.createGraphics();
74         graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
75           RenderingHints.VALUE_INTERPOLATION_BICUBIC);
76         graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
77         // save thumbnail image to OUTFILE
78

79         BufferedOutputStream JavaDoc out = new BufferedOutputStream JavaDoc(new
80           FileOutputStream JavaDoc(outfile));
81         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
82         JPEGEncodeParam param = encoder.
83           getDefaultJPEGEncodeParam(thumbImage);
84
85         param.setQuality(compressionQuality, false);
86         encoder.setJPEGEncodeParam(param);
87         encoder.encode(thumbImage);
88         
89         out.close();
90     }
91
92 }
Popular Tags