1

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"));
chresse
  • 5,486
  • 3
  • 30
  • 47
user3682590
  • 21
  • 1
  • 3
  • possible duplicate of [How to convert from CMYK to RGB in Java correctly?](http://stackoverflow.com/questions/3123574/how-to-convert-from-cmyk-to-rgb-in-java-correctly) – Braj Jun 12 '14 at 07:30
  • possible duplicate of [Problem reading JPEG image using ImageIO.read(File file)](http://stackoverflow.com/questions/2408613/problem-reading-jpeg-image-using-imageio-readfile-file) – Harald K Jun 12 '14 at 10:18

1 Answers1

2

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);
Community
  • 1
  • 1
Himanshu Tyagi
  • 5,201
  • 1
  • 23
  • 43
  • Except this will make the colors all wrong... CMYK != ABGR. You need to apply a color conversion. The easiest (if you really want to do it yourself) is to use `ColorConvertOp`. Best results will be if you use the CMYK ICC color profile in the JPEG when converting. Easier yet, is to just use an ImageIO plugin that handles CMYK profiles, as in one of the linked duplicates. :-) – Harald K Jun 12 '14 at 10:19