Javafx snapshot without showing application or scene

Hi, I use JavaFx WebView to create screenshots from HTML pages and it works fine, but I wanted to know if this can be done without running the application in graphic windows! I mean, there is no easier method for taking a screenshot, and then:

public class WebViewSample extends Application {
    private Scene scene;

    @Override
    public void start(Stage stage) {
        // create scene
        scene = new Scene(new Browser(snapshot), 750, 500, Color.web("#666970"));

        stage.setScene(scene);
//        show stage
        stage.show();
    }
    WritableImage snapshot;
    public static void main(String[] args) {
        launch(args);
        System.err.println("launched!");
    }
}
class Browser extends Region {


    final ImageView selectedImage = new ImageView();
    final WebView browser = new WebView();
    final WebEngine webEngine = browser.getEngine();
    private final  WritableImage snapshotImage;

    public Browser(WritableImage snapshot) {
        this.snapshotImage= snapshot;
        // process page loading
        webEngine.getLoadWorker().stateProperty().addListener(
                new ChangeListener<State>() {
                    @Override
                    public void changed(ObservableValue<? extends State> ov,
                                        State oldState, State newState) {
                        if (newState == State.SUCCEEDED) {
                            WritableImage newSnapshot = browser.snapshot(null, snapshotImage);
                            File file = new File("test2.png");
                            RenderedImage renderedImage = SwingFXUtils.fromFXImage(newSnapshot, null);
                            try {
                                ImageIO.write(renderedImage, "png", file);
                            } catch (IOException e1) {
                                e1.printStackTrace();
                            }
                            System.exit(0);
                        }
                    }
                }
        );

        // load the home page        
        webEngine.load("http://localhost/");


        //add components
        getChildren().add(browser);
    }



}  
+4
source share
1 answer

For JavaFX 2.2 and below, there is no functionality.

Headless JavaFX applications are currently unavailable, and the main JavaFX thread is required.

The best you can do is read a few workarounds to achieve this.

Related StackOverflow Questions:

JavaFX Java FX JavaFx JavaFX 2 ?
+4

Source: https://habr.com/ru/post/1534018/


All Articles