troubleshooting Question

Error when loading image with Java: javax.imageio.IIOException: Unsupported Image Type

Avatar of andy_baptiste
andy_baptiste asked on
Java
12 Comments2 Solutions5478 ViewsLast Modified:
Hi,

I keep getting errors when loading certain images with Java:
javax.imageio.IIOException: Unsupported Image Type

I think it because the images are CMYK rather than RGB so I installed JAI and grabbed the attached code from the imternet...but STILL getting errors as below:

javax.imageio.IIOException: Unsupported Image Type
      at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(Unknown Source)
      at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(Unknown Source)
      at javax.imageio.ImageIO.read(Unknown Source)
      at javax.imageio.ImageIO.read(Unknown Source)

Any ideas what I might be doing wrong?


private static String cmyk2rgb(String filename) throws IOException
    {    
    	String rgbFilename = filename; 
    	try{
    	// Change this format into any ImageIO supported format.        
    	String format = "JPG";        
    	File imageFile = new File(filename);        
    	       
    	BufferedImage image = ImageIO.read(imageFile);        
    	if (image != null)        
    	{            
    		int colorSpaceType = image.getColorModel().getColorSpace().getType();            
    		if (colorSpaceType == ColorSpace.TYPE_CMYK)            
    		{                
    			BufferedImage rgbImage = new BufferedImage( image.getWidth(), image.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
    			ColorConvertOp op = new ColorConvertOp(null);                
    			op.filter(image, rgbImage);                
    			rgbFilename = imageFile.getName();                
    			rgbFilename = new File(imageFile.getParent(), format + "_" + rgbFilename).getPath();                
    			ImageIO.write(rgbImage, format, new File(rgbFilename));            
    		}        
    	} 
    	}catch(Exception e){e.printStackTrace();}
    	return rgbFilename;
    }
    
    
    private static boolean isCMYK(String filename)
    {
    	boolean result = false;
    	BufferedImage img = null;
    	try
    	{
    		img = ImageIO.read(new File(filename));
    	}catch (IOException e){return true;}
    	if (img != null)
    	{
    		int colorSpaceType = img.getColorModel().getColorSpace().getType();
    		result = colorSpaceType == ColorSpace.TYPE_CMYK;
    	}        
    	return result;
    }
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 2 Answers and 12 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 12 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros