1

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

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

        });
    }
}

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?

Koray Tugay
  • 22,894
  • 45
  • 188
  • 319

1 Answers1

3

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.

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();
                }
            }
        });
    }
});
trashgod
  • 203,806
  • 29
  • 246
  • 1,045