Question

I am resizing many jpeg images using Apache Sanselan which also deals with CMYK colors. I have a problem when trying to convert jpeg images that has an alpha channel... when doing it the result is an image with different colors, and i guess that java somehow handles these type of images as a different color format. As i said, the RGB resizing works fine as well as CMYK. ARGB images turn out with different colors.

An example: Before resizing

After resizing

Any suggestions? Can i force somehow ignore the alpha channel and handle the image as an RGB image? or convert it to be an RGB image without losing the real colors?

The code that handles this image is:

    ImageInputStream stream = ImageIO.createImageInputStream(file);
    Iterator<ImageReader> iter = ImageIO.getImageReaders(stream);
    while (iter.hasNext()) {
        ImageReader reader = iter.next();
        reader.setInput(stream);

        BufferedImage image = null;
        ICC_Profile profile = null;
        try {
            image = reader.read(0);

        } catch (IIOException e) {
            ... (CMYK conversion if needed)
        }

        return image;
    }
    return null;

Thanks in advance

Was it helpful?

Solution

I found a good solution here (first solution worked great):

problem using ImageIO.write jpg file

Edit: There is a new open source library which supports CMYK processing. All you need to do is to add the dependency to your project and a new reader will be added to the list of readers (while the known JPEGImageReader can't deal with CMYK). You will probably want to iterate over these readers and read the image using the first reader which doesn't throw exception. This package is a release candidate, but i am using it and it solved a huge problem that we had hard time dealing with.

http://mvnrepository.com/artifact/com.twelvemonkeys.imageio/imageio-jpeg/3.0-rc5

You can do the iteration this way to get the BufferedImage, and after you got that, the rest is easy (you can use any existing image converting package to save it as another format):

try (ImageInputStream input = ImageIO.createImageInputStream(source)) {

        // Find potential readers
        Iterator<ImageReader> readers = ImageIO.getImageReaders(input);

        // For each reader: try to read
        while (readers != null && readers.hasNext()) {
            ImageReader reader = readers.next();
            try {
                reader.setInput(input);
                BufferedImage image = reader.read(0);
                return image;
            } catch (IIOException e) {
                // Try next reader, ignore.
            } catch (Exception e) {
                // Unexpected exception. do not continue
                throw e;
            } finally {
                // Close reader resources
                reader.dispose();
            }
        }

        // Couldn't resize with any of the readers
        throw new IIOException("Unable to resize image");
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top