JavaFX 2.0+ WebView / WebEngine将网页呈现给图像 [英] JavaFX 2.0+ WebView /WebEngine render web page to an image

查看:192
本文介绍了JavaFX 2.0+ WebView / WebEngine将网页呈现给图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种加载页面并将渲染保存为图像的方法,就像使用CutyCapt一样(QT + webkit EXE就是这样)。



目前没有JavaFX,我是通过从java调用外部进程并渲染到文件而不是将该文件加载到ImageBuffer中来实现的...既不优化也不优化实用甚至更少的跨平台...



使用JavaFX2 +我尝试使用WebView& WebEngine:

  public class WebComponentTrial extends Application {
private Scene scene;

@Override
public void start(final stage primaryStage)抛出异常{
primaryStage.setTitle(Web View);
final Browser browser = new Browser();
scene = new Scene(浏览器,1180,800,Color.web(#666970));
primaryStage.setScene(scene);
scene.getStylesheets()。add(webviewsample / BrowserToolbar.css);
primaryStage.show();
}

public static void main(final String [] args){
launch(args);
}
}
类浏览器扩展Region {
static {//在独立应用程序时使用系统代理设置
// System.setProperty(java.net.useSystemProxies ,true);
}

最终的WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();

public Browser(){
getStyleClass()。add(browser);
webEngine.load(http://www.google.com/);
getChildren()。add(browser);
}

@Override
protected void layoutChildren(){
final double w = getWidth();
final double h = getHeight();
layoutInArea(浏览器,0,0,w,h,0,HPos.CENTER,VPos.CENTER);
}

@Override
protected double computePrefWidth(最终双倍高度){
返回800;
}

@Override
protected double computePrefHeight(最终双倍宽度){
返回600;
}
}

有一种弃用的方法: renderToImage 场景中(见下面的链接),它会做一些接近我能够工作的东西,但它是不推荐使用...
它在JavaFX中被弃用似乎意味着没有javadoc广告替换方法,因为我无法访问代码,我看不出它是如何完成的......



以下是我发现一些信息的网站,但没有任何内容可以将网页呈现给图像:

  http://tornorbye.blogspot.com/2010/02/how-to-render-javafx-node-into-image.html 

canvasImage saveImage(canvasImage,fc.getSelectedFile())来自这一个:

  http://javafx.com/samples/EffectsPlayground/src/Main.fx.html 

其他:

  http://download.oracle.com/jav afx / 2.0 / webview / jfxpub-webview.htm 
http://download.oracle.com/javafx/2.0/get_started/jfxpub-get_started.htm
http://fxexperience.com/2011/ 05 / maps-in-javafx-2-0 /


解决方案

我通过在Swing JFrame和JFXPanel上启动JavaFX WebView来完成此操作。然后,一旦WebEngine状态为SUCCEEDED,我就在JFXPanel上使用paint()方法。



您可以按照本教程制作WebView:

下面的代码演示了如何从JFXPanel捕获渲染的屏幕。

  public static void main(String args [ ]){
jFrame = new JFrame(演示浏览器);
jfxPanel = new JFXPanel();
jFrame.add(jfxPanel);
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
Platform.runLater(new Runnable(){
@Override
public void run(){
browser = new FXBrowser();
jfxPanel.setScene(browser.getScene());
jFrame.setSize((int)browser .getWebView()。getWidth(),(int)browser.getWebView()。getHeight());

browser.getWebEngine()。getLoadWorker()。stateProperty()。addListener(
new ChangeListener(){
@Override
public void changed(ObservableValue observable,
Object oldValue,Object newValue){
State oldState =(State)oldValue;
状态newState =(State)new值;
if(State.SUCCEEDED == newValue){
captureView();
}
}
});
}
});
}
});
}

private static void captureView(){
BufferedImage bi = new BufferedImage(jfxPanel.getWidth(),jfxPanel.getHeight(),BufferedImage.TYPE_INT_ARGB);
图形图形= bi.createGraphics();
jfxPanel.paint(graphics);
try {
ImageIO.write(bi,PNG,new File(demo.png));
} catch(IOException e){
e.printStackTrace();
}
graphics.dispose();
bi.flush();
}


I'm looking for a way to load up a page and save the rendering as an image just as you would do with CutyCapt (QT + webkit EXE to do just that).

At the moment and without JavaFX, I do it by calling an external process from java and rendering to file than loading that file into an ImageBuffer... Neither very optimized nor practical and even less cross platform...

Using JavaFX2+ I tried playing with the WebView & WebEngine:

public class WebComponentTrial extends Application {
    private Scene scene;

    @Override
    public void start(final Stage primaryStage) throws Exception {
        primaryStage.setTitle("Web View");
        final Browser browser = new Browser();
        scene = new Scene(browser, 1180, 800, Color.web("#666970"));
        primaryStage.setScene(scene);
        scene.getStylesheets().add("webviewsample/BrowserToolbar.css");
        primaryStage.show();
    }

    public static void main(final String[] args) {
        launch(args);
    }
}
class Browser extends Region {
    static { // use system proxy settings when standalone application
    // System.setProperty("java.net.useSystemProxies", "true");
    }

    final WebView browser = new WebView();
    final WebEngine webEngine = browser.getEngine();

    public Browser() {
        getStyleClass().add("browser");
        webEngine.load("http://www.google.com/");
        getChildren().add(browser);
    }

    @Override
    protected void layoutChildren() {
        final double w = getWidth();
        final double h = getHeight();
        layoutInArea(browser, 0, 0, w, h, 0, HPos.CENTER, VPos.CENTER);
    }

    @Override
    protected double computePrefWidth(final double height) {
        return 800;
    }

    @Override
    protected double computePrefHeight(final double width) {
        return 600;
    }
}

There is a deprecated method : renderToImage in Scene (see links below) that would do something that comes close and with which I'd might be able to work but it is deprecated... It being deprecated in JavaFX seems to mean that there is no javadoc advertising the replacement method and because I don't have access to the code, I cannot see how it was done...

Here are a couple of sites where I found some information but nothing to render a webpage to an image:

http://tornorbye.blogspot.com/2010/02/how-to-render-javafx-node-into-image.html

canvasImage and saveImage(canvasImage, fc.getSelectedFile()) from this one :

http://javafx.com/samples/EffectsPlayground/src/Main.fx.html

Others:

http://download.oracle.com/javafx/2.0/webview/jfxpub-webview.htm
http://download.oracle.com/javafx/2.0/get_started/jfxpub-get_started.htm
http://fxexperience.com/2011/05/maps-in-javafx-2-0/

解决方案

I have done this by launching JavaFX WebView on a Swing JFrame and JFXPanel. And then I use the paint() method on JFXPanel once the WebEngine status is SUCCEEDED.

You may follow this tutorial to make a WebView: Integrating JavaFX into Swing Applications

The code below demonstrate how I capture the rendered screen from JFXPanel.

public static void main(String args[]) {
    jFrame = new JFrame("Demo Browser");
    jfxPanel = new JFXPanel();
    jFrame.add(jfxPanel);
    jFrame.setVisible(true);
    jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            Platform.runLater(new Runnable() {
                @Override
                public void run() {
                    browser = new FXBrowser();
                    jfxPanel.setScene(browser.getScene());
                    jFrame.setSize((int) browser.getWebView().getWidth(), (int) browser.getWebView().getHeight());

                    browser.getWebEngine().getLoadWorker().stateProperty().addListener(
                            new ChangeListener() {
                                @Override
                                public void changed(ObservableValue observable,
                                                    Object oldValue, Object newValue) {
                                    State oldState = (State) oldValue;
                                    State newState = (State) newValue;
                                    if (State.SUCCEEDED == newValue) {
                                        captureView();
                                    }
                                }
                            });
                }
            });
        }
    });
}

private static void captureView() {
    BufferedImage bi = new BufferedImage(jfxPanel.getWidth(), jfxPanel.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics graphics = bi.createGraphics();
    jfxPanel.paint(graphics);
    try {
        ImageIO.write(bi, "PNG", new File("demo.png"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    graphics.dispose();
    bi.flush();
}

这篇关于JavaFX 2.0+ WebView / WebEngine将网页呈现给图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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