我需要将画布的某些部分保存到从x1 > 0
和y1 > 0
到某些x2 > x1
和y2 > y1
的图像。我从javaFX API中了解到,快照必须占据整个节点区域,如
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));
然后
node.snapshot(parameter, wim);
image = SwingFXUtils.fromFXImage(wim, bufferedImage);
Graphics2D gd = (Graphics2D) image.getGraphics();
gd.translate(0,200);
ImageIO.write(image, "png", file);
答案 0 :(得分:1)
Hej Mohammad,
我为你写了一个小例子,其中包含WritableImage的硬编码宽度和高度,也许这对你有帮助。当点击按钮时,我将ChartBar放在舞台上并拍摄它的快照。
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 <Patrick.Ott@professional-webworkx.de>
*/
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);
}
}
帕特里克