2017-05-23 57 views
0

我有下面的代碼工作正常..我可以通過程序捕捉場景快照嗎?

import javafx.application.Platform; 
import javafx.embed.swing.JFXPanel; 
import javafx.embed.swing.SwingFXUtils; 
import javafx.scene.Scene; 
import javafx.scene.image.WritableImage; 
import javafx.scene.web.WebView; 

import javax.imageio.ImageIO; 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 

/* User: [email protected] Date: 2017/05/23 */ 
public class CaptureScene { 

    public static void main(String[] args) throws IOException { 
     JFXPanel jfxPanel = new JFXPanel(); 
     Platform.runLater(() -> { 
      WebView webView = new WebView(); 
      webView.getEngine().load("file:///Users/koraytugay/Desktop/sample.html"); 
      jfxPanel.setScene(new Scene(webView)); 

      final JFrame jFrame = new JFrame(); 
      jFrame.setLayout(new FlowLayout()); 
      jFrame.getContentPane().add(jfxPanel); 

      final JButton saveAsImage = new JButton("Save as Image"); 
      jFrame.getContentPane().add(saveAsImage); 
      saveAsImage.addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        Platform.runLater(new Runnable() { 
         @Override 
         public void run() { 
          final Scene scene = jfxPanel.getScene(); 
          WritableImage image = scene.snapshot(new WritableImage(1920, 1080)); 
          BufferedImage bufferedImage = SwingFXUtils.fromFXImage(image, null); 
          try { 
           ImageIO.write(bufferedImage, "png", new File("/Users/koraytugay/Desktop/vaaappp.png")); 
          } catch (IOException va) { 
           va.printStackTrace(); 
          } 
         } 
        }); 
       } 
      }); 

      jFrame.setSize(1920, 1080); 
      jFrame.setVisible(true); 
      jFrame.requestFocus(); 
     }); 
    } 
} 

所以當我運行該應用程序,sample.html將呈現精細,當我打在JFrame的按鈕,它會被保存爲圖像正好。

不過,我不想打擾擊中按鈕,這就是我想..

import javafx.application.Platform; 
import javafx.embed.swing.JFXPanel; 
import javafx.embed.swing.SwingFXUtils; 
import javafx.scene.Scene; 
import javafx.scene.image.WritableImage; 
import javafx.scene.web.WebView; 

import javax.imageio.ImageIO; 
import javax.swing.*; 
import java.awt.*; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 

/* User: [email protected] Date: 2017/05/23 */ 
public class CaptureScene { 

    public static void main(String[] args) throws IOException { 
     JFXPanel jfxPanel = new JFXPanel(); 
     Platform.runLater(() -> { 
      WebView webView = new WebView(); 
      webView.getEngine().load("file:///Users/koraytugay/Desktop/sample.html"); 
      jfxPanel.setScene(new Scene(webView)); 

      final JFrame jFrame = new JFrame(); 
      jFrame.setLayout(new FlowLayout()); 
      jFrame.getContentPane().add(jfxPanel); 


      jFrame.setSize(1920, 1080); 
      jFrame.setVisible(true); 
      jFrame.requestFocus(); 


      try { 
       Thread.sleep(2000); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 

      Platform.runLater(new Runnable() { 
       @Override 
       public void run() { 
        final Scene scene = jfxPanel.getScene(); 
        WritableImage image = scene.snapshot(new WritableImage(1920, 1080)); 
        BufferedImage bufferedImage = SwingFXUtils.fromFXImage(image, null); 
        try { 
         ImageIO.write(bufferedImage, "png", new File("/Users/koraytugay/Desktop/baanb.png")); 
        } catch (IOException va) { 
         va.printStackTrace(); 
        } 
       } 
      }); 

     }); 
    } 
} 

但保存的圖像僅僅是一個空白圖像。我錯過了什麼?基本上,我試圖將一個html頁面保存爲png,我是否採取了錯誤的方法?我應該用其他方式來做這件事嗎?其實,這將在服務器端運行,所以也許JFrame不會工作呢?

回答

2

WebEngine加載您的WebView添加合適的收聽者stateProperty。當員工的狀態爲SUCCEEDED時,安排快照。從example開始,字段webViewsnapshot()產生顯示的結果。

image

WebEngine webEngine = webView.getEngine(); 
Worker worker = webEngine.getLoadWorker(); 
worker.stateProperty().addListener((Observable o) -> { 
    if (worker.getState() == Worker.State.SUCCEEDED) { 
     Platform.runLater(new Runnable() { 
      @Override 
      public void run() { 
       WritableImage image = webView.snapshot(
        new SnapshotParameters(), new WritableImage(800, 400)); 
       BufferedImage bufferedImage = SwingFXUtils.fromFXImage(image, null); 
       try { 
        ImageIO.write(bufferedImage, "png", new File("image.png")); 
       } catch (IOException va) { 
        va.printStackTrace(); 
       } 
      } 
     }); 
    } 
}); 
相關問題