Thursday, January 7, 2016

Capture screen using java.awt.Robot

Java example to capture screen using java.awt.Robot:

package javarobotcapturescreen;

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 java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaRobotCaptureScreen {

    public static void main(String[] args) {
        try {
            Robot robot = new Robot();
            
            Toolkit myToolkit = Toolkit.getDefaultToolkit();
            Dimension screenSize = myToolkit.getScreenSize();
            
            Rectangle screen = new Rectangle(screenSize);
            
            BufferedImage screenFullImage = robot.createScreenCapture(screen);
            ImageIO.write(screenFullImage, "jpg", new File("screen.jpg"));
            
        } catch (AWTException | IOException ex) {
            Logger.getLogger(JavaRobotCaptureScreen.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
}




captured screen:


No comments:

Post a Comment