Capture screen shot in Java

It’s really easy to capture screen shots using java code. Let’s see how:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package screenshot;
 
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
 
public class Screenshot {
    public static void main(String[] args) throws IOException {
        try {
            Dimension ss = Toolkit.getDefaultToolkit().getScreenSize();
            Rectangle rec = new Rectangle(ss);
            Robot robot = new Robot();
            BufferedImage img = robot.createScreenCapture(rec);
            ImageIO.write(img, "png", new File("/home/developer/screenshot.png"));
        } catch (AWTException ex) {}
    }
}
%d bloggers like this: