14

Sounds simple right? Use

ImageIO.read(new ByteArrayInputStream(bytes));

Here's the wrinkle. For some reason it is detecting a jpeg as a bmp, and that is the first ImageReader returned when I call

ImageInputStream iis = ImageIO.createImageInputStream(new ByteArrayInputStream(bytes));
Iterator<ImageReader> readers=ImageIO.getImageReaders(iis);

This causes the image to come out corrupted. Is there a way to tell through java short of looking directly at the bytes for the header, and failing that does anyone know of a good reference for the byte headers for the different images?

Just letting you guys know I am still working on this. I'll let you know if/when I have an answer. I thank all of you for your responses so far.

PHeath
  • 1,157
  • 3
  • 13
  • 12
  • [This page](http://www.astro.keele.ac.uk/oldusers/rno/Computing/File_magic.html) might be a good starting point for magic numbers. [Here](http://en.wikipedia.org/wiki/Magic_number_(programming)) is something similar from Wikipedia. – R Ubben Jul 31 '09 at 14:44

3 Answers3

3

Haven't played with ImageIO in a awhile, and have not tested this, but I seem to recall something like this working. (since you say you know your file is a jpg and not a bitmap, I am using that information to help find the right loader).

String inFormat = "jpg";

Iterator inReaders = ImageIO.getImageReadersByFormatName(inFormat);

...

nextInReader.setInput( iis );
jedierikb
  • 12,752
  • 22
  • 95
  • 166
  • That's correct but the problem is the data collection is a mix of bmp, png, jpg, etc. So any of them COULD be valid, the trick is telling which one solely based upon the bytes. – PHeath Jul 31 '09 at 14:46
0

For the reference you can have a look at wikipedia, you can find the header of the different formats there.
http://en.wikipedia.org/wiki/Graphics_Interchange_Format
http://en.wikipedia.org/wiki/BMP_file_format
http://en.wikipedia.org/wiki/JPEG

mic.sca
  • 1,688
  • 2
  • 18
  • 34
-1

Is the BMP reader the only one returned by getImageReaders()? Maybe you get more than one and can make a choice based on that.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720