Saturday, 9 January 2016

Screen capture using java

Screen capturing using Java
java.awt.Robot,this class is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed. The primary purpose of Robot is to facilitate automated testing of Java platform implementations.
Here is one example to capture the current screen using java.awt.Robot class.
package com.vinod.test;

import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import javax.imageio.ImageIO;

public class JavaScreenCapture {

public static void main(String... args) throws Exception {
Robot robot = new Robot();
Rectangle rect = new Rectangle(
BufferedImage bImage = robot.createScreenCapture(rect);
String base64String = imgToBase64String(bImage, "png");

public static String imgToBase64String(final RenderedImage img,
final String formatName) throws IOException {
final ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(img, formatName, Base64.getEncoder().wrap(os));
return os.toString(;
The output of this program will be a base64 String and use any online tool to generate it as image ..use
Done !!