Link to home
Start Free TrialLog in
Avatar of andy_baptiste
andy_baptiste

asked on

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

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;
    }

Open in new window

Avatar of Mick Barry
Mick Barry
Flag of Australia image

JPEG/CMYK are not supported afaik (so you cannot use ImageIO to read them)
Please attach an image that you're having problems with
Avatar of andy_baptiste
andy_baptiste

ASKER

Hi Objects/CEHJ,

Just to confirm....yes the image is a CMYK one created with Photoshop, so that does seem to be the issue.

I was told the code attached would identify these types of images and save them as RGB types, but no joy.

Any ideas how I can achieve converting these images in Java?
Can't really say without an image
There you go....
097335.jpg
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi yes as my post said I have already installed JAI, but it was still falling over when I tried to open a CMYK image saved in Photoshop.

I think I have identified the problem now...my IDE (eclipse) was not using the JDK that the extentions were installed in.

The problem I now have is that it will only work from the machine that I installed JAI on.

Does anyone know how I can distribute a Java application that uses Jai ( and the native DLLs )?
 
>>Hi yes as my post said I have already installed JAI,

That's not JAI in my link. It's JAI ImageIO. Tried it with your image - works fine
Sorry.

So is there a way I can deploy the now working application including the DLL's?

The generated jar file doesn't seem to include the DLL's.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Many Thanks.
In the end I deployed it as a folder with the generated jar file, an exe that runs the jar file, and bundled a JRE that had the DLLs in.
:-)