我有以下代码可以正常工作..
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: koray@tugay.biz 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: koray@tugay.biz 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可能根本无法工作?
答案 0 :(得分:2)
为加载stateProperty
的{{1}}所占据的WebEngine
添加合适的听众。当工作人员的状态为WebView
时,请安排快照。从此example开始,字段SUCCEEDED
的{{1}}会生成显示的结果。
snapshot()