简体   繁体   中英

How to extract appended data at the end of jpeg image (detecting jpeg EOF).

Since 0xFFD9 is not necessary to be present in JPEG image (as EOF marker) and even if it is present it might give incorrect result due to embedded thumbnail in jpeg, so I need to parse JPEG to extract any appended data (e,g zip). I have following java code based on the assumption that a marker will be followed by 2 byte length. But this is not the case with SOS segment ie 0xFFDA marker. How can i detect EOF in JPEG?

public String getJPEGAppendedData(DataInputStream in) {
    StringBuilder message = new StringBuilder();
    try {
        // reading first two bytes 0xFFD8
        in.readFully(new byte[2]);

        // 0xFFXX
        byte twoBytes[] = new byte[2];

        while (true) {
            in.readFully(twoBytes);
            if (twoBytes[0] == (byte) 0xFF) {
                    if (twoBytes[1] == (byte) 0xDD) {
                        // fixed 4 bytes payload
                        in.readFully(new byte[4]);
                    } else if (twoBytes[1] == (byte) 0xD9) {
                        // end of image reached
                        break;
                    } else if (twoBytes[1] >= (byte) 0xD0 && twoBytes[1] <= (byte) 0xD7) {
                        // no payload
                    } else {
                        // reading payload length form two bytes
                        short length = in.readShort();
                        System.out.println(length);

                        // skipping payload
                        for (int i = 1; i <= length - 2; i++) {
                            in.readByte();
                        }
                    }
            } else {
                break;
            }
        }

        // reading appended data (byte by byte) if any
        boolean moreData = true;
        while (moreData) {
            try {
                byte b = in.readByte();
                message.append(String.format("%02X", b));
            } catch (Exception e) {
                moreData = false;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return message.toString();
}

I don't think that you need to detect the end.

If you yourself are producing the message then you can just go to the end of the byte array containing image and detect if there's a EOI (ddf9) present if not then just add it there and append your message.

Since EOI is supposed to be the end of the image, you can append it yourself even if it's present already.

When reading your message read from the end of the file and detect ddf9 (Which may be present in the message itself, so it's better to chose a longer separator like @@SecretMessageOfMine@@ and detect that.

For example given image below

某个黑洞

You can produce image array as below

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM