下面列出了java.awt.Robot#createScreenCapture ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void main(String[] args) throws Exception {
Robot robot = new Robot();
robot.setAutoDelay(20);
SwingUtilities.invokeAndWait(JInternalFrameDraggingTest::createAndShowGUI);
robot.waitForIdle();
final int translate = FRAME_SIZE / 4;
moveFrame(robot, translate, translate / 2, translate / 2);
robot.waitForIdle();
Point p = getDesktopPaneLocation();
int size = translate / 2;
Rectangle rect = new Rectangle(p.x, p.y, size, size);
BufferedImage img = robot.createScreenCapture(rect);
int testRGB = BACKGROUND_COLOR.getRGB();
for (int i = 0; i < size; i++) {
int rgbCW = img.getRGB(i, size / 2);
int rgbCH = img.getRGB(size / 2, i);
if (rgbCW != testRGB || rgbCH != testRGB) {
throw new RuntimeException("Background color is wrong!");
}
}
}
public void run() {
try {
File screenshot_file = new File(mainFrame.Home + File.separator + "screenshot.s3");
Random rand = new Random(System.currentTimeMillis());
int random = rand.nextInt(256);
String random_name = "Screenshot-" + random;
long t1 = System.currentTimeMillis();
long diff = 0;
while (diff < 5000) {
long t2 = System.currentTimeMillis();
diff = t2 - t1;
}
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = new Rectangle(screenSize);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(screenRectangle);
ImageIO.write(image, "png", new File(screenshot_file.getAbsolutePath()));
mainFrame.jTextArea1.append("\nCreated Screen Shot.");
put = new Put(screenshot_file.getAbsolutePath(), mainFrame.cred.getAccess_key(), mainFrame.cred.getSecret_key(), mainFrame.cred.getBucket(), mainFrame.cred.getEndpoint(), random_name, false, false, false);
put.startc(screenshot_file.getAbsolutePath(), mainFrame.cred.getAccess_key(), mainFrame.cred.getSecret_key(), mainFrame.cred.getBucket(), mainFrame.cred.getEndpoint(), random_name, false, false, false);
} catch (Exception screenShot) {
}
}
public ImagePlus takeSnapshot() {
ViewerFrame win = viewer.getViewerFrame();
win.toFront();
IJ.wait(500);
Rectangle bounds = win.getBounds();
Rectangle r = bounds;
ImagePlus imp = null;
Image img = null;
try {
Robot robot = new Robot();
img = robot.createScreenCapture(r);
} catch(Exception e) { }
if (img != null) {
imp = new ImagePlus("", img);
}
return imp;
}
@Test(description = "Adding screenshot to the report")
public void testAddScreenshot0() throws IOException, AWTException {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = new Rectangle(screenSize);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(screenRectangle);
File imgFile = File.createTempFile("screenshot_file", "png");
ImageIO.write(image, "png", imgFile);
report.addImage(imgFile, "My screenshot file");
imgFile.delete();
}
private static void doCapture(Component test) {
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {}
// Grab the screen region
try {
Robot robot = new Robot();
Point pt1 = test.getLocationOnScreen();
Rectangle rect =
new Rectangle(pt1.x, pt1.y, test.getWidth(), test.getHeight());
capture = robot.createScreenCapture(rect);
} catch (Exception e) {
e.printStackTrace();
}
}
private static void showFrame() throws Exception {
final Robot robot = new Robot();
robot.setAutoDelay(50);
createUI();
images[step] = robot.createScreenCapture(bounds);
SwingUtilities.invokeAndWait(frame::dispose);
sleep();
}
private static void doCapture(Component test) {
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {}
// Grab the screen region
try {
Robot robot = new Robot();
Point pt1 = test.getLocationOnScreen();
Rectangle rect =
new Rectangle(pt1.x, pt1.y, test.getWidth(), test.getHeight());
capture = robot.createScreenCapture(rect);
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
@TestProperties(name = "Adding screenshot to the report")
public void testAddScreenshot() throws Exception {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = new Rectangle(screenSize);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(screenRectangle);
File imgFile = File.createTempFile("screenshot_file", "png");
ImageIO.write(image, "png", imgFile);
ReporterHelper.copyFileToReporterAndAddLink(report, imgFile, "My screenshot file");
imgFile.delete();
}
private static void doCapture(Component test) {
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {}
// Grab the screen region
try {
Robot robot = new Robot();
Point pt1 = test.getLocationOnScreen();
Rectangle rect =
new Rectangle(pt1.x, pt1.y, test.getWidth(), test.getHeight());
capture = robot.createScreenCapture(rect);
} catch (Exception e) {
e.printStackTrace();
}
}
private static void doCapture(Component test) {
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {}
// Grab the screen region
try {
Robot robot = new Robot();
Point pt1 = test.getLocationOnScreen();
Rectangle rect =
new Rectangle(pt1.x, pt1.y, test.getWidth(), test.getHeight());
capture = robot.createScreenCapture(rect);
} catch (Exception e) {
e.printStackTrace();
}
}
private static void showFrame() throws Exception {
final Robot robot = new Robot();
robot.setAutoDelay(50);
createUI();
images[step] = robot.createScreenCapture(bounds);
SwingUtilities.invokeAndWait(frame::dispose);
sleep();
}
/**
* 座標取得アクション
*/
private void detectAction() {
try {
GraphicsConfiguration gc = this.currentGraphicsConfiguration();
Robot robot = new Robot(gc.getDevice());
BufferedImage image = robot.createScreenCapture(gc.getBounds());
Rectangle relative = ScreenCapture.detectGameScreen(image);
Rectangle screenBounds = gc.getBounds();
this.setBounds(robot, relative, screenBounds);
} catch (Exception e) {
LoggerHolder.get().error("座標取得に失敗しました", e);
}
}
public static void TakeScreenshot(String filePath, String fileName) {
try {
Robot robot = new Robot();
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage screenFullImage = robot.createScreenCapture(screenRect);
ImageIO.write(screenFullImage, "jpg", new File(filePath + fileName + ".jpg"));
} catch (AWTException | IOException ex) {
System.out.println(ex.getMessage());
}
}
public void snapshot()
{
try
{
Robot robot = new Robot();
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
image = robot.createScreenCapture(new Rectangle(0, 0, d.width,
d.height));
}
catch (AWTException e)
{
}
}
@Test(description = "Adding screenshot to the report")
public void testAddScreenshot1() throws IOException, AWTException {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = new Rectangle(screenSize);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(screenRectangle);
File imgFile = File.createTempFile("screenshot_file", "png");
ImageIO.write(image, "png", imgFile);
report.addImage(imgFile, "My screenshot file");
imgFile.delete();
}
private static void showFrame() throws Exception {
final Robot robot = new Robot();
robot.setAutoDelay(50);
createUI();
images[step] = robot.createScreenCapture(bounds);
SwingUtilities.invokeAndWait(frame::dispose);
sleep();
}
public static void main(String[] args) throws AWTException
{
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gds = ge.getScreenDevices();
if (gds.length < 2) {
System.out.println("It's a multiscreen test... skipping!");
return;
}
for (int i = 0; i < gds.length; ++i) {
GraphicsDevice gd = gds[i];
GraphicsConfiguration gc = gd.getDefaultConfiguration();
Rectangle screen = gc.getBounds();
Robot robot = new Robot(gd);
// check Robot.mouseMove()
robot.mouseMove(screen.x + mouseOffset.x, screen.y + mouseOffset.y);
Point mouse = MouseInfo.getPointerInfo().getLocation();
Point point = screen.getLocation();
point.translate(mouseOffset.x, mouseOffset.y);
if (!point.equals(mouse)) {
throw new RuntimeException(getErrorText("Robot.mouseMove", i));
}
// check Robot.getPixelColor()
Frame frame = new Frame(gc);
frame.setUndecorated(true);
frame.setSize(100, 100);
frame.setLocation(screen.x + frameOffset.x, screen.y + frameOffset.y);
frame.setBackground(color);
frame.setVisible(true);
robot.waitForIdle();
Rectangle bounds = frame.getBounds();
if (!Util.testBoundsColor(bounds, color, 5, 1000, robot)) {
throw new RuntimeException(getErrorText("Robot.getPixelColor", i));
}
// check Robot.createScreenCapture()
BufferedImage image = robot.createScreenCapture(bounds);
int rgb = color.getRGB();
if (image.getRGB(0, 0) != rgb
|| image.getRGB(image.getWidth() - 1, 0) != rgb
|| image.getRGB(image.getWidth() - 1, image.getHeight() - 1) != rgb
|| image.getRGB(0, image.getHeight() - 1) != rgb) {
throw new RuntimeException(
getErrorText("Robot.createScreenCapture", i));
}
frame.dispose();
}
System.out.println("Test PASSED!");
}
public static void main(String[] args) throws AWTException
{
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gds = ge.getScreenDevices();
if (gds.length < 2) {
System.out.println("It's a multiscreen test... skipping!");
return;
}
for (int i = 0; i < gds.length; ++i) {
GraphicsDevice gd = gds[i];
GraphicsConfiguration gc = gd.getDefaultConfiguration();
Rectangle screen = gc.getBounds();
Robot robot = new Robot(gd);
// check Robot.mouseMove()
robot.mouseMove(screen.x + mouseOffset.x, screen.y + mouseOffset.y);
Point mouse = MouseInfo.getPointerInfo().getLocation();
Point point = screen.getLocation();
point.translate(mouseOffset.x, mouseOffset.y);
if (!point.equals(mouse)) {
throw new RuntimeException(getErrorText("Robot.mouseMove", i));
}
// check Robot.getPixelColor()
Frame frame = new Frame(gc);
frame.setUndecorated(true);
frame.setSize(100, 100);
frame.setLocation(screen.x + frameOffset.x, screen.y + frameOffset.y);
frame.setBackground(color);
frame.setVisible(true);
robot.waitForIdle();
Rectangle bounds = frame.getBounds();
if (!Util.testBoundsColor(bounds, color, 5, 1000, robot)) {
throw new RuntimeException(getErrorText("Robot.getPixelColor", i));
}
// check Robot.createScreenCapture()
BufferedImage image = robot.createScreenCapture(bounds);
int rgb = color.getRGB();
if (image.getRGB(0, 0) != rgb
|| image.getRGB(image.getWidth() - 1, 0) != rgb
|| image.getRGB(image.getWidth() - 1, image.getHeight() - 1) != rgb
|| image.getRGB(0, image.getHeight() - 1) != rgb) {
throw new RuntimeException(
getErrorText("Robot.createScreenCapture", i));
}
frame.dispose();
}
System.out.println("Test PASSED!");
}
public static void main(String[] args) throws AWTException
{
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gds = ge.getScreenDevices();
if (gds.length < 2) {
System.out.println("It's a multiscreen test... skipping!");
return;
}
for (int i = 0; i < gds.length; ++i) {
GraphicsDevice gd = gds[i];
GraphicsConfiguration gc = gd.getDefaultConfiguration();
Rectangle screen = gc.getBounds();
Robot robot = new Robot(gd);
// check Robot.mouseMove()
robot.mouseMove(screen.x + mouseOffset.x, screen.y + mouseOffset.y);
Point mouse = MouseInfo.getPointerInfo().getLocation();
Point point = screen.getLocation();
point.translate(mouseOffset.x, mouseOffset.y);
if (!point.equals(mouse)) {
throw new RuntimeException(getErrorText("Robot.mouseMove", i));
}
// check Robot.getPixelColor()
Frame frame = new Frame(gc);
frame.setUndecorated(true);
frame.setSize(100, 100);
frame.setLocation(screen.x + frameOffset.x, screen.y + frameOffset.y);
frame.setBackground(color);
frame.setVisible(true);
robot.waitForIdle();
Rectangle bounds = frame.getBounds();
if (!Util.testBoundsColor(bounds, color, 5, 1000, robot)) {
throw new RuntimeException(getErrorText("Robot.getPixelColor", i));
}
// check Robot.createScreenCapture()
BufferedImage image = robot.createScreenCapture(bounds);
int rgb = color.getRGB();
if (image.getRGB(0, 0) != rgb
|| image.getRGB(image.getWidth() - 1, 0) != rgb
|| image.getRGB(image.getWidth() - 1, image.getHeight() - 1) != rgb
|| image.getRGB(0, image.getHeight() - 1) != rgb) {
throw new RuntimeException(
getErrorText("Robot.createScreenCapture", i));
}
frame.dispose();
}
System.out.println("Test PASSED!");
}
/**
* <p>
* スクリーンショットをrectangleで指定した領域で取得します<br>
* 失敗した場合nullを返します
* </p>
*
* @param rectangle
* @return スクリーンショット
*/
public static BufferedImage getCapture(Rectangle rectangle) {
try {
Robot robot = new Robot();
return robot.createScreenCapture(rectangle);
} catch (Exception e) {
return null;
}
}