Tutorial List
Home
Interview Questions
Interview
Interview Questions
Links
Web Home
About Us

Capture Screen Shot From Java


Here are the codes blow to capture screen shot from Java. Just pass string path to captureScreenShot() method, where you want to save the screen shot.
Enjoy!

*************************************************************************
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 javax.imageio.ImageIO;
public class ScreenShot {

/**
method to capture screen shot
@param String uploadPath to save screen shot as image
@returns boolean true if capture successful else false
*/
boolean captureScreenShot(String uploadPath)
{
boolean isSuccesful = false;
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage capture;
try {
capture = new Robot().createScreenCapture(screenRect);

// screen shot image will be save at given path with name "screen.jpeg"
ImageIO.write(capture, "jpg", new File( uploadPath, "screen.jpeg"));
isSuccesful = true;
} catch (AWTException awte) {
awte.printStackTrace();
isSuccesful = false;
}
catch (IOException ioe) {
ioe.printStackTrace();
isSuccesful = false;
}
return isSuccesful;
}
}


*****************************************************************************

I hope this program will help you a lot in your work/ learning. If you have any query contact me at mohit.amour@gmail.com

Thanks,
Mohit Singh

4 comments:

Anonymous said...

cool ..super code.can u make this to take image at an interval.

Anonymous said...

hi
this code helped me a lot. thanks in avance.

Arpan said...

Thanks but when I have locked screen locked i get blank screen.
Is there a way to capture the screen when screen is locked?

shouib said...

hi , i am using extended screen , one my laptop screen and other projector screen , is there any function in java to take screenshot of both at a single time and place it in one image file...