views:

1036

answers:

2

Hi,

I'm having problems reading this one JPEG file using ImageIO.read(File file) - it throws an exception with the message "Unsupported Image Type".

I have tried other JPEG images, and they seem to work fine.

The only differance I've been able to spot is that this file seems to include a thumbnail - is that known to cause problems with ImageIO.read()?

Troublesome image

EDIT:

Added the resulting image:

Strange colors

+4  A: 

Your image "Color Model" is CMYK, JPEGImageReader (the inner class that reads your file) reads only RGB Color Model.

If you insist on reading CMYK images, then you will need to convert them, try this code.

UPDATE

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);
medopal
Excellent, I will give it a try. Will it work for RGB images as well, or will I need to detect the type somehow?
Malakim
You will find many ways to detect the color model, my preferred is using JPEGImageReader, if it throws `Unsupported Image Type` exception then its most probably CMYK.
medopal
This works well enough, however, the colors gets all messed up. See the new image I've attached to the question. Do you have any advice regarding this?Thanks!
Malakim
Yea, the color bleed is usual. Maybe you should try to use Javas `ColorConvertOp`, but not sure if it will help either. There is many ways of converting from one color model to another if thats your interest. I only gave the simplest one. If its a one time image, i would say just save it to RGB model in a professional image editing software.
medopal
OK, problem is I don't have control over what images the users will upload. So I guess I'll need to look deeper into the color model conversions to support any JPEG's the users might throw at the application.Thank you so much for the help.
Malakim
in that case you should make a function to check the image model before processing it. Check this http://forums.sun.com/thread.jspa?threadID=5391316 good luck :)
medopal
+1 Thanks for the solution. Now work on color model conversion.
torak
A: 

You might also want to check this link: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4799903

There is a comment that shows how to convert from CMYK to RGB.

raymi