2 q hsolucky q_hsolucky 于 2017.09.11 20:24 提问

如何解决exif中 组件数目部分按“II”字节序排列的四个十六进制字节转换为十进制
  1. #include #include #define Num 14

typedef struct pro{ //新建目录项的结构体
unsigned char label[2];
unsigned char format[2];
unsigned char component[4];
unsigned char value[4];
}catalog;

catalog *data = new catalog[Num]; //动态分配内存

FILE * readPhoto, Head_w; //定义指向文件的指针
unsigned char Exif;
unsigned char binary2[25];
int datastructure;
//unsigned char binary1[100][16];
int i,j,k,temp; //行列循环
char
outputstr; //输出内容

char datastr(int structure) // 数据格式函数
{
switch (structure)
{
case 1:printf("unsigned byte"); break;
case 2:printf("ascii strings"); break;
case 3:printf("unsigned short"); break;
case 4:printf("unsigned long"); break;
case 5:printf("unsigned rational"); break;
case 6:printf("signed byte"); break;
case 7:printf("undefined"); break;
case 8:printf("signed short"); break;
case 9:printf("signed long"); break;
case 10:printf("signed rational"); break;
case 11:printf("signed float"); break;
case 12:printf("double float"); break;
default:printf("error:");
}
return 0;
}

//int Hex2Dconvert(unsigned char Hex[][2]) //进制转换
//{
// int t, d = 0;
// for (j = 0; j < Num; j++)
// {
// for (t = 0; t // {
// if (Hex[j][t] >= 'A' && Hex[j][t] <= 'F')
// d = d * 16 + Hex[j][t] - 'A' + 10;
// else if (Hex[j][t] >= 'a' && Hex[j][t] <= 'f')
// d = d * 16 + Hex[j][t] - 'a' + 10;
// else
// d = d * 16 + Hex[j][t] - '0';
// }
// printf("%d", d);
// }
//

// return d;
//}

int main()
{

void structure(int structure);
fopen_s(&readPhoto,"E:\吴老师\wu teacher\DJI_0001.JPG", "rb"); //以只读方式打开一个二进制文件
if (!readPhoto)
{
printf("打开文件失败!\n");
system("pause");
return -1;
}
else{
printf("打开成功!\n");
fseek(readPhoto, 0L, SEEK_SET); //从文件头开始,第一个字节
fread(&Exif, sizeof(unsigned char), 1, readPhoto);
binary2[0] = Exif;
printf("%02x ", binary2[0]);
fseek(readPhoto, 0L, SEEK_CUR); //第二个字节
fread(&Exif, sizeof(unsigned char), 1, readPhoto);
binary2[1] = Exif;
printf("%02x ", binary2[1]);
outputstr = "SOI";
printf(" :%s\n", outputstr);
for (i = 2; i < 4; i++)
{
fseek(readPhoto, 0L, SEEK_CUR); //APP1部分
fread(&Exif, sizeof(unsigned char), 1, readPhoto);
binary2[i] = Exif;
printf("%02x ", binary2[i]);
}
outputstr = "APP1";
printf(" :%s\n", outputstr);
for (i = 4; i < 6; i++)
{
fseek(readPhoto, 0L, SEEK_CUR); //APP1数据大小
fread(&Exif, sizeof(unsigned char), 1, readPhoto);
binary2[i] = Exif;
printf("%02x ", binary2[i]);
}
outputstr = "the number of APP1";
printf(" :%s\n", outputstr);

    for (i = 6; i < 12; i++)
    {
        fseek(readPhoto, 0L, SEEK_CUR);                                //EXIF头
        fread(&Exif, sizeof(unsigned char), 1, readPhoto);
        binary2[i] = Exif;
        printf("%02x ", binary2[i]);
    }
    outputstr = "EXIF头";
    printf(" :%s\n", outputstr);
    for (i = 12; i < 20; i++)
    {
        fseek(readPhoto, 0L, SEEK_CUR);                                //TIFF头
        fread(&Exif, sizeof(unsigned char), 1, readPhoto);
        binary2[i] = Exif;
        printf("%02x ", binary2[i]);
    }
    outputstr = "TIFF头";
    printf(" :%s\n", outputstr);
    for (i = 20; i < 22; i++)
    {
        fseek(readPhoto, 0L, SEEK_CUR);                                //目录项的号码
        fread(&Exif, sizeof(unsigned char), 1, readPhoto);
        binary2[i] = Exif;
        printf("%02x ", binary2[i]);
    }
    outputstr = "目录项的号码";
    printf(" :%s\n", outputstr);
}


for (j = 0; j < Num; j++)
{
    printf("项目%d\n", j);
    for (k = 0; k < 2; k++)
    {
        fseek(readPhoto, 0L, SEEK_CUR);                                //目录项
        fread(&Exif, sizeof(unsigned char), 1, readPhoto);
        data[j].label[k] = Exif;
        printf("%02x ", data[j].label[k]);
    }
    printf(":标签号");
    printf("\n");
    for (k = 0; k < 2; k++)
    {
        fseek(readPhoto, 0L, SEEK_CUR);                                //目录项
        fread(&Exif, sizeof(unsigned char), 1, readPhoto);
        data[j].format[k] = Exif;
        printf("%02x ", data[j].format[k]);
    }
    printf(":数据值=%d,对应的数据格式为:", data[j].format[0] );
    temp = (int)(data[j].format[0]);                                  //十六进制强转十进制
    //temp = Hex2Dconvert(temp);
    printf("", datastr(temp));

    printf("\n");

    for (k = 0; k < 4; k++)
    {
        fseek(readPhoto, 0L, SEEK_CUR);                                //目录项
        fread(&Exif, sizeof(unsigned char), 1, readPhoto);
        data[j].component[k] = Exif;
        printf("%02x ", data[j].component[k]);
    }
    printf(":组件数目");
    printf("\n");
    for (k = 0; k < 4; k++)
    {
        fseek(readPhoto, 0L, SEEK_CUR);                                //目录项
        fread(&Exif, sizeof(unsigned char), 1, readPhoto);
        data[j].value[k] = Exif;
        printf("%02x ", data[j].value[k]);
    }
    printf(":数据值");
    printf("\n");
}

fclose(readPhoto);
system("pause");
delete data;
return 0;

    //
    //for (i = 1;i<=15; i++)
    //{
    //  fseek(readPhoto, 0L, SEEK_CUR);                                    //内容循环部分
    //  fread(&Exif, sizeof(unsigned char), 1, readPhoto);
    //  binary0[i] = Exif;
    //  printf("%02x ", binary0[i]);
    //  if (i%15==0) printf("\n");
    //}
//else
//{
//  printf("打开成功!\n");
//  for (j = 0; j < 2; j++)                                                //行循环    
//  {
//      for (i = 0; i <16; i++)                                            //列循环
//      {
//          fseek(readPhoto, 0L, SEEK_CUR);                                //内容循环部分
//          fread(&Exif, sizeof(unsigned char), 1, readPhoto);
//          binary1[j][i] = Exif;
//          printf("%02x ", binary1[j][i]);
//          if (i!=0&&i % 15 == 0) printf("\n");
//      }
//  }
    //fseek(readPhoto, 0L, SEEK_CUR);                                      //内容循环部分
    //fread(&Exif, sizeof(unsigned char), 1, readPhoto);
    //printf("%x", Exif);
}
//Exif = 97;
//fopen_s(&Head_w, "C:\\Users\\Administrator\\Desktop\\Data.txt", "w");//以只写方式打开一个文本文件,指定文件不存在,建立新文件
////fopen_s(&Head_w, "Data.txt", "w");
//for (j = 0; j < 2; j++)                                               //行循环    
//{
//  for (i = 0; i < 16; i++)                                            //列循环
//  {
//      fprintf(Head_w, "%02x ", binary1[j][i]);
//  }
//}
////fwrite(&Exif, sizeof(unsigned char), 1, Head_w);
//fclose(readPhoto);
//fclose(Head_w);
//system("pause");
//return 0;

//}

Csdn user default icon
上传中...
上传图片
插入图片