2

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?

DVarga
  • 21,311
  • 6
  • 55
  • 60
sharad_kalya
  • 255
  • 1
  • 3
  • 11
  • The linked question just answers how to save an `ImageView` into a file. This question is about to save `ImageView` with all of the changes made to the `ImageView` which question is not covered by the linked answer. – DVarga Jul 14 '16 at 12:01

1 Answers1

3

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);
        }

    }
});
DVarga
  • 21,311
  • 6
  • 55
  • 60
  • Can you please tell about the methods you used in this example : setCache() and set cacheHint(). – sharad_kalya Jun 07 '16 at 09:11
  • Both properties are coming from the class `Node`: [setCache](https://docs.oracle.com/javase/8/javafx/api/javafx/scene/Node.html#setCache-boolean-), [cacheHintProperty](https://docs.oracle.com/javase/8/javafx/api/javafx/scene/Node.html#cacheHintProperty--). Basically it indicates that this `Node` shall be cached as a bitmap, with the hint of that reason of the caching is to optimize animation speed on this `Node`. You can read the content of the links to inform yourself about the whole system. – DVarga Jun 07 '16 at 09:20
  • The solution provided by you is working but there still exists a problem. I am saving file in "png" format but the when i open the file, windows ask me to pick an approriate application to view that file. Please help. Thanks – sharad_kalya Jun 07 '16 at 09:37
  • actually i read the oracle documentation for the methods but it didnt hit my mind thats why I asked it to you. Well I am developing a desktop application for viewing image so should i use these methods? I want to show high quality images. – sharad_kalya Jun 07 '16 at 09:41
  • Problem #1: If you save the file with .png extension the operating system (in your case Windows) will use the software which is associated with ".png" extension to open the file. It is nothing about the source code, it is a Windows settings. Problem #2: If you have read the documentation of `cacheHintProperty()` you could see, that there is a contant `CacheHint.QUALITY`. This way the goal of the optimization will be to display the node in high quality (with lower speed). As you could see in the documentation, caching needs more memory. So it is up to you: experiment, see what's working well :) – DVarga Jun 07 '16 at 09:46
  • Actually the file which I am saving through my application, windows shows no extension to that file unless I save the file providing extension manually. Say if I save file with name "xyz.png" it's all right. But if I save it with name "xyz" then windows show this file with no extension and asks me choose an appn to open it. – sharad_kalya Jun 07 '16 at 10:20
  • Of course you shall append the extension. That's why I have defined the file as `File outputFile = new File("D:/formattedPicture.png");`. – DVarga Jun 07 '16 at 10:28