Source Code Cross Referenced for SaveGraphicsService.java in  » Testing » jakarta-jmeter » org » apache » jmeter » save » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » jakarta jmeter » org.apache.jmeter.save 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.