From: <gjo...@us...> - 2007-01-19 18:53:47
|
Revision: 2166 http://abbot.svn.sourceforge.net/abbot/?rev=2166&view=rev Author: gjohnsto Date: 2007-01-19 10:51:11 -0800 (Fri, 19 Jan 2007) Log Message: ----------- Rewrite createScreenCapture() to use only SWT API (i.e., not use AWT API). Modified Paths: -------------- abbot.swt/trunk/abbot.swt/src/abbot/swt/utilities/ScreenCapture.java Modified: abbot.swt/trunk/abbot.swt/src/abbot/swt/utilities/ScreenCapture.java =================================================================== --- abbot.swt/trunk/abbot.swt/src/abbot/swt/utilities/ScreenCapture.java 2007-01-19 18:48:51 UTC (rev 2165) +++ abbot.swt/trunk/abbot.swt/src/abbot/swt/utilities/ScreenCapture.java 2007-01-19 18:51:11 UTC (rev 2166) @@ -1,77 +1,207 @@ package abbot.swt.utilities; -import java.awt.AWTException; -import java.awt.Dimension; -import java.awt.Rectangle; -import java.awt.Robot; -import java.awt.Toolkit; -import java.awt.image.BufferedImage; -import java.io.File; -import java.io.IOException; +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.ImageData; +import org.eclipse.swt.graphics.ImageLoader; +import org.eclipse.swt.graphics.Rectangle; +import org.eclipse.swt.widgets.Display; -import javax.imageio.ImageIO; +import abbot.swt.Robot; /** - * dumps a png of the current screen in the current directory + * Captures an image from a {@link Display} and saves it to a file. * + * @see Robot#createScreenCapture(Rectangle) + * @see Robot#createScreenCapture() */ -public class ScreenCapture -{ - private static final String BASE_IMAGE_NAME = "screenshot"; - private static final String IMAGE_EXT = "png"; // best format for ui graphics +public class ScreenCapture { - // increment counter for unique screenshot file names - // on a per run basis - private static int _counter = 0; + /** + * The default file name (in the current directory). + */ + public static final String DEFAULT_FILENAME = "capture"; - // keep a static Robot around to do the screencapture - private static Robot _robot; - - static - { - try - { - _robot = new Robot(); - } - catch ( AWTException e ) - { - throw new RuntimeException( "Could not instantiate awt Robot for screen capture", e ); - } - } - - /** - * purely static class, no instances allowed - */ - private ScreenCapture() - { - } + /** + * The default image file format to save as. + */ + public static final int DEFAULT_FORMAT = SWT.IMAGE_PNG; - /** - * Save the screen pixels as a PNG image file in the current directory. - * Existing screen cap files will be overwritten. - * - * The name parameter will be used as a prefix for the name of the - * produced image. - * - * TODO parameterize the image file location - */ - public static void createScreenCapture(String name) - { - try - { - // determine current screen size - Toolkit toolkit = Toolkit.getDefaultToolkit(); - Dimension screenSize = toolkit.getScreenSize(); - Rectangle screenRect = new Rectangle( screenSize ); - // create screen shot using an AWT Robot - BufferedImage image = _robot.createScreenCapture( screenRect ); - // save captured image to PNG file - ImageIO.write( image, IMAGE_EXT, new File( name+"_"+BASE_IMAGE_NAME + "_" - + _counter++ + "." + IMAGE_EXT ) ); - } - catch ( IOException e ) - { - System.err.println("Error creating screen capture: " + e.getMessage() ); - } - } + /** No instances. */ + private ScreenCapture() {} + + /** + * Capture a rectangular area of a display and save it to a file with a specified format. + * + * @param display + * the {@link Display} to capture from + * @param area + * the {@link Rectangle} area in the {@link Display} to be captured + * @param name + * the name (including path, if desired) of the target file + * @param format + * the image file format to use (see {@link SWT#IMAGE_PNG} et al.) + */ + public static void capture(Display display, Rectangle area, String name, int format) { + Image image = null; + try { + image = getImage(display, area); + String filename = getFilename(name, format); + saveImage(image, filename, format); + } finally { + if (image != null) + image.dispose(); + } + } + + /** + * Capture a rectangular area of a display and save it to a file with the default format. + * + * @param display + * the {@link Display} to capture from + * @param area + * the {@link Rectangle} area in the {@link Display} to be captured + * @param name + * the name (including path, if desired) of the target file + * @see #DEFAULT_FORMAT + */ + public static void capture(Display display, Rectangle area, String name) { + capture(display, area, name, DEFAULT_FORMAT); + } + + /** + * Capture a rectangular area of a display and save it to a file with the default name and + * format. + * + * @param display + * the {@link Display} to capture from + * @param area + * the {@link Rectangle} area in the {@link Display} to be captured + * @see #DEFAULT_FORMAT + * @see #DEFAULT_FILENAME + */ + public static void capture(Display display, Rectangle area) { + capture(display, area, DEFAULT_FILENAME); + } + + /** + * Capture an entire display and save it to a file with a specified format. + * + * @param display + * the {@link Display} to capture from + * @param name + * the name (including path, if desired) of the target file + * @param format + * the image file format to use (see {@link SWT#IMAGE_PNG} et al.) + */ + public static void capture(Display display, String name, int format) { + capture(display, display.getBounds(), name, format); + } + + /** + * Capture an entire display and save it to a file with the default format. + * + * @param display + * the {@link Display} to capture from + * @param name + * the name (including path, if desired) of the target file + * @see #DEFAULT_FORMAT + */ + public static void capture(Display display, String name) { + capture(display, name, DEFAULT_FORMAT); + } + + /** + * Capture an entire display and save it to a file with the default name and format. + * + * @param display + * the {@link Display} to capture from + * @see #DEFAULT_FORMAT + * @see #DEFAULT_FILENAME + */ + public static void capture(Display display) { + capture(display, DEFAULT_FILENAME); + } + + /** + * Captures a {@link Rectangle} area of a {@link Display} and returns it as an {@link Image}. + * <p> + * <strong>Note:</strong> The caller is responsible for disposing the {@link Image} that is + * returned from this method. + * + * @param display + * the {@link Display} to capture from + * @param area + * the {@link Rectangle} area of the {@link Display} to capture + * @return an {@link Image} + * @see Robot#createScreenCapture(Rectangle) + */ + private static Image getImage(Display display, Rectangle area) { + Robot robot = new Robot(display); + return robot.createScreenCapture(area); + } + + /** + * Gets an appropriate file name to use for saving an image in a particular format. + * + * @param filename + * the proposed file name + * @param format + * the image file format + * @return the file name, which will be either the original filename (if it already had the + * appropriate extension for the specified format) or the filename with the appropriate + * extension appended. + */ + private static String getFilename(String filename, int format) { + String extension = getExtension(format); + if (filename.endsWith(extension)) + return filename; + return filename + extension; + } + + /** + * Gets the appropriate file name extension for a specified image file format. + * + * @param format + * the image file format + * @return the appropriate corresponding file name extension + */ + private static String getExtension(int format) { + switch (format) { + case SWT.IMAGE_PNG: + return ".png"; + case SWT.IMAGE_JPEG: + return ".jpg"; + case SWT.IMAGE_GIF: + return ".gif"; + case SWT.IMAGE_BMP: + case SWT.IMAGE_BMP_RLE: + case SWT.IMAGE_OS2_BMP: + return ".bmp"; + case SWT.IMAGE_ICO: + return ".ico"; + case SWT.IMAGE_TIFF: + return ".tif"; + default: + SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT); + } + throw new RuntimeException("unreachable"); + } + + /** + * Saves an {@link Image} to a file with a specified image file format. + * + * @param image + * the {@link Image} to save + * @param filename + * the name (including path, if desired) of the target file + * @param format + * the image file format to use + */ + private static void saveImage(Image image, String filename, int format) { + ImageLoader loader = new ImageLoader(); + loader.data = new ImageData[] { image.getImageData() }; + loader.save(filename, format); + } + } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |