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

前端 未结 6 1773
没有蜡笔的小新
没有蜡笔的小新 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:25

    From wikipedia:

    JPEG image files begin with FF D8 and end with FF D9.

    http://en.wikipedia.org/wiki/Magic_number_(programming)

    0 讨论(0)
  • 2020-11-27 05:29

    Some Extra info about other file format with jpeg: initial of file contains these bytes

    BMP : 42 4D
    JPG : FF D8 FF EO ( Starting 2 Byte will always be same)
    PNG : 89 50 4E 47
    GIF : 47 49 46 38
    

    some code:

    private static Boolean isJPEG(File filename) throws Exception {
        DataInputStream ins = new DataInputStream(new BufferedInputStream(new FileInputStream(filename)));
        try {
            if (ins.readInt() == 0xffd8ffe0) {
                return true;
            } else {
                return false;
    
            }
        } finally {
            ins.close();
        }
    }
    
    0 讨论(0)
  • 2020-11-27 05:29

    Another source of "knowledge" about magic numbers (including for JPEG files) is the magic file used by the GNU/Linux file command.

    If you have the file command installed, then file --version will tell you where the magic file lives, and you can read it using a text editor ... and careful reading of man 5 magic.

    (And the magic file contents confirm the details of other answers.)

    0 讨论(0)
  • 2020-11-27 05:39

    A JPG file does have a specific header that you could use to determine a very good likelihood that it is a JPG file. However, it's not clear if you will have the entire file in the byte array.

    Anyway, here's specifics on the header: http://www.fastgraph.com/help/jpeg_header_format.html

    0 讨论(0)
  • 2020-11-27 05:45

    Quoting this wikipedia article:

    JPEG image files begin with FF D8 and end with FF D9. JPEG/JFIF files contain the ASCII code for "JFIF" (4A 46 49 46) as a null terminated string. JPEG/Exif files contain the ASCII code for "Exif" (45 78 69 66) also as a null terminated string, followed by more metadata about the file.

    0 讨论(0)
  • 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."

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