Frames | No Frames |
1: /* =========================================================== 2: * JFreeChart : a free chart library for the Java(tm) platform 3: * =========================================================== 4: * 5: * (C) Copyright 2000-2007, 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: * EncoderUtil.java 29: * ---------------- 30: * (C) Copyright 2004-2007, by Richard Atkinson and Contributors. 31: * 32: * Original Author: Richard Atkinson; 33: * Contributor(s): -; 34: * 35: * $Id: EncoderUtil.java,v 1.3.2.2 2007/02/02 14:51:22 mungady Exp $ 36: * 37: * Changes 38: * ------- 39: * 01-Aug-2004 : Initial version (RA); 40: * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG); 41: * 42: */ 43: 44: package org.jfree.chart.encoders; 45: 46: import java.awt.image.BufferedImage; 47: import java.io.IOException; 48: import java.io.OutputStream; 49: 50: /** 51: * A collection of utility methods for encoding images and returning them as a 52: * byte[] or writing them directly to an OutputStream. 53: */ 54: public class EncoderUtil { 55: 56: /** 57: * Encode the image in a specific format. 58: * 59: * @param image The image to be encoded. 60: * @param format The {@link ImageFormat} to use. 61: * 62: * @return The byte[] that is the encoded image. 63: * @throws IOException 64: */ 65: public static byte[] encode(BufferedImage image, String format) 66: throws IOException { 67: ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format); 68: return imageEncoder.encode(image); 69: } 70: 71: /** 72: * Encode the image in a specific format. 73: * 74: * @param image The image to be encoded. 75: * @param format The {@link ImageFormat} to use. 76: * @param encodeAlpha Whether to encode alpha transparency (not supported 77: * by all ImageEncoders). 78: * @return The byte[] that is the encoded image. 79: * @throws IOException 80: */ 81: public static byte[] encode(BufferedImage image, String format, 82: boolean encodeAlpha) throws IOException { 83: ImageEncoder imageEncoder 84: = ImageEncoderFactory.newInstance(format, encodeAlpha); 85: return imageEncoder.encode(image); 86: } 87: 88: /** 89: * Encode the image in a specific format. 90: * 91: * @param image The image to be encoded. 92: * @param format The {@link ImageFormat} to use. 93: * @param quality The quality to use for the image encoding (not supported 94: * by all ImageEncoders). 95: * @return The byte[] that is the encoded image. 96: * @throws IOException 97: */ 98: public static byte[] encode(BufferedImage image, String format, 99: float quality) throws IOException { 100: ImageEncoder imageEncoder 101: = ImageEncoderFactory.newInstance(format, quality); 102: return imageEncoder.encode(image); 103: } 104: 105: /** 106: * Encode the image in a specific format. 107: * 108: * @param image The image to be encoded. 109: * @param format The {@link ImageFormat} to use. 110: * @param quality The quality to use for the image encoding (not supported 111: * by all ImageEncoders). 112: * @param encodeAlpha Whether to encode alpha transparency (not supported 113: * by all ImageEncoders). 114: * @return The byte[] that is the encoded image. 115: * @throws IOException 116: */ 117: public static byte[] encode(BufferedImage image, String format, 118: float quality, boolean encodeAlpha) 119: throws IOException { 120: ImageEncoder imageEncoder 121: = ImageEncoderFactory.newInstance(format, quality, encodeAlpha); 122: return imageEncoder.encode(image); 123: } 124: 125: /** 126: * Encode the image in a specific format and write it to an OutputStream. 127: * 128: * @param image The image to be encoded. 129: * @param format The {@link ImageFormat} to use. 130: * @param outputStream The OutputStream to write the encoded image to. 131: * @throws IOException 132: */ 133: public static void writeBufferedImage(BufferedImage image, String format, 134: OutputStream outputStream) throws IOException { 135: ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format); 136: imageEncoder.encode(image, outputStream); 137: } 138: 139: /** 140: * Encode the image in a specific format and write it to an OutputStream. 141: * 142: * @param image The image to be encoded. 143: * @param format The {@link ImageFormat} to use. 144: * @param outputStream The OutputStream to write the encoded image to. 145: * @param quality The quality to use for the image encoding (not 146: * supported by all ImageEncoders). 147: * @throws IOException 148: */ 149: public static void writeBufferedImage(BufferedImage image, String format, 150: OutputStream outputStream, float quality) throws IOException { 151: ImageEncoder imageEncoder 152: = ImageEncoderFactory.newInstance(format, quality); 153: imageEncoder.encode(image, outputStream); 154: } 155: 156: /** 157: * Encode the image in a specific format and write it to an OutputStream. 158: * 159: * @param image The image to be encoded. 160: * @param format The {@link ImageFormat} to use. 161: * @param outputStream The OutputStream to write the encoded image to. 162: * @param encodeAlpha Whether to encode alpha transparency (not 163: * supported by all ImageEncoders). 164: * @throws IOException 165: */ 166: public static void writeBufferedImage(BufferedImage image, String format, 167: OutputStream outputStream, boolean encodeAlpha) throws IOException { 168: ImageEncoder imageEncoder 169: = ImageEncoderFactory.newInstance(format, encodeAlpha); 170: imageEncoder.encode(image, outputStream); 171: } 172: 173: /** 174: * Encode the image in a specific format and write it to an OutputStream. 175: * 176: * @param image The image to be encoded. 177: * @param format The {@link ImageFormat} to use. 178: * @param outputStream The OutputStream to write the encoded image to. 179: * @param quality The quality to use for the image encoding (not 180: * supported by all ImageEncoders). 181: * @param encodeAlpha Whether to encode alpha transparency (not supported 182: * by all ImageEncoders). 183: * @throws IOException 184: */ 185: public static void writeBufferedImage(BufferedImage image, String format, 186: OutputStream outputStream, float quality, boolean encodeAlpha) 187: throws IOException { 188: ImageEncoder imageEncoder 189: = ImageEncoderFactory.newInstance(format, quality, encodeAlpha); 190: imageEncoder.encode(image, outputStream); 191: } 192: 193: }