KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > krysalis > jcharts > encoders > JPEGEncoder


1 /***********************************************************************************************
2  * Copyright 2002 (C) Nathaniel G. Auvil. All Rights Reserved.
3  *
4  * Redistribution and use of this software and associated documentation ("Software"), with or
5  * without modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain copyright statements and notices.
8  * Redistributions must also contain a copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
11  * conditions and the following disclaimer in the documentation and/or other materials
12  * provided with the distribution.
13  *
14  * 3. The name "jCharts" or "Nathaniel G. Auvil" must not be used to endorse or promote
15  * products derived from this Software without prior written permission of Nathaniel G.
16  * Auvil. For written permission, please contact nathaniel_auvil@users.sourceforge.net
17  *
18  * 4. Products derived from this Software may not be called "jCharts" nor may "jCharts" appear
19  * in their names without prior written permission of Nathaniel G. Auvil. jCharts is a
20  * registered trademark of Nathaniel G. Auvil.
21  *
22  * 5. Due credit should be given to the jCharts Project (http://jcharts.sourceforge.net/).
23  *
24  * THIS SOFTWARE IS PROVIDED BY Nathaniel G. Auvil AND CONTRIBUTORS ``AS IS'' AND ANY
25  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * jCharts OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
32  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
33  ************************************************************************************************/

34
35 package org.krysalis.jcharts.encoders;
36
37
38 import org.krysalis.jcharts.Chart;
39 import org.krysalis.jcharts.chartData.ChartDataException;
40 import org.krysalis.jcharts.properties.PropertyException;
41
42 import javax.imageio.IIOImage JavaDoc;
43 import javax.imageio.ImageIO JavaDoc;
44 import javax.imageio.ImageTypeSpecifier JavaDoc;
45 import javax.imageio.ImageWriteParam JavaDoc;
46 import javax.imageio.ImageWriter JavaDoc;
47 import javax.imageio.plugins.jpeg.JPEGImageWriteParam JavaDoc;
48 import javax.imageio.stream.ImageOutputStream JavaDoc;
49 import java.awt.image.BufferedImage JavaDoc;
50 import java.awt.image.IndexColorModel JavaDoc;
51 import java.io.IOException JavaDoc;
52 import java.io.OutputStream JavaDoc;
53 import java.util.Iterator JavaDoc;
54
55
56 /*************************************************************************************
57  * This class REQUIRES the jdk 1.4
58  *
59  * @author Nathaniel Auvil
60  * @version $Id: JPEGEncoder.java,v 1.2 2003/05/26 13:40:20 nathaniel_auvil Exp $
61  ************************************************************************************/

62 public class JPEGEncoder
63 {
64
65
66     private static final String JavaDoc JPEG = "jpeg";
67
68
69     static
70     {
71         //---do not use a file cache as hurts performance
72
ImageIO.setUseCache( false );
73     }
74
75
76     /******************************************************************************************
77      *
78      *
79      ******************************************************************************************/

80     private JPEGEncoder() throws Exception JavaDoc
81     {
82         throw new Exception JavaDoc( "No need to create an instance of this class!" );
83     }
84
85
86     /******************************************************************************************
87      * Encodes the chart to a JPEG format. If you are generating large dimension images, the file
88      * size can get quite large. You can try decreasing the quality to decrease the file size.
89      *
90      * @param outputStream
91      * @param quality float value from 0.0f(worst image quality) - 1.0f(best image quality)
92      * @throws ChartDataException
93      * @throws PropertyException
94      * @throws IOException
95      *******************************************************************************************/

96     public static void encode( Chart chart,
97                                         float quality,
98                                         OutputStream JavaDoc outputStream ) throws ChartDataException, PropertyException, IOException JavaDoc
99     {
100         BufferedImage JavaDoc bufferedImage = BinaryEncoderUtil.render( chart );
101
102         Iterator JavaDoc writers = ImageIO.getImageWritersByFormatName( JPEG );
103         ImageWriter JavaDoc imageWriter = (ImageWriter JavaDoc) writers.next();
104
105         JPEGImageWriteParam JavaDoc params = new JPEGImageWriteParam JavaDoc( null );
106         params.setCompressionMode( ImageWriteParam.MODE_EXPLICIT );
107         params.setCompressionQuality( quality );
108         params.setProgressiveMode( javax.imageio.ImageWriteParam.MODE_DISABLED );
109         params.setDestinationType( new ImageTypeSpecifier JavaDoc( IndexColorModel.getRGBdefault(),
110                                                                             IndexColorModel.getRGBdefault().createCompatibleSampleModel( 16, 16 ) ) );
111
112         ImageOutputStream JavaDoc imageOutputStream = ImageIO.createImageOutputStream( outputStream );
113         imageWriter.setOutput( imageOutputStream );
114         imageWriter.write( null, new IIOImage JavaDoc( bufferedImage, null, null ), params );
115
116         imageOutputStream.close();
117
118         imageWriter.dispose();
119     }
120 }
121
122
123
Popular Tags