Click here to Skip to main content
15,501,490 members
Please Sign up or sign in to vote.
1.00/5 (1 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
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900