javafx快照没有显示应用程序或场景 [英] javafx snapshot without showing application or Scene

查看:160
本文介绍了javafx快照没有显示应用程序或场景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在使用JavaFx WebView创建HTML页面的屏幕截图并且它工作正常但我想知道是否可以在不在图形窗口中启动应用程序的情况下执行此操作!我的意思是没有任何更轻量级的方法来获取截图然后这个:

Hi i'm using JavaFx WebView to create Screenshot of HTML pages and it works fine but i wanted to know is it possible to do this without launching the application in Graphical Windows!! I mean aren't there any more lightweight method to get the screenshot then this:

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);
    }



}  


推荐答案

对于JavaFX 2.2及更低版本,没有类似的功能。

For JavaFX 2.2 and below, there's not functionality of the kind.

无头JavaFX应用程序目前是不可能的,主要是JavaFX线程是强制性的。

Headless JavaFX applications are currently not possible, and a main JavaFX thread is mandatory.

您可以做的最好的事情是通过几种解决方法来实现这一点。

The best you can do is read upon several workarounds for achieving this.

< b>相关的StackOverflow问题:

  • 用于服务器端图像生成的JavaFX
  • 使用Java FX在服务器端生成映像
  • JavaFx在无头模式下
  • 如何测试无头环境中的JavaFX 2?

    这篇关于javafx快照没有显示应用程序或场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆