12

Ok I have an Image that I'm trying to read. Problem is that the Image.read(file) returns NULL.

File file = new File("C:\\images\\image1.jpg");
if(file.exists()){
    System.out.println("Image file exists.");
    BufferedImage originalImage = ImageIO.read(file);

}

So image exists but ImageIO.read(file) returns NULL. No thrown errors nothing!!! Whats going on?

This is what I have tried so far:

  1. Ok my environment is Windows 7. I tested with one of those images that comes with Windows and its able to read the image.
  2. The image1.jpg was created by another system. Now sure what method they are using.
  3. I tried converting the image into RGB as suggested here link text but it fails with "Not a JPEG file: starts with 0x4d 0x4d".
  4. The image extension is .jpg, but windows says its a JPEG type? This is confusing.

Can someone help with this? I'm new to this, not sure how to fix this.

Ok I just figured out that ImageIO.getImageReaders(stream) returns an empty Iterator. This means that it couldn't find a suitable reader? How am I supposed to read this image?

Community
  • 1
  • 1
Marquinio
  • 4,601
  • 13
  • 45
  • 68
  • 1
    Since your tags include 'applet', just thought I'd mention that unless you are writing an applet which is for loading images off the disks of the end-user, you will probably be after an URL instead. Further comments, Windows probably has a great deal of 'magic' built in for dealing with invalid images. It might pay to open the JPG in a trustworthy image editor and save it as a new file. That might correct the problem (assuming it is an invalid image). – Andrew Thompson Oct 29 '10 at 00:44
  • And what are those doubles slashes for? – user207421 Oct 29 '10 at 02:21

3 Answers3

6

Ok since I switched laptops, I looked at my old laptop and found this JAR jai-imageio.jar in the jre/ext/lib (I know bad idea). I moved it to my project/lib and it worked! I guess this jai-imageio.jar contains additional image readers.

Marquinio
  • 4,601
  • 13
  • 45
  • 68
5

From the Javadocs for ImageIO.read()

Returns a BufferedImage as the result of decoding a supplied File with an ImageReader chosen automatically from among those currently registered. The File is wrapped in an ImageInputStream. If no registered ImageReader claims to be able to read the resulting stream, null is returned.

Try creating an ImageInputStream, then pass that onto the ImageIO.read() method, instead of sending the file itself.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Jason
  • 11,263
  • 21
  • 87
  • 181
  • I think its environment issue. I'm trying to migrate all my work into a new laptop. I just tested the same code in my previous laptop and it works fine. I'm probably missing some jars in the EXT folder. I'll keep looking. Thanks for you response. – Marquinio Oct 29 '10 at 06:45
4

Awesome, I had the same problem, wherein it was supporting 24 image formats in eclipse but was supporting only 12 image formats in command prompt with maven. Once I placed jai_imageio.jar in maven's test path, maven has begun to support 24 image formats as well.

Sanjeev
  • 41
  • 1
  • 1
    You can also use ImageIO.getReaderFormatNames() to get all formats supported by ImageReaders in your environment. – Sanjeev Aug 04 '11 at 14:41