加载到JavaFX WebView组件中的完整网页的屏幕截图,不仅是可见部分 [英] Screenshot of the full web page loaded into JavaFX WebView component, not only visible part
问题描述
经过两年的管理工作后,我正在编写我的第一行代码。没有时间阅读很多文档,需要在几分钟内创建一个概念验证。
所以我必须使用JavaFX并且需要提供允许将Web页面加载到WebView组件的屏幕截图的功能。问题是我需要整页的屏幕截图,而不仅仅是适合当前应用程序窗口大小的那个。这是我使用的一个简单代码:
I'm writing my first lines of code after 2 years of managerial work. No time to read a lot of docs, need to create a proof-of-concept just in minutes. So I have to work with JavaFX and need to provide functionality that allows to take a screenshot of web-page loaded into WebView component. The issue is that I need a screenshot of the full page, not only that piece that fits into current size of application window. Here is a simple code I use:
WritableImage image = browser.snapshot(new SnapshotParameters(), null);
// browser is javafx.scene.web.WebView
File file = new File("screenshot_fx.png");
try {
ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", file);
} catch (IOException e) {
e.printStackTrace();
}
它基本上只捕获我在屏幕上看到的内容。如果网页需要滚动 - 我将不会在屏幕截图上显示不可见的部分。请建议如何进行。
And it basically captures only what I see on the screen. If web-page requires scrolling -- I will not have not-visible part on the screenshot. Please suggest how to proceed.
推荐答案
try {
Robot pixelGrabber = new Robot();
BufferedImage bi = pixelGrabber
.createScreenCapture(new Rectangle(x, y, width, height));
Image screen = SwingFXUtils.toFXImage(bi,
new WritableImage(bi.getWidth(), bi.getHeight()));
} catch (AWTException ex) {
ex.printStackTrace();
}
这会创建一个以像素x * y开头的屏幕截图,其中包含您需要的高度和宽度独立于应用程序窗口的当前大小。如果您使用的是JavaFX,只需使用SwingFXUtils将awt-image转换为JavaFX图像。
This creates a screenshot starting on pixel x*y with your needed height and width independent of the current size of your application window. If your are using JavaFX, just use SwingFXUtils to transform the awt-image to a JavaFX-image.
这篇关于加载到JavaFX WebView组件中的完整网页的屏幕截图,不仅是可见部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!