我正在尝试读取.raw文件并恢复JPG文件,然后创建其中的50个。我可以编译,但我的输出不显示,虽然我有所有50个jpg文件。
我已成功打印50张JPG照片,名称从000.jpg到049.jpg。当试图打开它们时,我收到了这条消息:
解释JPEG图像文件时出错(在状态201中对JPEG库的不正确调用)
我希望在打开另一个文件之前正确确保文件已关闭
这是我的代码:
#define JPEG1 0xff 0xd8 0xff 0xe0
#define JPEG2 0xff 0xd8 0xff 0xe1
#define BLOCK 512
int main(int argc, char* argv[])
{
// long enough to store the name of a jpeg file
char jpeg_name[4];
// where we are going to store our data
BYTE buffer[512];
// open the picture file
FILE* file = fopen("card.raw", "r");
// error checking
if (file == NULL)
{
printf("File could not be opened");
return 2;
}
// how many jpegs we have at any one time
int jpeg_num = 0;
// check if we're open
int open = 0;
// the outfile we will use for all jpeg files
FILE* jpeg = NULL;
// do this until we can't come up with a full 512, fread returns what it has succesfully read
// dont need to use address operator for image_data because its an array
while (fread(buffer, sizeof(BYTE), BLOCK, file) == BLOCK)
{
// this will help us count and name files
int i = 0;
// if this the begenning of a jpeg file?
if ((buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff) && (buffer[3] == 0xe0 || buffer[3] == 0xe1))
{
// is there a jpeg file already open?
//if (fopen("jpeg_name", "r") != NULL)
if(open == 1)
{
fclose(jpeg);
open = 0;
}
// name the jpegs we find
sprintf(jpeg_name, "%03d.jpg", jpeg_num+i);
// open the jpeg from sprintf
jpeg = fopen(jpeg_name, "w");
open = 1;
// error checking
if (jpeg == NULL)
{
printf("JPEG file could not be created");
return 1;
}
// write to our file
fwrite(buffer, sizeof(BYTE), BLOCK, jpeg);
// increment counter
i++;
jpeg_num += 1;
}
}
if(jpeg)
{
fclose(jpeg);
}
fclose(file);
return 0;
}
答案 0 :(得分:0)
我们来看看。
jpeg_name变量的大小为3,但您要写入8个字节:001.jpg(null)
您正在阅读所有内容,但只是编写每个JPEG样式文件的第一个块(假设您的标题正确)。
你是否确定在JPEG内的随机二进制文件中不会出现二进制字符串0xff 0xd8 0xff 0xe0?
答案 1 :(得分:0)
我有相同的编程任务。我在代码中注意到的一件事是jpeg的第四个字节的范围可以从0xe0到0xef。在您的代码中,我只会看到 buffer [3] == 0xe0 ||缓冲区[3] == 0xe1