Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture only a desired portion of screen using createScreenCapture

The following code captures the screen:

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;


public class capture{
    public static void main(String args[]) { 

        try { 
            Dimension size = Toolkit.getDefaultToolkit().getScreenSize(); 
            Robot robot = new Robot(); 
            BufferedImage img = robot.createScreenCapture(new Rectangle(size)); 
        } catch(Exception e) { 
        } 

    }
}

Is there a way, to capture only a desired portion of the screen (e.g. a rectangle, from one x,y point to another)?

like image 392
Kumara Avatar asked May 14 '14 16:05

Kumara


1 Answers

You can set the x and y of the top left corner, along with the width, and height dimensions of the rectangle to capture like this:

BufferedImage img = robot.createScreenCapture( new Rectangle(x, y, width, height) );
like image 95
sfletche Avatar answered Sep 18 '22 10:09

sfletche