Click here to Skip to main content
14,267,320 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi,

i want to convert a cmyk type image into RGB Image.
if i use the below code the conversion happens but the image is not displaying properly.i can see block strike-through codes in my output image. any other way to convert CMYK to RGB.


java.util.Iterator<ImageReader> 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(fromFile);
               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_INT_RGB);

               //Fill the new image with the old raster
               bi.getRaster().setRect(raster);
           System.out.println("\n\n\n   bi.getColorModel().getColorSpace().getType()\n\n "+bi.getColorModel().getColorSpace().getType());



           //  BufferedImage bi = ImageIO.read(toFile);
               int colorSpaceType = bi.getColorModel().getColorSpace().getType();

                if( colorSpaceType == ColorSpace.TYPE_CMYK)
                {
                    System.out.println("\n\n\n is CMYK image \n\n\n");
                }
                else if(colorSpaceType == ColorSpace.TYPE_RGB)
                {
                System.out.println("\n\n\n is RGB image \n\n\n");
                ImageIO.write(bi, "JPEG", new File("d:\\hi.jpg"));

                }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100