Take Screen Shot Using Java Program

Here I have attached, Simple program about How to Take Screen Shot Using Java Program. The example program has been tested and output is shared.

MyScreenCaptureTool.java

package com.javatraineronline;
import javax.swing.JButton;
import javax.swing.JFrame;
public class MyScreenCaptureTool extends JFrame {
private JButton captureButton;
private String outputPath;
public MyScreenCaptureTool(String title, String outputPath) {
this.outputPath = outputPath;
super.setTitle(title);
super.setSize(400, 150);
super.setVisible(true);
// Defining the JButton Object
captureButton = new JButton("Click here to Capture Screen");
// ActionListener for button click event.
captureButton.addActionListener(new ClickListener(this));
// Adding the button object to JFrame
add(captureButton);
}
public String getOutputPath() {
return outputPath;
}
public static void main(String[] args) {
// Defining the MyScreenCaptureTool Object by passing 'Title' and 'Output Path' should be with '\\'
new MyScreenCaptureTool("My Screen Capture Tool", "C:\\Users\\dineshkrishnan\\Desktop\\images\\"); // You can change accordingly
}
}

ClickListener.java

package com.javatraineronline;
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
import javax.imageio.ImageIO;
public class ClickListener implements ActionListener {
private MyScreenCaptureTool myScreenCaptureTool;
public ClickListener(MyScreenCaptureTool myScreenCaptureTool) {
this.myScreenCaptureTool = myScreenCaptureTool;
}
@Override
public void actionPerformed(ActionEvent e) {
// Getting the unique file name using java.util.UUID
final String FILENAME = getUniqueFileName();
//File Extension
final String FILE_EXTENSION = ".png";
try {
myScreenCaptureTool.setVisible(false);
// Delaying the screen capturing for 1 sec. to avoid JFrame to be captured in the screen shot 
Thread.sleep(1000);
// Invoking the method
if(captureScreen(myScreenCaptureTool.getOutputPath()+FILENAME+FILE_EXTENSION)) {
myScreenCaptureTool.setVisible(true);
} else {
myScreenCaptureTool.setVisible(true);
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
// Method to get the random Unique file name
private String getUniqueFileName() {
UUID uuid = UUID.randomUUID();
return uuid.toString();
}
// Method to capture the screen
private boolean captureScreen(String fileName) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = new Rectangle(screenSize);
Robot robot;
try {
robot = new Robot();
BufferedImage image = robot.createScreenCapture(screenRectangle);
return ImageIO.write(image, "png", new File(fileName));
} catch (AWTException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
return false;
}
}

Output

Take Screen Shot Using Java Program

Tags:

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *