Hello there,
In this blog I will provide a solution to how you can get screenshot of your screen and then save it at any location of your choice for this, we will use java.awt.Robot for creating an image which will generated by reading pixels from the screen. You can then write that image to a file on disk.
Java code for taking screenshot
import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Objects;
import javax.imageio.ImageIO;
public class ScreenshotDemo {
public static void main(String[] args) {
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage capture;
String str = Objects.toString(System.currentTimeMillis());
try {
capture = new Robot().createScreenCapture(screenRect);
try {
ImageIO.write(capture, "png", new File(str));
System.out.println("Image sucessfully created");
} catch (IOException e) {
e.printStackTrace();
}
} catch (AWTException e) {
e.printStackTrace();
}
}
}
Image generated by the above program
Thanks for reading :)
0 Comment(s)