Difference between revisions of "Taking a Screenshot in Java"
PeterHarding (talk | contribs) (New page: Here is how to capture a screenshot and save it as a JPG file using Java. It is done using the ImageIO API for saving the screenshot as the JPG file and using the Robot class to perform t...) |
PeterHarding (talk | contribs) |
||
Line 3: | Line 3: | ||
First the screen dimension should be worked out and then create a rectangle with these dimensions. The screen dimansions can be found using the Toolkit class. The following code snippet show how... | First the screen dimension should be worked out and then create a rectangle with these dimensions. The screen dimansions can be found using the Toolkit class. The following code snippet show how... | ||
< | <pre> | ||
//to find the screen dimension | //to find the screen dimension | ||
Line 13: | Line 13: | ||
After getting the screen dimensions, create an instance of the Robot class and copy the rectangle from the screen. The createScreenCapture() method in the Robot class can be used to get the BufferedImage with the rectangular dimension passed as argument to it. The following code snippet demonstrates how this is done... | After getting the screen dimensions, create an instance of the Robot class and copy the rectangle from the screen. The createScreenCapture() method in the Robot class can be used to get the BufferedImage with the rectangular dimension passed as argument to it. The following code snippet demonstrates how this is done... | ||
< | <pre> | ||
// Get the BufferedImage with createScreenCapture method | // Get the BufferedImage with createScreenCapture method | ||
Line 23: | Line 23: | ||
Creating the JPG file from the BufferedImage is possible using the ImageIO API to convert the BufferedImage to the JPG file. The following code snippet show how... | Creating the JPG file from the BufferedImage is possible using the ImageIO API to convert the BufferedImage to the JPG file. The following code snippet show how... | ||
< | <pre> | ||
// Convert BufferedImage to JPG File | // Convert BufferedImage to JPG File | ||
Revision as of 11:33, 4 December 2008
Here is how to capture a screenshot and save it as a JPG file using Java. It is done using the ImageIO API for saving the screenshot as the JPG file and using the Robot class to perform the screenshot.
First the screen dimension should be worked out and then create a rectangle with these dimensions. The screen dimansions can be found using the Toolkit class. The following code snippet show how...
//to find the screen dimension Toolkit toolkit = Toolkit.getDefaultToolkit(); Dimension screenSize = toolkit.getScreenSize(); Rectangle rectangle = new Rectangle(0, 0, screenSize.width, screenSize.height);
After getting the screen dimensions, create an instance of the Robot class and copy the rectangle from the screen. The createScreenCapture() method in the Robot class can be used to get the BufferedImage with the rectangular dimension passed as argument to it. The following code snippet demonstrates how this is done...
// Get the BufferedImage with createScreenCapture method Robot robot = new Robot(); BufferedImage image = robot.createScreenCapture(rectangle);
Creating the JPG file from the BufferedImage is possible using the ImageIO API to convert the BufferedImage to the JPG file. The following code snippet show how...
// Convert BufferedImage to JPG File File file = new File(“screen.jpg”); ImageIO.write(image, “jpg”, file);