Tuesday, February 17, 2015

Screen capture by Java robot class

Came across a rare util class of java "Robot" class, which provie the control/auto typing of letter/keyboard controll and screen capering.

sample code
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;
/**
*
* @author Rivet Systems
*/
public class RobotSample {
public static void main(String[] args) throws Exception {
for(int i=0;i<2;i++){
captureScreen(System.currentTimeMillis()+"_"+i);
}
}
public static void captureScreen(String fileName) throws Exception {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = new Rectangle(screenSize);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(screenRectangle);
ImageIO.write(image, "png", new File(fileName));
}
}
view raw RobotSample hosted with ❤ by GitHub

Related Posts:

  • Generate PDF page as imageWe can generate the image of pdf page by using the Pdf-renderer Simple class which generate the images file of given pdf file. /* * To change this license header, choose License Headers in Project Properties. * To change … Read More
  • Reverse elements in ArrayReverse all elements in array, traditional way how we use to do is get the array and iterate through the for loop and use swap logic to move to temporary array and get the result. But from jdk 1.5 introduced reverse() in ja… Read More
  • Spring + JPA + Hibernate Sample IntroductionStand-alone Spring application sample, Spring framework supports the persistence layer to be fully implemented through JPA. Simple example of configure the Spring needed components to perform persistence over sta… Read More
  • Compare images are same by javaWe can compare the given images are same or not by comparing the buffer data of the image. 1. Compare the image sizes are same or not. 2. Compare the binary data of two images are same or not. sample code : import java.awt… Read More
  • find character uppercase/lowercase count from given textFind the character upper/lower case occurrence count with simple single line code without iteration in java. int counta = text.split("(?=[a])").length - 1; This simple code will find all the lowercase character 'a… Read More

0 comments:

Post a Comment