Getting Black Image when saving a JavaFX snapshot

淺唱寂寞╮ 提交于 2019-12-04 19:40:57

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!