How to capture computer screen using Java?

UPDATED: 31 May 2014
screen capture macbook and desktop

Abstract Window Toolkit (AWT)
           AWT used to create and manage windows. It is used to create windows that run in a GUI environment. Swing is built on base of AWT. Swing is using AWT directly or indirectly to provide you richer user interface. AWT is placed under java.awt package.

We will use Abstract Window Toolkit to create screen capture program. For the sake of convenience, I’m executing example from main method. Following example will capture whole screen of your system.
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;

/**
 * @author javaQuery
 */
public class ImageCapture {
    public static void main(String[] args) throws AWTException, IOException {
        /* File location is C:\Users\computerName\screenshot.png */
        File saveImage = new File(System.getProperty("user.home")+"\\screenshot.png");
        /* Define how much area you want to capture */
        Rectangle rectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); 
        /* Capture image */
        BufferedImage imageCapture = new Robot().createScreenCapture(rectangle);
        /* Write BufferedImage to file */
        ImageIO.write(imageCapture, "png", saveImage);
    }
}

Variations

Rectangle(int width, int height);
Rectangle rectangle = new Rectangle(100,100);
It will capture an image of 100 x 100 (WxH) from top left corner of screen.

Rectangle(Dimension(int width, int height));
Rectangle rectangle = new Rectangle(new Dimension(100, 100));
It will capture an image of 100 x 100 (WxH) as specified its dimension. It’s same as above specification. To use dimension you need to import java.awt.Dimension class in your code.

Rectangle(Point(int x, int y));
Rectangle rectangle = new Rectangle(new Point(20, 20));
/* setSize(int width, int height) */
rectangle.setSize(100, 100);
Point is used to specify PointX and PointY of screen. It capture image block of given size from PointX and PointY. In this case it will capture an image of 100 x 100 (WxH) from point x = 20 and point y = 20. To use Point you need to import java.awt.Point class in your code.

Rectangle(Point(int x, int y), Dimension(int width, int height))
Rectangle rectangle = new Rectangle(new Point(10,10), new Dimension(100, 100));
Using Point and Dimension at the same time. Capture 100 x 100 (WxH) image from point x = 10 and point y = 10. You need to import both class as follow java.awt.Point and java.awt.Dimension.

Rectangle(int x, int y, int width, int height)
Rectangle rectangle = new Rectangle(20, 20, 100, 100);
It’s same as specifing PointX and PointY of screen and its width - height. It will capture an image of 100 x 100 (WxH) from point x = 20 and point y = 20.

You can use this program for different purposes. One of them is Key Logger in Java.

0 comments :