How to identify contents of a byte[] is a JPEG?

前端 未结 6 1801
没有蜡笔的小新
没有蜡笔的小新 2020-11-27 05:24

I have a small byte array (under 25K) that I receive and decode as part of a larger message envelope. Sometimes this is an image, furthermore it is a JPG. I have no context

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-27 05:46

    A lot of formats are identified by so-called magic numbers. These are byte sequences usually in the front of the file to identify whether the following binary data is really what you think it is. A quick google search returned: http://www.linfo.org/magic_number.html and specifically the citation:

    "Similarly, a commonly used magic number for JPEG (Joint Photographic Experts Group) image files is 0x4A464946, which is the ASCII equivalent of JFIF (JPEG File Interchange Format). However, JPEG magic numbers are not the first bytes in the file; rather, they begin with the seventh byte. Additional examples include 0x4D546864 for MIDI (Musical Instrument Digital Interface) files and 0x425a6831415925 for bzip2 compressed files."

提交回复
热议问题