ImageIO.read does not work for jpgs in cmyk

trying to find a method to read it been searching on google but found lot of different results. Maybe one of you know a easy method to do this.

Recommended Answers

All 9 Replies

i see the libraries. but still no clue where to start

all I want is to read a image to a BufferedImage of a jpeg with cmyk

if not possible than I would most likely go by converting a image with some class than make that class output the image into a bytearray stream.

looking at jaiimageio but not sure can i use it to convert a image.

well i found a code that uses jai but the returned byte array image color isnt right.

public static BufferedImage convertCMYKtoRGB(byte[] ba) throws Exception {

        int[] bitsCMYK = {8, 8, 8, 8};
        ICC_ColorSpace csCMYK = new ICC_ColorSpace(ICC_Profile.getInstance(SharedData.WRITELOCATION + "images\\pdf\\CMYK.pf"));

        ColorModel colorModelCMYK = new ComponentColorModel(csCMYK, bitsCMYK, false, false, java.awt.Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
        ImageLayout cmykLayout = new ImageLayout();
        cmykLayout.setColorModel(colorModelCMYK);
        RenderingHints rh = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, cmykLayout);

        ByteArraySeekableStream bais = new ByteArraySeekableStream(ba);
        ParameterBlock params = new ParameterBlock();
        params.add(bais);
        RenderedOp op = JAI.create("stream", params, rh);
        bais.close();

        ColorSpace csRGB = null;
        csRGB = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        ColorModel colorModel = new ComponentColorModel(csRGB, bitsCMYK, false, false, java.awt.Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
        ImageLayout il = new ImageLayout();
        il.setSampleModel(colorModel.createCompatibleSampleModel(op.getWidth(), op.getHeight()));
        RenderingHints hints = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, il);
        ParameterBlock pb = new ParameterBlock();

        pb.addSource(op).add(colorModel);
        il = new ImageLayout();
        il.setSampleModel(colorModel.createCompatibleSampleModel(op.getWidth(), op.getHeight()));
        hints = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, il);

        RenderedOp opNeu = JAI.create("ColorConvert", pb, hints);

        BufferedImage img = opNeu.getAsBufferedImage();
        return img;
    }

I also tried

PlanarImage inImage = JAI.create("fileload",SharedData.WRITELOCATION + (String) theorder.get("ordertype") + "Data\\" + String.valueOf((Integer) theorder.get("hiddenkey")) + "\\logos\\cappreview\\" + (String) theorder.getItem(item).get("previewfilename"));
                        BufferedImage realimage = inImage.getAsBufferedImage();

the color isnt right for this too.

well i can't figure out how to do this.

using jai i can read the jpg but cant get the colors rigth.

Did you read through some of the work-arounds mentioned in that bug report to translate the color space?

Did you read through some of the work-arounds mentioned in that bug report to translate the color space?

ya but the work round use other stuff i dont even know so i cant even use those functions

Just compare it wit javaio API

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.