【发布时间】:2015-10-26 17:49:46
【问题描述】:
我正在 Windows 8.1 64 位和 4GB RAM 和 JDK 版本 8u45 64 位上开发 JavaFX 应用程序。
我想使用Robot 捕获屏幕的一部分,但问题是我无法获得我想要捕获的锚窗格的屏幕坐标,我不想使用snapshot,因为输出质量很差。这是我的代码。
我在这个链接中看到了这个问题 Getting the global coordinate of a Node in JavaFX 和这个 get real position of a node in javaFX 我尝试了所有答案,但没有任何效果,图像显示了屏幕的不同部分。
private void capturePane() {
try {
Bounds bounds = pane.getLayoutBounds();
Point2D coordinates = pane.localToScene(bounds.getMinX(), bounds.getMinY());
int X = (int) coordinates.getX();
int Y = (int) coordinates.getY();
int width = (int) pane.getWidth();
int height = (int) pane.getHeight();
Rectangle screenRect = new Rectangle(X, Y, width, height);
BufferedImage capture = new Robot().createScreenCapture(screenRect);
ImageIO.write(capture, "png", new File("image.png"));
} catch (IOException | AWTException ex) {
ex.printStackTrace();
}
}
【问题讨论】:
-
快照的“输出质量”差在哪方面?
-
有时图像不清晰。
-
你试过localToScreen()吗,因为本地到场景会返回场景内的坐标,当你放入机器人时会捕捉到不同的位置
-
@TomasBisciak 我刚刚尝试了 localToScreen() 并且图像进一步显示了屏幕的一角
标签: java javafx awt javafx-8 awtrobot