繁体   English   中英

如何在jpeg图像的末尾提取附加数据(检测jpeg EOF)。

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

由于0xFFD9不一定存在于JPEG图像中(作为EOF标记),即使它存在,也可能由于jpeg中嵌入的缩略图而给出不正确的结果,因此我需要解析JPEG以提取任何附加数据(例如,g zip) )。 我有以下java代码,假设标记后面跟着2个字节的长度。 但这不是SOS段的情况,即0xFFDA标记。 如何在JPEG中检测EOF?

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();
}

我认为你不需要检测结束。

如果您自己正在生成消息,那么您可以直接转到包含图像的字节数组的末尾并检测是否存在EOI (ddf9) ,如果没有,则只需将其添加到那里并附加您的消息。

由于EOI应该是图像的结尾,所以即使它已经存在,您也可以自己添加它。

当读取从文件末尾读取的消息并检测到ddf9 (可能存在于消息本身中时,因此最好选择较长的分隔符,如@@SecretMessageOfMine@@并检测到它。

例如下面的图片

某个黑洞

您可以生成如下图像数组

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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