Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
835 views
in Technique[技术] by (71.8m points)

java - Screenshot of the full web page loaded into JavaFX WebView component, not only visible part

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
        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();
        }

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...