我可以通过编程方式捕获场景快照吗? [英] Can I capture snapshot of scene programmatically?

查看:48
本文介绍了我可以通过编程方式捕获场景快照吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码可以正常工作..

I have the following code which works fine..

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上的按钮时,它会另存为图像.

so when I run this application, sample.html will be rendered fine and when I hit the button on the jframe, it will be saved as an image just fine.

但是,我不想打扰按钮,这就是我正在尝试的方法.

However, I do not want to bother hitting the button, and this is what I am trying..

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还是无法正常工作?

But the saved image is just a blank white image. What am I missing? Basically, I am trying to save an html page as a png, am I taking a wrong approach? Should I do this with some other way? Actually, this will run on the server side so maybe JFrame will not work at all anyway?

推荐答案

向加载您的WebViewWebEngine持有的stateProperty添加合适的侦听器.当工作程序的状态为SUCCEEDED时,安排快照.从这个示例开始,字段webViewsnapshot()产生显示的结果.

Add a suitable listener to the stateProperty held by the WebEngine that loads your WebView. When the worker's state is SUCCEEDED, schedule the snapshot. Starting from this example, a snapshot() of the field webView produces the result shown.

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();
                }
            }
        });
    }
});

这篇关于我可以通过编程方式捕获场景快照吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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