JavaFX 2.0 WebView / webengine renders web pages to images
I'm looking for a way to load the page and save the rendering as an image, just as you did with cutycapt (QT WebKit exe)
At present and without JavaFX, I call an external process to render from Java to a file instead of loading the file into an imagebuffer... Which is neither very optimized nor practical, or even less cross platform
Using javafx2, I tried 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,w,h,HPos.CENTER,VPos.CENTER); } @Override protected double computePrefWidth(final double height) { return 800; } @Override protected double computePrefHeight(final double width) { return 600; } }
There is an obsolete method: rendertoimage in scene (see the link below), which will do something close, and I can use it, but it has been deprecated... Its deprecation in JavaFX seems to mean that there is no Javadoc advertising replacement method, because I can't access the code, and I can't see how it is done
There are several websites here. I found some information, but nothing can present the web page to the 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
other:
http://download.oracle.com/javafx/2.0/webview/jfxpub-webview.htm http://download.oracle.com/javafx/2.0/get_started/jfxpub-get_started.htmMaps in JavaFX 2.0
Solution
I do this by launching JavaFX WebView on swing JFrame and jfxpanel Then, when the webengine status is succeeded, I use the paint () method on jfxpanel
You can follow this tutorial to create WebView: integrating JavaFX into swing applications
The following code demonstrates how to capture a 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();}