KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.Serializable JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 import javax.imageio.ImageIO JavaDoc;
31 import javax.imageio.ImageWriteParam JavaDoc;
32 import javax.imageio.ImageWriter JavaDoc;
33 import javax.imageio.stream.ImageOutputStream JavaDoc;
34
35 import java.awt.image.renderable.ParameterBlock JavaDoc;
36
37 import javax.media.jai.Interpolation;
38 import javax.media.jai.JAI;
39 import javax.media.jai.RenderedOp;
40
41 import com.sun.media.jai.codec.FileSeekableStream;
42
43 import de.jwi.jgallery.IThumbnailWriter;
44
45 /**
46  * @author Daniel Kottow
47  * Source file created on 3.11.2004
48  */

49
50 public class JAIThumbnailWriter
51 implements IThumbnailWriter, Serializable JavaDoc
52 {
53     public void write(File JavaDoc infile, File JavaDoc outfile, float compressionQuality,
54             int thumbBounds) throws IOException JavaDoc
55     {
56
57
58         /* Create an operator to decode the image file. */
59         RenderedOp image = JAI.create("stream", new FileSeekableStream(infile));
60
61         int imageWidth = image.getWidth();
62         int imageHeight = image.getHeight();
63
64         float thumbRatio = (float) thumbBounds
65                 / Math.max(imageWidth, imageHeight);
66
67              /*
68               * Create a standard bilinear interpolation object to be
69               * used with the "scale" operator.
70               */

71               
72              Interpolation interp = Interpolation.getInstance(
73                                         Interpolation.INTERP_BILINEAR);
74
75              /**
76               * Stores the required input source and parameters in a
77               * ParameterBlock to be sent to the operation registry,
78               * and eventually to the "scale" operator.
79               */

80               
81              ParameterBlock JavaDoc params = new ParameterBlock JavaDoc();
82              params.addSource(image);
83              params.add(thumbRatio); // x scale factor
84
params.add(thumbRatio); // y scale factor
85
params.add(0.0F); // x translate
86
params.add(0.0F); // y translate
87
params.add(interp); // interpolation method
88

89              /* Create an operator to scale image1. */
90              RenderedOp thumbImage = JAI.create("scale", params);
91
92
93         // Find a jpeg writer
94
ImageWriter JavaDoc writer = null;
95         Iterator JavaDoc iter = ImageIO.getImageWritersByFormatName("jpg");
96         if (iter.hasNext())
97         {
98             writer = (ImageWriter JavaDoc) iter.next();
99         }
100
101         ImageWriteParam JavaDoc iwp = writer.getDefaultWriteParam();
102         iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
103         iwp.setCompressionQuality(compressionQuality);
104
105         // Prepare output file
106
ImageOutputStream JavaDoc ios = ImageIO.createImageOutputStream(outfile);
107         writer.setOutput(ios);
108
109         writer.write(thumbImage);
110
111         // Cleanup
112
ios.flush();
113         writer.dispose();
114         ios.close();
115     }
116
117 }
118
Popular Tags