我想从JPEG头文件中读取图像标记的开头。如何从c?
中的JPEG头文件中读取此标记答案 0 :(得分:0)
图片代码的开头是FF
D8
,因此您需要在序列中找到这些字节,例如:
#include <stdio.h>
#include <string.h>
int main(void)
{
unsigned char seq[] = {0x01, 0x02, 0xFF, 0xD8, 'S', 'O', 'I', 0x00};
unsigned char *res = seq;
/* We can not use strstr because seq is not a valid string */
while ((res = memchr(res, 0xFF, sizeof seq - (res - seq)))) {
if (*(++res) == 0xD8) {
res++; /* + 1 to consume 0xD8 */
break;
}
}
if (res != NULL) {
printf("%s\n", res);
}
return 0;
}
输出:
SOI
答案 1 :(得分:0)
jpeg标题中的任何标记前面都有0xFF
的起始标记,soi标记为0xD8
。
因此,您可以简单地遍历数据,直到找到值为0xFF
的字节,并检查后面的字节是否为0xD8
。恭喜你,你现在已经找到了soi标记。