18

The above answer that someone has suggest, converts my colored image to a black and white one. So it's not appropriate for my question.

File file = new File("path");          
BufferedImage bufferedImage = ImageIO.read( file );

here is the code and below is the image. Download the image and save in your pc. And try to run the above code with a correct value of path, it will throw an exception in the topic

Download image: https://skydrive.live.com/?cid=19547371C4F3B839&id=19547371C4F3B839%21105

Simply if someone can obtain a java.awt.image.BufferedImage object from the image given that's enough (should not convert the image to gray scale one).

You are a genius if you can answer this :D. Plz help.

Shanaka Kuruwita
  • 689
  • 2
  • 6
  • 19
  • possible duplicate http://stackoverflow.com/questions/10416378/imageio-read-illegal-argument-exception-raster-bands-colour-space-components – Philipp Sander Aug 21 '13 at 09:10
  • Hi Philipp please read my question well. I have clearly mentioned "should not convert the image to gray scale one". The answer that you have suggest convert my colored image to a gray scale one(black and white), if you want please try it and see. And don't suggest other questions and don't give me - points with out trying it. Be responsible for your comments. – Shanaka Kuruwita Aug 21 '13 at 10:58
  • @PhilippSander While similar, the problem with this image is different from the one you linked. – Harald K Aug 27 '13 at 11:13

1 Answers1

30

There's nothing wrong with your code here. I could read your image using my JPEGImageReader plugin for ImageIO. This image reader tries to be lenient about JPEG errors, and is slightly more capable than the standard Java JPEGImageReader.

However, your JPEG file seems to have a number of problems so it can't be read 100%:

  • First, the ICC color profile in the image has 4 color components, while the image data has only 3 color components (this is causing the exception you see). The root cause is probably bad conversion software. Use ExifTool or similar software to have a look at the metadata for further investigation. My reader will simply give a warning and ignore the ICC profile in this case.
  • Second, the JPEG stream ends prematurely (missing EOI). You'll notice that there's some garbage pixels at the lower right of the image. There's nothing you can do about that, except getting the original image (well, actually, the image contains a thumbnail and the thumbnail is undamaged; you could try to recreate the data from that if you really need to). The image returned from my reader is consistent with images read by native software.
Harald K
  • 26,314
  • 7
  • 65
  • 111
  • 1
    Hi Harald thanks a lot for answering. Now this error is clear to me, thanks for explaining. I tried to use your plugin, but due to some missing dependency libraries I couldn't compile it (I added some twelvemonkeys-imageio jars). So would you mind explaining the exact way to use the plugin, and it would be great if you could show the simple code sample to get a buffered for my image. – Shanaka Kuruwita Aug 27 '13 at 05:14
  • 3
    Details on how to use the plugin in java apps, and servlet containers can be found here: https://github.com/haraldk/TwelveMonkeys – Sinisha Mihajlovski Jan 05 '15 at 14:54
  • Worked like a charm :) – DominikAngerer Apr 05 '15 at 16:34
  • 2
    @haraldK ok, I found out that package `libimage-exiftool-perl` contains exiftool. So, I have an image with a value of `3` for colour components and `CMYK` as colour space data. IIRC, CMYK has 4 colour components and that mismatch is what causes the exception. Is that right? – Carcamano Feb 17 '16 at 15:12
  • 3
    Maven dependency: `com.twelvemonkeys.imageioimageio-jpeg3.3.1` – Edd Mar 23 '18 at 15:32
  • Is there any sample howto use this. I searched for a sample to simply read an image with twelvemonkey api. – mcfly soft Oct 25 '19 at 11:17
  • @mcflysoft The point is, there is no "TwelveMonkeys API" for reading images. You simply use the `javax.imageio` API like you would otherwise do, but with the TwelveMonkeys ImageIO plugins installed at run time. See the [project page on GitHub](https://github.com/haraldk/TwelveMonkeys) for more details on that, and examples, or follow a generic ImageIO tutorial. – Harald K Oct 25 '19 at 12:57