1

I have set up a background Task that waits several seconds after a given Panel/Chart become visible. That is performed by running a sleep on a background non-GUI thread and then upon waking up it runs a

Platform.runLater

to create the snapshot and image.

Before the real 'action' for saving the image occurs we can see the window come up:

enter image description here

While that image is rendering we have the background code that has been put to sleep by a Task. After 5000 millis that background task wakes up and launches a Platform.runLater to save the scene/pane/chart to a file.

Here is the snapshot and image code:

All this happens on a background thread via a Task submitted to a ThreadPool

    Thread.sleep(5000)   // Wait for images to be rendered -
              // they are visually confirmed to be available at  about 1000 ms actually
    javafx.application.Platform.runLater(new Runnable() {
      override def run() = {
//            val snapShot = chart.snapshot(null)
//            val snapShot = scene.snapshot(null)
        val snapShot = pane.snapshot(null,null)
        ImageIO.write(SwingFXUtils.fromFXImage(snapShot, null),
          "jpg", new File(fileName))

As you can see (from the commented out lines) - I have confused about which object to use for creating the snapshot: all three above have been attempted:

  • Chart
  • Scene
  • Pane

Always the result is a Black Image. OOC I also tried changing the background color via

snapshotParameters.setFill(Color.WHITE)

That had no effect.

enter image description here

What is the correct procedure ?

Update I also tried a callback approach:

        pane.snapshot(  // Also tried scene and chart here ..
        new Callback[SnapshotResult, Void]() {
          override def call(result: SnapshotResult): Void = {
            ImageIO.write(SwingFXUtils.fromFXImage(result.getImage, null),
              "jpg", new File(fileName))
            latch.countDown
            null
          }
        },p,null)

Likewise - still a Black Image.

WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560

2 Answers2

2

Using such kinds of heuristics is rather risky. However, you didn't provide a full MCVE. If you want to do it that way, you could use a PauseTransition, snapshot any node and then save the image to a file.

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javafx.animation.PauseTransition;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.image.Image;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;

import javax.imageio.ImageIO;

public class AutoSnapshot extends Application {

    private static String fileName = "c:/temp/image.jpg";
    private static Color backgroundColor = Color.WHITE;

    @Override
    public void start(Stage primaryStage) {

        AnchorPane root = new AnchorPane();

        // dummy chart
        final NumberAxis xAxis = new NumberAxis();
        final NumberAxis yAxis = new NumberAxis();
        xAxis.setLabel("Number of Month");
        final LineChart<Number, Number> lineChart = new LineChart<Number, Number>(xAxis, yAxis);
        lineChart.setTitle("Stock Monitoring, 2010");
        XYChart.Series series = new XYChart.Series();
        series.setName("My portfolio");
        series.getData().add(new XYChart.Data(1, 23));
        series.getData().add(new XYChart.Data(2, 14));
        series.getData().add(new XYChart.Data(3, 15));
        series.getData().add(new XYChart.Data(4, 24));
        lineChart.getData().add(series);

        root.getChildren().add( lineChart);

        Scene scene = new Scene(root, 800, 600, backgroundColor);
        primaryStage.setScene(scene);
        primaryStage.show();

        Duration delay = Duration.seconds(5);
        PauseTransition pt = new PauseTransition(delay);
        pt.setOnFinished(e -> {
            saveSnapshot(lineChart);
        });
        pt.play();

    }

    public static Image createImage(Node node) {

        WritableImage wi;

        SnapshotParameters parameters = new SnapshotParameters();
        parameters.setFill(backgroundColor);

        int imageWidth = (int) node.getBoundsInLocal().getWidth();
        int imageHeight = (int) node.getBoundsInLocal().getHeight();

        wi = new WritableImage(imageWidth, imageHeight);
        node.snapshot(parameters, wi);

        return wi;

    }

    private static void saveSnapshot(Node node) {

        Image image = createImage(node);

        // save image !!! has bug because of transparency (use approach below) !!!
        // ImageIO.write(SwingFXUtils.fromFXImage( selectedImage.getImage(), null), "jpg", file);

        // save image (without alpha)
        BufferedImage bufImageARGB = SwingFXUtils.fromFXImage(image, null);
        BufferedImage bufImageRGB = new BufferedImage(bufImageARGB.getWidth(), bufImageARGB.getHeight(), BufferedImage.OPAQUE);

        Graphics2D graphics = bufImageRGB.createGraphics();
        graphics.drawImage(bufImageARGB, 0, 0, null);

        try {
            ImageIO.write(bufImageRGB, "jpg", new File(fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }

        graphics.dispose();

        System.out.println( "Image saved: " + fileName);

    }

    public static void main(String[] args) {
        launch(args);
    }
}
Roland
  • 18,114
  • 12
  • 62
  • 93
  • Your code has the following: "save image !!! has bug because of transparency (use approach below) ". The commented out code is what I presently use. I will start with replacing with your saveSnapshot. – WestCoastProjects Aug 10 '15 at 06:18
  • That code would only give you wrong colors, but not a black image. I rather think the problem is in your threading. Just try the pause transition. – Roland Aug 10 '15 at 06:21
  • after replacing the saveSnapshot/createImage I do see white background and *some* of the chart. The OPAQUE does not exist: should I replace with BufferedImage.TYPE_BYTE_BINARY? btw I am about to add the pause transition. – WestCoastProjects Aug 10 '15 at 06:32
  • Well, whatever works for you, although OPAQUE should exist. It's Java standard. The code I posted is what works for me. Btw, if you want transparency, you need to use Color.TRANSPARENT as snapshot fill parameter. – Roland Aug 10 '15 at 06:41
  • I am upvoting since helpful (though long ways yet to fixed). The java.awt.BufferedImage on JDK8 does not have OPAQUE. – WestCoastProjects Aug 10 '15 at 06:45
  • It does and it must have or else there'd be no backwards compatibility. – Roland Aug 10 '15 at 06:57
  • Roland you can see the issue yourself by trying to compile on jdk8: does not compile. But here is the correct modification: **Transparency**.OPAQUE – WestCoastProjects Aug 10 '15 at 15:24
  • I developed the code with JDK8 from Oracle. No idea why it doesn't compile for you. – Roland Aug 10 '15 at 15:41
  • That's true - i recall you have closures in there. Strange. I'm using IJ – WestCoastProjects Aug 10 '15 at 15:51
  • On a more important note: I need to print an entire Pane not just a single chart. Does that work for your code? – WestCoastProjects Aug 10 '15 at 15:54
  • Just put any Node into the createImage method, a Pane is also a Node. – Roland Aug 10 '15 at 16:47
  • The code isn't complicated and it does work. Nobody can possibly guess what your problem is [when you don't provide enough information](http://stackoverflow.com/help/mcve). – Roland Aug 18 '15 at 05:19
1

After all that .. the answer is that "jpg" is simply not working. "png" format works fine.

WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560