Java Tutorial : How to make PC screenShot program with Java #1 - Single Shot
What Will I Learn?
- You will learn How to make a screen shot program using java classes
Requirements
- System Requirements: Java JDK, Eclipse IDE or Netbeans IDE
- OS Support for Java:Windows, macOS, Linux
- Required Knowledge A fair knowlege of java Basics and fundamentals
Resources for Java and this tutorial:
- Oracle Website: https://www.oracle.com/index.html
- Java Docs:http://www.oracle.com/technetwork/java/javase/documentation/api-jsp-136079.html
- others : https://www.javaworld.com/article/2073446/screen-snapshots-with-java-s-robot.html
Description
Outline and Overview
Java provides API that allows Screen captured of your PC screen. Java robot class provides the method to enable this and it could be used to make screen shot programs or even video recorder program.
Classes needed
- File - Used to create a new file
- Rectangle - Used to get the Screen Dimension
- Robot - Does the actual screen capture and returns a bufferedImage object
- Bufferedimage - used to store the captured screen
- ImageIO - Used to write the buffered image to file
Before we start coding, lets create a new project
STEP 1
// create a new file
File myFileOutput = new File("screen.png");
STEP 2
// create a new Robot object
Robot robot = new Robot();
STEP 3
//get screen rectangle
Rectangle screenRectangle = new Rectangle(Toolkit.getDefaultToolkit()
.getScreenSize());
STEP4
//capture current screen and save to Screencapture
BufferedImage Screencapture = robot.createScreenCapture(screenRectangle);
STEP5
//write Screencapture to file
ImageIO.write(Screencapture, "png", myFileOutput);
This is the image of the code
Run the program and get something like this
The Video Tutorial for this :