【发布时间】:2020-04-22 06:15:23
【问题描述】:
我的源代码编译成功,一共生成了50张图片。
但是,没有一个恢复的图像与原始图像匹配。
所有的 jpeg 如下所示。
如您所见,它们似乎有奇怪的边缘重叠。
其中一些看起来不错,但仍然无法匹配原始图片。
如果您能提供有关如何调试的任何见解,请告诉我。
这是我的代码
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Usage: ./recover image\n");
return 1;
}
FILE *file = fopen(argv[1], "r");
if (file == NULL)
{
printf("Could not open %s.\n", argv[1]);
return 1;
}
typedef uint8_t BYTE;
BYTE buffer[512];
char *filename[8];
int jpeg_counter = 0;
bool foundStartOfJPEG = false;
FILE *img;
// read memory card until the end of file
while(fread(buffer, sizeof(BYTE) * 512, 1, file) == 1)
{
// if buffer has a signature of JPEG file,
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && ((buffer[3]) & 0xf0) == 0xe0)
{
if (foundStartOfJPEG == true)
{
fclose(img);
jpeg_counter += 1;
}
foundStartOfJPEG = true;
// create a file with index
sprintf(*filename, "%03i.jpg", jpeg_counter);
// open that file to write into it
img = fopen(*filename, "w");
// write the block of memory (buffer), to that file
fwrite(buffer, sizeof(buffer), 1, img);
}
else if (foundStartOfJPEG == true)
{
fwrite(buffer, sizeof(buffer), 1, img);
}
}
fclose(file);
return 0;
}
【问题讨论】:
-
为什么已经在 while 循环
fread()ing 中再次调用fread()? -
谢谢,我刚刚删除了
fread()的多余行。现在,我恢复了 50 张图片,但它与原始图像不匹配。我会更新问题。 @alk