KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > apps > ScreenShot


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2002 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.apps;
15
16 import java.io.*;
17 import java.awt.*;
18 import java.awt.image.*;
19 import java.util.*;
20 import javax.swing.*;
21 import javax.imageio.*;
22 import javax.imageio.stream.*;
23
24 import com.sun.image.codec.jpeg.*;
25 import org.compiere.util.*;
26
27 /**
28  * JPEG File Utility
29  *
30  * @author Jorg Janke
31  * @version $Id: ScreenShot.java,v 1.2 2002/09/21 04:01:26 jjanke Exp $
32  */

33 public class ScreenShot
34 {
35     /**
36      * Create JPEG file from window
37      * @param window window
38      * @param fileName optional file name
39      * @return true if created
40      */

41     public static boolean createJPEG (Window window, String JavaDoc fileName)
42     {
43         if (window == null || fileName == null)
44             new IllegalArgumentException JavaDoc("ScreenShot.createJPEG Window os NULL");
45
46         // Get File
47
File file = getJPGFile (window);
48         if (file == null)
49             return false;
50         Log.trace(Log.l3_Util, "ScreenShot.createJPEG", "File=" + file);
51         if (file.exists())
52             file.delete();
53
54         // Get Writer
55
Iterator writers = ImageIO.getImageWritersByFormatName("jpg");
56         ImageWriter writer = (ImageWriter)writers.next();
57         if (writer == null)
58         {
59             Log.error("ScreenShot.createJPEG - no ImageWriter");
60             return false;
61         }
62
63         // Get Image
64
BufferedImage bi = getImage(window);
65
66         // Write Image
67
try
68         {
69             ImageOutputStream ios = ImageIO.createImageOutputStream (file);
70             writer.setOutput(ios);
71             writer.write(bi);
72             ios.flush();
73             ios.close();
74
75         }
76         catch (IOException ex)
77         {
78             Log.error("ScreenShot.createJPEG", ex);
79             return false;
80         }
81         return true;
82     } // createJPEG
83

84
85     /**
86      * Get JPEG File
87      * @param parent parent
88      * @return file
89      */

90     protected static File getJPGFile (Component parent)
91     {
92         JFileChooser fc = new JFileChooser();
93         fc.addChoosableFileFilter(new ExtensionFileFilter("jpg", "JPEG"));
94         if (fc.showSaveDialog(parent) != JFileChooser.APPROVE_OPTION)
95             return null;
96         File file = fc.getSelectedFile();
97         if (file == null)
98             return null;
99         String JavaDoc fileName = file.getAbsolutePath();
100         if (!(fileName.toUpperCase().equals(".JPG") || fileName.toUpperCase().equals(".JPEG")))
101             fileName += ".jpg";
102         return new File (fileName);
103     } // getFile
104

105     /**
106      * Get Image of Window
107      * @param window window
108      * @return image
109      */

110     protected static BufferedImage getImage (Window window)
111     {
112         BufferedImage bi = new BufferedImage (window.getWidth(), window.getHeight(),
113             BufferedImage.TYPE_INT_RGB); // TYPE_INT_ARGB is tinted red
114
window.paintAll(bi.createGraphics());
115         return bi;
116     } // getImage
117

118 } // ScreenShot
119
Popular Tags