001: /***********************************************************************************************
002: * Copyright 2002 (C) Nathaniel G. Auvil. All Rights Reserved.
003: *
004: * Redistribution and use of this software and associated documentation ("Software"), with or
005: * without modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain copyright statements and notices.
008: * Redistributions must also contain a copy of this document.
009: *
010: * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
011: * conditions and the following disclaimer in the documentation and/or other materials
012: * provided with the distribution.
013: *
014: * 3. The name "jCharts" or "Nathaniel G. Auvil" must not be used to endorse or promote
015: * products derived from this Software without prior written permission of Nathaniel G.
016: * Auvil. For written permission, please contact nathaniel_auvil@users.sourceforge.net
017: *
018: * 4. Products derived from this Software may not be called "jCharts" nor may "jCharts" appear
019: * in their names without prior written permission of Nathaniel G. Auvil. jCharts is a
020: * registered trademark of Nathaniel G. Auvil.
021: *
022: * 5. Due credit should be given to the jCharts Project (http://jcharts.sourceforge.net/).
023: *
024: * THIS SOFTWARE IS PROVIDED BY Nathaniel G. Auvil AND CONTRIBUTORS ``AS IS'' AND ANY
025: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
026: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * jCharts OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
028: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
029: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
030: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,STRICT LIABILITY, OR TORT
031: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
032: * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
033: ************************************************************************************************/package org.krysalis.jcharts.encoders;
034:
035: import org.krysalis.jcharts.Chart;
036: import org.krysalis.jcharts.chartData.ChartDataException;
037: import org.krysalis.jcharts.properties.PropertyException;
038:
039: import javax.imageio.IIOImage;
040: import javax.imageio.ImageIO;
041: import javax.imageio.ImageTypeSpecifier;
042: import javax.imageio.ImageWriteParam;
043: import javax.imageio.ImageWriter;
044: import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
045: import javax.imageio.stream.ImageOutputStream;
046: import java.awt.image.BufferedImage;
047: import java.awt.image.IndexColorModel;
048: import java.io.IOException;
049: import java.io.OutputStream;
050: import java.util.Iterator;
051:
052: /*************************************************************************************
053: * This class REQUIRES the jdk 1.4
054: *
055: * @author Nathaniel Auvil
056: * @version $Id: JPEGEncoder.java,v 1.2 2003/05/26 13:40:20 nathaniel_auvil Exp $
057: ************************************************************************************/
058: public class JPEGEncoder {
059:
060: private static final String JPEG = "jpeg";
061:
062: static {
063: //---do not use a file cache as hurts performance
064: ImageIO.setUseCache(false);
065: }
066:
067: /******************************************************************************************
068: *
069: *
070: ******************************************************************************************/
071: private JPEGEncoder() throws Exception {
072: throw new Exception(
073: "No need to create an instance of this class!");
074: }
075:
076: /******************************************************************************************
077: * Encodes the chart to a JPEG format. If you are generating large dimension images, the file
078: * size can get quite large. You can try decreasing the quality to decrease the file size.
079: *
080: * @param outputStream
081: * @param quality float value from 0.0f(worst image quality) - 1.0f(best image quality)
082: * @throws ChartDataException
083: * @throws PropertyException
084: * @throws IOException
085: *******************************************************************************************/
086: public static void encode(Chart chart, float quality,
087: OutputStream outputStream) throws ChartDataException,
088: PropertyException, IOException {
089: BufferedImage bufferedImage = BinaryEncoderUtil.render(chart);
090:
091: Iterator writers = ImageIO.getImageWritersByFormatName(JPEG);
092: ImageWriter imageWriter = (ImageWriter) writers.next();
093:
094: JPEGImageWriteParam params = new JPEGImageWriteParam(null);
095: params.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
096: params.setCompressionQuality(quality);
097: params
098: .setProgressiveMode(javax.imageio.ImageWriteParam.MODE_DISABLED);
099: params.setDestinationType(new ImageTypeSpecifier(
100: IndexColorModel.getRGBdefault(), IndexColorModel
101: .getRGBdefault().createCompatibleSampleModel(
102: 16, 16)));
103:
104: ImageOutputStream imageOutputStream = ImageIO
105: .createImageOutputStream(outputStream);
106: imageWriter.setOutput(imageOutputStream);
107: imageWriter.write(null,
108: new IIOImage(bufferedImage, null, null), params);
109:
110: imageOutputStream.close();
111:
112: imageWriter.dispose();
113: }
114: }
|