Mohammad Fajar
Mohammad Fajar

Reputation: 1007

Take a snapshot part of canvas in javaFX

I need to save some part of canvas to image that is from x1 > 0 and y1 > 0 to some x2 > x1 and y2 > y1. What I understand from javaFX API, snapshot must occupy a whole area of node, like

wim = new WritableImage(((int) width), ((int) height));
bufferedImage = new BufferedImage((int) width, (int) height, BufferedImage.TYPE_INT_ARGB);
parameter = new SnapshotParameters();
parameter.setTransform(new Translate(0, 200));

and then

node.snapshot(parameter, wim);
image = SwingFXUtils.fromFXImage(wim, bufferedImage);
Graphics2D gd = (Graphics2D) image.getGraphics();
gd.translate(0,200);
ImageIO.write(image, "png", file);

Upvotes: 2

Views: 8495

Answers (1)

Patrick
Patrick

Reputation: 4572

Hej Mohammad,

i made a small example for you with hard coded width and height for the WritableImage, maybe this will help you. I put a ChartBar on the Stage and take a snapshot of it, when the button is clicked.

package de.professional_webworkx.blog.takesnapshoot;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javax.imageio.ImageIO;

/**
 *
 * @author Patrick Ott <[email protected]>
 */
public class TakeSnapShoot extends Application {

    @Override
    public void start(Stage primaryStage) {
        AnchorPane root = new AnchorPane();
        Scene scene = new Scene(root, 1024, 768);
        ObservableList<String> observableArrayList = FXCollections.observableArrayList();
        observableArrayList.add("Kitchen");
        observableArrayList.add("Living Room");

        CategoryAxis xAxis = new CategoryAxis();
        NumberAxis yAxis = new NumberAxis();
        final BarChart barChart = new BarChart(xAxis, yAxis);

        xAxis.setLabel("Room");

        XYChart.Series series = new XYChart.Series();
        series.getData().add(new XYChart.Data<String, Number>("Kitchen", 1245));
        series.getData().add(new XYChart.Data<String, Number>("Living Room", 245));
        series.getData().add(new XYChart.Data<String, Number>("Child 1", 3445));

        barChart.getData().add(series);
        root.getChildren().add(barChart);

        Button snapShotBtn = new Button("Take a Snapshot");
        root.getChildren().add(snapShotBtn);
        snapShotBtn.setOnAction((ActionEvent t) -> {
            try {
                SnapshotParameters parameters = new SnapshotParameters();
                WritableImage wi = new WritableImage(100, 100);
                WritableImage snapshot = barChart.snapshot(new SnapshotParameters(), wi);

                File output = new File("snapshot" + new Date().getTime() + ".png");
                ImageIO.write(SwingFXUtils.fromFXImage(snapshot, null), "png", output);
            } catch (IOException ex) {
                Logger.getLogger(TakeSnapShoot.class.getName()).log(Level.SEVERE, null, ex);
            }
        });


        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }

}

Patrick

Upvotes: 1

Related Questions