我正在 Windows 8.1 64 位上开发 JavaFX 应用程序,具有 4GB RAM 和 JDK 版本 8u45 64 位。
我想使用捕获部分屏幕Robot
但问题是我无法获取我想要捕获并且不想使用的锚点窗格的屏幕坐标snapshot
因为输出质量很差。这是我的代码。
我在这个链接中看到了这个问题在JavaFX中获取节点的全局坐标 https://stackoverflow.com/questions/1531538/getting-the-global-coordinate-of-a-node-in-javafx和这个获取javaFX中节点的真实位置 https://stackoverflow.com/questions/31148690/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();
}
}
由于您是从本地(而不是布局)坐标开始,因此请使用getBoundsInLocal()
代替getLayoutBounds()
。由于您想要转换为屏幕(而不是场景)坐标,因此请使用localToScreen(...)
代替localToScene(...)
:
private void capturePane() {
try {
Bounds bounds = pane.getBoundsInLocal();
Bounds screenBounds = pane.localToScreen(bounds);
int x = (int) screenBounds.getMinX();
int y = (int) screenBounds.getMinY();
int width = (int) screenBounds.getWidth();
int height = (int) screenBounds.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();
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)