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:

  • Regular expression to extract the src tag details from given image tag or html source text By using the regular expression we can extract the any data from the given input, here we are trying to get the src attribute value from the given image tag or html text data. Image 'src' attribute extract Regx: <img[^&… 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
  • 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
  • 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

0 comments:

Post a Comment