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

前端 未结 6 1799
没有蜡笔的小新
没有蜡笔的小新 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: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();
        }
    }
    

提交回复
热议问题