在javaFX中拍摄画布的快照部分 [英] Take a snapshot part of canvas in javaFX
本文介绍了在javaFX中拍摄画布的快照部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将画布的某些部分保存到来自 x1>的图像中0
和 y1> 0
到某些 x2> x1
和 y2> Y1
。我从javaFX API中了解到,快照必须占据节点的整个区域,例如
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));
然后
node.snapshot(parameter, wim);
image = SwingFXUtils.fromFXImage(wim, bufferedImage);
Graphics2D gd = (Graphics2D) image.getGraphics();
gd.translate(0,200);
ImageIO.write(image, "png", file);
推荐答案
Hej Mohammad,
Hej Mohammad,
我为你写了一个小例子,其中包含了WritableImage的硬编码宽度和高度,也许这对你有帮助。当点击按钮时,我将ChartBar放在舞台上并拍摄它的快照。
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 <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);
}
}
Patrick
这篇关于在javaFX中拍摄画布的快照部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文