Pin It

How to Capture Screen through a Java program using Robot class ?.

Program to demonstrate screen capture through a Java program using Robot class.

package com.hubberspot.example;

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

import javax.imageio.ImageIO;

public class RobotScreenCapture {
   public static void main(String[] args) {
 try {
 // 1. Create a Robot Object 
 Robot robot = new Robot();
        // 2. Create a Dimension Object for the screen capture 
 // by providing width and height for the screen area 
 Dimension dimension = new Dimension(300,300);
 // 3. Create a Rectangle by taking the dimension reference
 // to create a screen capture of area specified by dimension 
 Rectangle screen = new Rectangle(dimension);
 // 4. Calling createScreenCapture method of Robot class by 
 // passing it Rectangle reference. It will return a reference 
 // to a BufferedImage Object.  
 BufferedImage buffer = robot.createScreenCapture(screen);
 // 5. Create a file to store the image 
 File screenCapture = new File("screen.jpg");
 // 6. Call the write method of ImageIO class by passing
 // it BufferedImage and File created above 
 ImageIO.write(buffer, "jpg", screenCapture);
 } catch (AWTException e) {   
 } catch (IOException e) {  



Output of the program :

Please Give Us Your 1 Minute In Sharing This Post!
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
© 2012 Learn Java by Examples Template by Hubber Spot