Source
Using the Robot class you can capture portions of your screen. Supports the four main image formats (PNG, JPG, GIF, BMP) maybe more.
Code:
/**
* Class By: Vanquish from HackForums.Net
* Date: March 19th/2011
*/
//Imports
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.*;
public class ScreenCapture
{
//Fields
private Robot provider;
private Toolkit tools;
private boolean deniedAccess;
//Constructor
public ScreenCapture()
{ //initialize the fields
try{
provider = new Robot();
tools = Toolkit.getDefaultToolkit();
} catch (AWTException ex) { deniedAccess = true; } //Permission denied for screen shots.
}
//Capture image here
private BufferedImage captureScreen(Rectangle bounds)
{
return provider.createScreenCapture(bounds);
}
//Writes the image to the HDD.
private boolean writeImage(BufferedImage image, String filePath){
try{
String imgFormat = filePath.substring(filePath.lastIndexOf(".") + 1).toLowerCase();
ImageIO.write(image, imgFormat, new File(filePath));
return true;
} catch (IOException ex) { return false; }
}
/* This is for another project of mine.
public BufferedImage rawImage(int delay)
{
try{
Thread.sleep(delay);
return captureScreen(
new Rectangle(tools.getScreenSize()));
} catch (InterruptedException ex) { return null; }
}
*/
/**
* Captures the entire screen.
* @param filePath The path to save the image to.
* @return Returns true if the image was captured and saved, otherwize false.
*/
public boolean takeShot(String filePath)
{
if (deniedAccess) //permission check
return false;
return writeImage(captureScreen(
new Rectangle(tools.getScreenSize())), filePath);
}
/**
* Captures an area of the screen from the orign of the monitor (upper left) to the specified width and height.
* @param width The horizontal length of the image.
* @param height The vertical length of the image.
* @param filePath The path to save the image to.
* @return Returns true if the image was captured and saved, otherwize false.
*/
public boolean takeShot(int width, int height, String filePath)
{
if (deniedAccess)
return false;
return writeImage(captureScreen(
new Rectangle(width, height)), filePath);
}
/**
* Captures an area of the screen within the specified bounds.
* @param bounds The rectangle portion to the screen to capture.
* @param filePath The path to save the image to.
* @return Returns true if the image was captured and saved, otherwize false.
*/
public boolean takeShot(Rectangle bounds, String filePath)
{
if (deniedAccess)
return false;
return writeImage(captureScreen(bounds), filePath);
}
/**
* Captures an area of the screen from the specified x and y coordinates to the width and height.
* @param x The x-coordinate of the bounds.
* @param y The y-coordinate of the bounds.
* @param width The horizontal length of the image.
* @param height The vertical length of the image.
* @param filePath The path to save the image to.
* @return Returns true if the image was captured and saved, otherwize false.
*/
public boolean takeShot(int x, int y, int width, int height, String filePath)
{
if (deniedAccess)
return false;
return writeImage(captureScreen(
new Rectangle(x, y, width, height)), filePath);
}
}
Geen opmerkingen:
Een reactie posten