c++获取图片的位置信息

EasyEXIF 简介:
     EasyEXIF 是一个小巧轻量级的C++库,用于解析 JPEG 文件中的基本信息。
在项目只需要两个文件 exif.cpp、exif.h,不依赖于任何构建系统或外部库。

使用功能easyexif库获取图片的经纬度信息接口:

void  getPositionInfo(char* photoPath,double &latitude,double &longitude,unsigned char &latDirection,unsigned char &lonDirection)
{
    FILE *fp = fopen(photoPath,"rb");
    if(!fp)
    {
        printf("can not open photoPath:%s\n",photoPath);
        exit(-1);
    }
    fseek(fp,0,SEEK_END);
    unsigned long fileSize = ftell(fp);
    rewind(fp);
    unsigned char *buf = new unsigned char[fileSize];
    if(fread(buf,1,fileSize,fp) != fileSize)
    {
        printf("can not read photoPath:%s\n",photoPath);
        delete [] buf;
        exit(-1);
    }
    fclose(fp);

    //Parse EXIF
    easyexif::EXIFInfo result;
    int code = result.parseFrom(buf,fileSize);
    delete[] buf;
    if(code && code != 1983)
    {
        printf("parse EXIF INFO error,code %d\n",code);
        exit(-1);
    }
    if(code == 1983)
    {
        //维度
        latitude = 0;
        latDirection = 'N';
        //经度
        longitude = 0;
        lonDirection = 'E';
        return;
    }
    //维度
    latitude = result.GeoLocation.Latitude;
    latDirection = result.GeoLocation.LatComponents.direction;
    //经度
    longitude = result.GeoLocation.Longitude;
    lonDirection = result.GeoLocation.LonComponents.direction;

    if(latDirection != 'N' && latDirection != 'S')
        latDirection = 'N';
    if(lonDirection != 'E' && lonDirection != 'W')
        lonDirection = 'E';

    printf("GPS Latitude: %f deg (%f deg, %f min, %f sec %c)\n",
               result.GeoLocation.Latitude, result.GeoLocation.LatComponents.degrees,
               result.GeoLocation.LatComponents.minutes,
               result.GeoLocation.LatComponents.seconds,
               result.GeoLocation.LatComponents.direction);
    printf("GPS Longitude: %f deg (%f deg, %f min, %f sec %c)\n",
               result.GeoLocation.Longitude, result.GeoLocation.LonComponents.degrees,
               result.GeoLocation.LonComponents.minutes,
               result.GeoLocation.LonComponents.seconds,
               result.GeoLocation.LonComponents.direction);
}

参数说明:
photoPath:文件的全路径
latitude:维度数值,单位°
longitude:经度数值,单位°
latDirection:维度方向:N/S
lonDirection:经度方向:E/W

你可能感兴趣的:(c++,qt,图像处理)