Skip to content

Instantly share code, notes, and snippets.

@kimble
Created August 24, 2012 10:50
Show Gist options
  • Save kimble/3449046 to your computer and use it in GitHub Desktop.
Save kimble/3449046 to your computer and use it in GitHub Desktop.
Java screenshot robot
import org.junit.Test;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import static java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment;
public class ScreenshotTest {
@Test
public void screenshottingRobotExperiment() throws AWTException, IOException {
GraphicsEnvironment ge = getLocalGraphicsEnvironment();
for (GraphicsDevice screen : ge.getScreenDevices()) {
GraphicsConfiguration configuration = screen.getDefaultConfiguration();
Rectangle bounds = configuration.getBounds();
Rectangle screenRectangle = new Rectangle((int) bounds.getWidth(), (int) bounds.getHeight());
Robot robot = new Robot(screen);
BufferedImage screenCapture = robot.createScreenCapture(screenRectangle);
ImageIO.write(screenCapture, "png", new File("c:/output/" + screen.getIDstring() + ".png"));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment