user3682590
user3682590

Reputation: 21

Java ImageIO IIOException: Unsupported image type?ImageIO can't read CMYK-encoded Images

How can I decode the Image if it is not RGB color.It Should Decode the Image by Supporting all formats (Jpg,Png,Gif..etc) Any api is there to decode.

Here's the line of code that is failing.So which approach can use to reslove the issue.

BufferedImage imgSelected = ImageIO.read(new File("/abs/url/to/file/image.jpg"));

Upvotes: 1

Views: 8771

Answers (1)

hityagi
hityagi

Reputation: 5256

You might get your answer here : https://stackoverflow.com/a/2408779/3603806

Which says :

Read a CMYK image into RGB BufferedImage.

File f = new File("/path/imagefile.jpg");

//Find a suitable ImageReader
Iterator readers = ImageIO.getImageReadersByFormatName("JPEG");
ImageReader reader = null;
while(readers.hasNext()) {
    reader = (ImageReader)readers.next();
    if(reader.canReadRaster()) {
        break;
    }
}

//Stream the image file (the original CMYK image)
ImageInputStream input =   ImageIO.createImageInputStream(f); 
reader.setInput(input); 

//Read the image raster
Raster raster = reader.readRaster(0, null); 

//Create a new RGB image
BufferedImage bi = new BufferedImage(raster.getWidth(), raster.getHeight(), 
BufferedImage.TYPE_4BYTE_ABGR); 

//Fill the new image with the old raster
bi.getRaster().setRect(raster);

Upvotes: 2

Related Questions