Monday, August 20, 2007

Screen Capture

The java.awt.Robot class provides several opportunities for fun. One opportunity involves building a screen-capture utility. This Java code uses Robot to capture the contents of the primary screen device.

To capture the screen with the Robot class, Capture must first create a Robot object. ScreenCapture class's printScreen()method attempts to create this object by invoking Robot's public Robot() constructor. If successful, a reference to a Robot configured to the primary screen device's coordinate system returns. If the platform does not support low-level control , a java.awt.AWTException is thrown. A java.lang.SecurityException is thrown if the platform doesn't grant permission to create a Robot. Hopefully, you will not encounter either exception.

Assuming a Robot object is created, createScreenCapture() invokes the Capture class's constructor .Capture obtains the dimensions of the primary screen device by invoking Toolkit.getDefaultToolkit ().getScreenSize ();. Because Robot's public BufferedImage createScreenCapture(Rectangle screenRect) method, which is used to perform a screen capture, requires a java.awt.Rectangle argument, the constructor converts the java.awt.Dimension object to a Rectangle object via rectScreenSize = new Rectangle(dimScreenSize);. The screen capture is done with java.awt.Robot.

Then call createScreenCapture() by passing required parameters through the reference of ScreenCapture Class where your application needs to capture the screen, or from public static void main(String[] a).

Code:
import java.awt.AWTException;
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 javax.imageio.ImageIO;
import javax.swing.JFrame;

class ScreenCapture {
public void printScreen() throws AWTException, IOException {
// capture the whole screen
BufferedImage screencapture = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()) );
// Save as JPEG
File file = new File("screencapture.jpg");
ImageIO.write(screencapture, "jpg", file);

// Save as PNG
File file = new File("screencapture.png");
ImageIO.write(screencapture, "png", file);
}

//To capture a specific area
public void printScreen(int x, int y, int width, int height) throws AWTException, IOException {
// capture the whole screen
BufferedImage screencapture = new Robot().createScreenCapture( new Rectangle( x, y, width, height));
// Save as JPEG
File file = new File("screencapture.jpg");
ImageIO.write(screencapture, "jpg", file);

// Save as PNG
File file = new File("screencapture.png");
ImageIO.write(screencapture, "png", file);
}

//To capture a specific visual object
public void printScreen(JFrame myframe) throws AWTException, IOException {
// capture the whole screen
BufferedImage screencapture = new Robot().createScreenCapture(new Rectangle( myframe.getX(), myframe.getY(), myframe.getWidth(), myframe.getHeight() ) );
// Save as JPEG
File file = new File("screencapture.jpg");
ImageIO.write(screencapture, "jpg", file);

// Save as PNG
File file = new File("screencapture.png");
ImageIO.write(screencapture, "png", file);
}
}

No comments: