Oniya Daniel
Oniya Daniel

Reputation: 399

javafx read javafx.scene.image.Image to ImageIO.write(), from CMYK to RGB

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

Upvotes: 1

Views: 1258

Answers (1)

mipa
mipa

Reputation: 10650

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.

Upvotes: 1

Related Questions