Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ImageIO.read(InputStream ..) returns null  RSS feed

 
Simon Freienstein
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am using the commons-fileupload lib to receive an image from the user. I want to resize the image on the servside and thus use the ImageIO component.

That's where the problem comes in. If I directly hand over the InputStream from commons-fileupload to the ImageIO.read, it returns null (the InputStream is not null though).

If I first write the image to disk and then load it via ImageIO.read(new File("xyz")) everything works fine.

Here is the code:



Always he second assert is thrown.

Do you have any hint, where the problem might be?
[ May 07, 2007: Message edited by: Bernd Stromberg ]
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

On your way in you may have missed that we have a policy on screen names here at JavaRanch. Basically, it must consist of a first name, a space, and a last name, and not be obviously fictitious. Since yours does not conform with it, please take a moment to change it, which you can do right here.

As to your question, the javadocs for ImageIO.read(InputStream) state that "If no registered ImageReader claims to be able to read the resulting stream, null is returned." It also states "This methods does not attempt to locate ImageReaders that can read directly from an InputStream; that may be accomplished using IIORegistry and ImageReaderSpi." That's slightly different from what ImageIO.read(File) does. What kind of image is this?
 
Simon Freienstein
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:

On your way in you may have missed that we have a policy on screen names here at JavaRanch.

Sorry for that.

Originally posted by Ulf Dittmer:

As to your question, the javadocs for ImageIO.read(InputStream) state that "If no registered ImageReader claims to be able to read the resulting stream, null is returned." It also states "This methods does not attempt to locate ImageReaders that can read directly from an InputStream; that may be accomplished using IIORegistry and ImageReaderSpi." That's slightly different from what ImageIO.read(File) does. What kind of image is this?


It doesn't matter wether the picture is a JPEG or a PNG (other not tested yet).

Can you point me to an example how to register an valid ImageReader for InputStreams?

Thanks!
 
lajos kamocsay
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ImageIO.read alse takes a File object. Example:



You can get a list of registered readers from ImageIO.getReaderFormatNames().


Read more here:
Working with Images
 
Simon Freienstein
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the returned List from ImageIO.getReaderFormatNames();

 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A JPEG or PNG shouldn't cause ImageIO to hiccup, as long as it's not a CMYK image (instead of an RGB one).

Are the bytes coming in from FileUpload the exact same ones that are being read from the file?
 
Simon Freienstein
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that's the point. They are. If I first write the InputStream to disk and then re-open the file it works.



Only using the InputStream directly fails (returns null).
Is there a way to tell ImageIO which ImageReader to use manually?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you know that ImageReader classes for the formats listed above are present, why not try to use it directly - it has methods setInput and read, which together should be able to read images from an InputStream.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!