1

What I want to do is read an image from FileChooser and write it to file. I had to store the image in a javafx.scene.image.Image so that I can display it and clip it inside a circle. I have a little problem with trying to write the image that I got from javafx.scene.image.Image to file. The conversion process is not fluid, converts from CMYK to RGB (therefore turning my picture to some pink thing.

Please, I have checked a lot of other sources, and no one has been able to give me a notable solution

FileChooser fileChooser = new FileChooser();
File selectedFile = fileChooser.showOpenDialog(parent);

// get Image from selectedFile
Image userImg =  = new Image( selectedFile.toURI().toURL().toString() );

if ( userImg != null ) {

        String format = "jpg";
        String filename = "d:\\pictureName."+ format;
        File file = new File(filename);

        // convert Image to BufferedImage
        BufferedImage bufferedImage = SwingFXUtils.fromFXImage( userImg, null);

        try {

            // this is where i want to convert the color mode
            // from cmyk to rgb, before i write it to file
            ImageIO.write( bufferedImage, format, file );

        } catch (IOException e) {

            System.out.println("Exception :: "+ e.getMessage() );
        }
    }
Oniya Daniel
  • 359
  • 6
  • 15
  • A good CMYK to RGB conversion in Java is a challenge. See this [answer](http://stackoverflow.com/a/12132630/413337) for a complete solution. – Codo Sep 01 '16 at 13:14

1 Answers1

1

Why do you think that there is some CMYK to RGB conversion happening? I suspect the reason for your "pink thing" is something different. The easiest way to find out is to change your output format from jpeg to png and see whether it makes a difference.

I think you are again one of the many people who are hit by this bug https://bugs.openjdk.java.net/browse/JDK-8119048 which is not considered important enough to be fixed. If you read the comments in there you will find a work-arround. Basically the idea is to copy the image after the conversion into a new image without alpha channel. I'd really like to know how many more people have to waste their time until this bug finally gets enough attention to be fixed.

mipa
  • 10,369
  • 2
  • 16
  • 35
  • Thank you very much for your answer. I uploaded the file as png, and that solved the problem, I only have doubts. I don't know if uploading jpg to png using `ImageIO.write()` may have any pitfalls of its own . . . – Oniya Daniel Sep 02 '16 at 12:37
  • Using PNG was just meant as a quick test to see whether my assumption was right, which seems to be the case. If you have to, you can stick with JPG but then you have to follow the workarround which is given in one of the comments in the above linked bug report. – mipa Sep 02 '16 at 16:37
  • Thank you very much @mipa, you are the best. All is resolved. I hope your answer gets more votes. – Oniya Daniel Sep 03 '16 at 10:02