我在ImageView
的帮助下向显示的图像添加了效果和颜色调整。 .
现在我想将这些更改保存到另一个文件中。我该如何执行此操作?
最佳答案
您需要snapshot
与 fromFXImage
配对的 Node
类的功能SwingFXUtils
的方法。
Takes a snapshot of this node and returns the rendered image when it is ready. CSS and layout processing will be done for the node, and any of its children, prior to rendering it. The entire destination image is cleared to the fill Paint specified by the SnapshotParameters.
示例:
ImageView imageViewAdjusted = new ImageView(new Image(getClass().getResource("thinking-man.jpg").toExternalForm(), 250, 250, true, true));
ColorAdjust colorAdjust = new ColorAdjust();
colorAdjust.setContrast(0.9);
imageViewAdjusted.setEffect(colorAdjust);
imageViewAdjusted.setCache(true);
imageViewAdjusted.setCacheHint(CacheHint.SPEED);
Button btnSave = new Button("Save to File");
btnSave.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
File outputFile = new File("D:/formattedPicture.png");
BufferedImage bImage = SwingFXUtils.fromFXImage(imageViewAdjusted.snapshot(null, null), null);
try {
ImageIO.write(bImage, "png", outputFile);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
关于java - 如何在 JavaFX 中保存对 ImageView 所做的更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37608808/