Java: Screen Capture using Robot and save

Robot : java.awt.Robot  is used to take control of the mouse and keyboard. It is used for test automation, self-running demos and screen capture at various state of execution of the program.
Java Code for detecting screen size and capturing whole screen using Robot and saving :

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 javax.imageio.ImageIO; public class ScreenCapture { public static void main(String[] args) { saveScreen("capturedImage"); } public static boolean saveScreen(String fileNameJpeg) { try { // Get the "actual" screen size Dimension scr = Toolkit.getDefaultToolkit().getScreenSize(); System.out.println("(" + scr.width + "," + scr.height + ")"); // Allocate a Robot instance Robot robot = new Robot(); Rectangle rect = new Rectangle(0, 0, scr.width, scr.height); //screen capture BufferedImage image = robot.createScreenCapture(rect); // Save the captured image to file with ImageIO ImageIO.write(image, "jpeg", new File(fileNameJpeg + ".jpg")); return true; } catch (Exception ex) { System.out.println(ex.toString()); return false; } } } Screen Capture/logger is also available : Periodic screen capture- screen logger

No comments :

Post a Comment

Your Comment and Question will help to make this blog better...