I added effect and color adjustments to an image which is displayed with help of ImageView
.
Now I want to save those changes to a different file. How can I perform this?
I added effect and color adjustments to an image which is displayed with help of ImageView
.
Now I want to save those changes to a different file. How can I perform this?
You need the snapshot
functionality of the class Node
paired with the fromFXImage
method of 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.
Example:
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);
}
}
});