我正在寻找一种加载页面的方法,并将渲染保存为图像,就像您使用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) 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; } }
有一个废弃的方法:在Scene中的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/javafx/2.0/webview/jfxpub-webview.htm http://download.oracle.com/javafx/2.0/get_started/jfxpub-get_started.htmMaps in JavaFX 2.0
解决方法
@H_404_30@ 我通过在Swing JFrame和JFXPanel上启动JavaFX WebView来完成此任务.然后,当WebEngine状态为SUCCEEDED时,我在JFXPanel上使用paint()方法.您可以按照本教程制作WebView:Integrating JavaFX into Swing Applications
下面的代码演示了如何从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();}