KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > encoders > SunJPEGEncoderAdapter


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * --------------------------
28  * SunJPEGEncoderAdapter.java
29  * --------------------------
30  * (C) Copyright 2004-2006, by Richard Atkinson and Contributors.
31  *
32  * Original Author: Richard Atkinson;
33  * Contributor(s): David Gilbert (for Object Refinery Limited);
34  *
35  * $Id: SunJPEGEncoderAdapter.java,v 1.3.2.3 2006/07/20 14:27:00 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 01-Aug-2004 : Initial version (RA);
40  * 01-Nov-2005 : To remove the dependency on non-supported APIs, use ImageIO
41  * instead of com.sun.image.codec.jpeg.JPEGImageEncoder - this
42  * adapter will only be available on JDK 1.4 or later (DG);
43  * ------------- JFREECHART 1.0.0 ---------------------------------------------
44  * 20-Jul-2006 : Pass quality setting to ImageIO. Also increased default
45  * value to 0.95 (DG);
46  *
47  */

48
49 package org.jfree.chart.encoders;
50
51 import java.awt.image.BufferedImage JavaDoc;
52 import java.io.ByteArrayOutputStream JavaDoc;
53 import java.io.IOException JavaDoc;
54 import java.io.OutputStream JavaDoc;
55 import java.util.Iterator JavaDoc;
56
57 import javax.imageio.IIOImage JavaDoc;
58 import javax.imageio.ImageIO JavaDoc;
59 import javax.imageio.ImageWriteParam JavaDoc;
60 import javax.imageio.ImageWriter JavaDoc;
61 import javax.imageio.stream.ImageOutputStream JavaDoc;
62
63 /**
64  * Adapter class for the Sun JPEG Encoder. The {@link ImageEncoderFactory}
65  * will only return a reference to this class by default if the library has
66  * been compiled under a JDK 1.4+ and is being run using a JRE 1.4+.
67  */

68 public class SunJPEGEncoderAdapter implements ImageEncoder {
69     
70     /** The quality setting (in the range 0.0f to 1.0f). */
71     private float quality = 0.95f;
72
73     /**
74      * Creates a new <code>SunJPEGEncoderAdapter</code> instance.
75      */

76     public SunJPEGEncoderAdapter() {
77     }
78
79     /**
80      * Returns the quality of the image encoding, which is a number in the
81      * range 0.0f to 1.0f (higher values give better quality output, but larger
82      * file sizes). The default value is 0.95f.
83      *
84      * @return A float representing the quality, in the range 0.0f to 1.0f.
85      *
86      * @see #setQuality(float)
87      */

88     public float getQuality() {
89         return this.quality;
90     }
91
92     /**
93      * Set the quality of the image encoding.
94      *
95      * @param quality A float representing the quality (in the range 0.0f to
96      * 1.0f).
97      *
98      * @see #getQuality()
99      */

100     public void setQuality(float quality) {
101         if (quality < 0.0f || quality > 1.0f) {
102             throw new IllegalArgumentException JavaDoc(
103                     "The 'quality' must be in the range 0.0f to 1.0f");
104         }
105         this.quality = quality;
106     }
107
108     /**
109      * Returns <code>false</code> always, indicating that this encoder does not
110      * encode alpha transparency.
111      *
112      * @return <code>false</code>.
113      */

114     public boolean isEncodingAlpha() {
115         return false;
116     }
117
118     /**
119      * Set whether the encoder should encode alpha transparency (this is not
120      * supported for JPEG, so this method does nothing).
121      *
122      * @param encodingAlpha ignored.
123      */

124     public void setEncodingAlpha(boolean encodingAlpha) {
125         // No op
126
}
127
128     /**
129      * Encodes an image in JPEG format.
130      *
131      * @param bufferedImage the image to be encoded (<code>null</code> not
132      * permitted).
133      *
134      * @return The byte[] that is the encoded image.
135      *
136      * @throws IOException if there is an I/O problem.
137      * @throws NullPointerException if <code>bufferedImage</code> is
138      * <code>null</code>.
139      */

140     public byte[] encode(BufferedImage JavaDoc bufferedImage) throws IOException JavaDoc {
141         ByteArrayOutputStream JavaDoc outputStream = new ByteArrayOutputStream JavaDoc();
142         encode(bufferedImage, outputStream);
143         return outputStream.toByteArray();
144     }
145
146     /**
147      * Encodes an image in JPEG format and writes it to an output stream.
148      *
149      * @param bufferedImage the image to be encoded (<code>null</code> not
150      * permitted).
151      * @param outputStream the OutputStream to write the encoded image to
152      * (<code>null</code> not permitted).
153      *
154      * @throws IOException if there is an I/O problem.
155      * @throws NullPointerException if <code>bufferedImage</code> is
156      * <code>null</code>.
157      */

158     public void encode(BufferedImage JavaDoc bufferedImage, OutputStream JavaDoc outputStream)
159             throws IOException JavaDoc {
160         if (bufferedImage == null) {
161             throw new IllegalArgumentException JavaDoc("Null 'image' argument.");
162         }
163         if (outputStream == null) {
164             throw new IllegalArgumentException JavaDoc("Null 'outputStream' argument.");
165         }
166         Iterator JavaDoc iterator = ImageIO.getImageWritersByFormatName("jpeg");
167         ImageWriter JavaDoc writer = (ImageWriter JavaDoc) iterator.next();
168         ImageWriteParam JavaDoc p = writer.getDefaultWriteParam();
169         p.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
170         p.setCompressionQuality(this.quality);
171         ImageOutputStream JavaDoc ios = ImageIO.createImageOutputStream(outputStream);
172         writer.setOutput(ios);
173         writer.write(null, new IIOImage JavaDoc(bufferedImage, null, null), p);
174         ios.flush();
175         writer.dispose();
176         ios.close();
177     }
178
179 }
180
Popular Tags