KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > jwi > jgallery > imageio > ImageIOThumbnailWriter


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

24
25 import java.awt.Graphics2D JavaDoc;
26 import java.awt.Image JavaDoc;
27 import java.awt.RenderingHints JavaDoc;
28 import java.awt.image.BufferedImage JavaDoc;
29 import java.io.File JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.Serializable JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34 import javax.imageio.ImageIO JavaDoc;
35 import javax.imageio.ImageWriteParam JavaDoc;
36 import javax.imageio.ImageWriter JavaDoc;
37 import javax.imageio.stream.ImageOutputStream JavaDoc;
38
39 import de.jwi.jgallery.IThumbnailWriter;
40
41 /**
42  * @author Jürgen Weber
43  * Source file created on 29.02.2004
44  */

45 public class ImageIOThumbnailWriter
46 implements IThumbnailWriter, Serializable JavaDoc
47 {
48     public void write(File JavaDoc infile, File JavaDoc outfile, float compressionQuality,
49             int thumbBounds) throws IOException JavaDoc
50     {
51         // Retrieve jpg image to be resized
52
Image JavaDoc image = ImageIO.read(infile);
53
54         int imageWidth = image.getWidth(null);
55         int imageHeight = image.getHeight(null);
56
57         float thumbRatio = (float) thumbBounds
58                 / Math.max(imageWidth, imageHeight);
59
60         int thumbWidth = (int) (imageWidth * thumbRatio);
61         int thumbHeight = (int) (imageHeight * thumbRatio);
62
63         // draw original image to thumbnail image object and
64
// scale it to the new size on-the-fly
65
BufferedImage JavaDoc thumbImage = new BufferedImage JavaDoc(thumbWidth, thumbHeight,
66                 BufferedImage.TYPE_INT_RGB);
67         Graphics2D JavaDoc graphics2D = thumbImage.createGraphics();
68         graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
69                 RenderingHints.VALUE_INTERPOLATION_BICUBIC);
70         graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
71
72         // Find a jpeg writer
73
ImageWriter JavaDoc writer = null;
74         Iterator JavaDoc iter = ImageIO.getImageWritersByFormatName("jpg");
75         if (iter.hasNext())
76         {
77             writer = (ImageWriter JavaDoc) iter.next();
78         }
79
80         ImageWriteParam JavaDoc iwp = writer.getDefaultWriteParam();
81         iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
82         iwp.setCompressionQuality(compressionQuality);
83
84         // Prepare output file
85
ImageOutputStream JavaDoc ios = ImageIO.createImageOutputStream(outfile);
86         writer.setOutput(ios);
87
88         writer.write(thumbImage);
89
90         // Cleanup
91
ios.flush();
92         writer.dispose();
93         ios.close();
94     }
95
96 }
97
Popular Tags