Convert Byte Array to image in Java - without knowing the type

后端 未结 3 2096
轮回少年
轮回少年 2020-12-06 10:50

Sounds simple right? Use

ImageIO.read(new ByteArrayInputStream(bytes));

Here\'s the wrinkle. For some reason it is detecting a jpeg as a b

相关标签:
3条回答
  • 2020-12-06 11:10

    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 );
    
    0 讨论(0)
  • 2020-12-06 11:19

    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

    0 讨论(0)
  • 2020-12-06 11:23

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

    0 讨论(0)
提交回复
热议问题