001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.jmeter.save;
019:
020: import java.awt.Dimension;
021: import java.awt.Graphics2D;
022: import java.awt.image.BufferedImage;
023: import java.io.File;
024: import java.io.FileNotFoundException;
025: import java.io.FileOutputStream;
026: import java.io.OutputStream;
027:
028: import javax.swing.JComponent;
029:
030: //import com.sun.image.codec.jpeg.JPEGCodec;
031: //import com.sun.image.codec.jpeg.JPEGEncodeParam;
032: //import com.sun.image.codec.jpeg.JPEGImageEncoder;
033:
034: import org.apache.batik.ext.awt.image.codec.PNGEncodeParam;
035: import org.apache.batik.ext.awt.image.codec.PNGImageEncoder;
036: import org.apache.batik.ext.awt.image.codec.tiff.TIFFEncodeParam;
037: import org.apache.batik.ext.awt.image.codec.tiff.TIFFImageEncoder;
038: import org.apache.jorphan.logging.LoggingManager;
039: import org.apache.jorphan.util.JOrphanUtils;
040: import org.apache.log.Logger;
041:
042: /**
043: * Class is responsible for taking a component and saving it as a JPEG, PNG or
044: * TIFF. The class is very simple. Thanks to Batik and the developers who worked
045: * so hard on it. The original implementation I used JAI, which allows
046: * redistribution but requires indemnification. Luckily Batik has an alternative
047: * to JAI. Hurray for Apache projects. I don't see any noticeable differences
048: * between Batik and JAI.
049: */
050: public class SaveGraphicsService {
051:
052: private static final Logger log = LoggingManager
053: .getLoggerForClass();
054:
055: public static final int PNG = 0;
056:
057: public static final int TIFF = 1;
058:
059: public static final String PNG_EXTENSION = ".png"; //$NON-NLS-1$
060:
061: public static final String TIFF_EXTENSION = ".tif"; //$NON-NLS-1$
062:
063: public static final String JPEG_EXTENSION = ".jpg"; //$NON-NLS-1$
064:
065: /**
066: *
067: */
068: public SaveGraphicsService() {
069: super ();
070: }
071:
072: /*
073: * This is not currently used by JMeter code.
074: * As it uses Sun-specific code (the only such in JMeter), it has been commented out for now.
075: */
076: // /**
077: // * If someone wants to save a JPEG, use this method. There is a limitation
078: // * though. It uses gray scale instead of color due to artifacts with color
079: // * encoding. For some reason, it does not translate pure red and orange
080: // * correctly. To make the text readable, gray scale is used.
081: // *
082: // * @param filename
083: // * @param component
084: // */
085: // public void saveUsingJPEGEncoder(String filename, JComponent component) {
086: // Dimension size = component.getSize();
087: // // We use Gray scale, since color produces poor quality
088: // // this is an unfortunate result of the default codec
089: // // implementation.
090: // BufferedImage image = new BufferedImage(size.width, size.height, BufferedImage.TYPE_USHORT_GRAY);
091: // Graphics2D grp = image.createGraphics();
092: // component.paint(grp);
093: //
094: // File outfile = new File(filename + JPEG_EXTENSION);
095: // FileOutputStream fos = createFile(outfile);
096: // JPEGEncodeParam param = JPEGCodec.getDefaultJPEGEncodeParam(image);
097: // Float q = new Float(1.0);
098: // param.setQuality(q.floatValue(), true);
099: // JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos, param);
100: //
101: // try {
102: // encoder.encode(image);
103: // } catch (Exception e) {
104: // log.warn(e.toString());
105: // } finally {
106: // JOrphanUtils.closeQuietly(fos);
107: // }
108: // }
109: /**
110: * Method will save the JComponent as an image. The formats are PNG, and
111: * TIFF.
112: *
113: * @param filename
114: * @param type
115: * @param component
116: */
117: public void saveJComponent(String filename, int type,
118: JComponent component) {
119: Dimension size = component.getSize();
120: BufferedImage image = new BufferedImage(size.width,
121: size.height, BufferedImage.TYPE_INT_RGB);
122: Graphics2D grp = image.createGraphics();
123: component.paint(grp);
124:
125: if (type == PNG) {
126: filename += PNG_EXTENSION;
127: this .savePNGWithBatik(filename, image);
128: } else if (type == TIFF) {
129: filename = filename + TIFF_EXTENSION;
130: this .saveTIFFWithBatik(filename, image);
131: }
132: }
133:
134: /**
135: * Use Batik to save a PNG of the graph
136: *
137: * @param filename
138: * @param image
139: */
140: public void savePNGWithBatik(String filename, BufferedImage image) {
141: File outfile = new File(filename);
142: OutputStream fos = createFile(outfile);
143: PNGEncodeParam param = PNGEncodeParam
144: .getDefaultEncodeParam(image);
145: PNGImageEncoder encoder = new PNGImageEncoder(fos, param);
146: try {
147: encoder.encode(image);
148: } catch (Exception e) {
149: // do nothing
150: } finally {
151: JOrphanUtils.closeQuietly(fos);
152: }
153: }
154:
155: /**
156: * Use Batik to save a TIFF file of the graph
157: *
158: * @param filename
159: * @param image
160: */
161: public void saveTIFFWithBatik(String filename, BufferedImage image) {
162: File outfile = new File(filename);
163: OutputStream fos = createFile(outfile);
164: TIFFEncodeParam param = new TIFFEncodeParam();
165: TIFFImageEncoder encoder = new TIFFImageEncoder(fos, param);
166: try {
167: encoder.encode(image);
168: } catch (Exception e) {
169: // do nothing
170: } finally {
171: JOrphanUtils.closeQuietly(fos);
172: }
173: }
174:
175: /**
176: * Create a new file for the graphics. Since the method creates a new file,
177: * we shouldn't get a FNFE.
178: *
179: * @param filename
180: * @return output stream created from the filename
181: */
182: public FileOutputStream createFile(File filename) {
183: try {
184: return new FileOutputStream(filename);
185: } catch (FileNotFoundException e) {
186: log.warn(e.toString());
187: return null;
188: }
189: }
190:
191: }
|