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.

4

3 回答 3

3

有一段时间没有玩过 ImageIO,也没有测试过,但我似乎记得这样的工作。(因为您说您知道您的文件是 jpg 而不是位图,所以我正在使用该信息来帮助找到正确的加载器)。

String inFormat = "jpg";

Iterator inReaders = ImageIO.getImageReadersByFormatName(inFormat);

...

nextInReader.setInput( iis );
于 2009-07-31T14:42:59.143 回答
1

作为参考,您可以查看维基百科,您可以在那里找到不同格式的标题。
http://en.wikipedia.org/wiki/Graphics_Interchange_Format
http://en.wikipedia.org/wiki/BMP_file_format
http://en.wikipedia.org/wiki/JPEG

于 2009-07-31T14:48:34.390 回答
0

BMP阅读器是唯一返回的getImageReaders()吗?也许你得到了不止一个,并且可以根据它做出选择。

于 2009-07-31T14:49:51.423 回答