EasyEXIF 简介
EasyEXIF 是一个小巧轻量级的C++库,用于解析 JPEG 文件中的基本信息。它只使用 std::string 库,是纯C++。它传递 JPEG 文件的二进制内容,解析几个最重要的 EXIF 字段。
如何使用?项目只需要两个文件 exif.cpp、exif.h,不依赖于任何构建系统或外部库。
解析效果
解析JPEG图片exif的代码
#include <iostream>
#include "exif.h"
void EXIT(const char *buf)
{
std::cout << buf << "\n";
system("pause");
}
int main()
{
// FILE打开图片
const char *JpgPath = "./test.jpeg";
FILE *fp = fopen(JpgPath, "rb");
if (!fp) { EXIT("read jpg fail!.."); return -1; }
// 获取图片size、data
fseek(fp, 0, SEEK_END);
unsigned long fsize = ftell(fp);
rewind(fp);
unsigned char *buf = new unsigned char[fsize];
if (fread(buf, 1, fsize, fp) != fsize)
{
EXIT("Can't read file.");
delete[] buf;
return -2;
}
fclose(fp);
// 解析 EXIF
easyexif::EXIFInfo result;
int code = result.parseFrom(buf, fsize);
delete[] buf;
if (code)
{
// 错误号:code
EXIT("Error parsing EXIF error");
return -3;
}
// 获取exif信息
printf("Camera make : %s\n", result.Make.c_str()); // 相机制造商
printf("Camera model : %s\n", result.Model.c_str()); // 摄像头型号
printf("Software : %s\n", result.Software.c_str()); // 软件
printf("Bits per sample : %d\n", result.BitsPerSample); // 样本位数
printf("Image width : %d\n", result.ImageWidth); // 图像宽度
printf("Image height : %d\n", result.ImageHeight); // 图像高度
printf("Image description : %s\n", result.ImageDescription.c_str()); // 图像描述
printf("Image orientation : %d\n", result.Orientation); // 图像方向
printf("Image copyright : %s\n", result.Copyright.c_str()); // 图片版权
printf("Image date/time : %s\n", result.DateTime.c_str()); // 图像日期/时间
printf("Original date/time : %s\n", result.DateTimeOriginal.c_str()); // 原始日期/时间
printf("Digitize date/time : %s\n", result.DateTimeDigitized.c_str()); // 数字化日期/时间
printf("Subsecond time : %s\n", result.SubSecTimeOriginal.c_str()); // 亚秒时间
printf("Exposure time : 1/%d s\n",
(unsigned)(1.0 / result.ExposureTime)); // 曝光时间(s)
printf("F-stop : f/%.1f\n", result.FNumber); // F-停止
printf("Exposure program : %d\n", result.ExposureProgram); // 暴光程序
printf("ISO speed : %d\n", result.ISOSpeedRatings); // ISO速度
printf("Subject distance : %f m\n", result.SubjectDistance); // 受试者距离
printf("Exposure bias : %f EV\n", result.ExposureBiasValue); // 暴光偏差
printf("Flash used? : %d\n", result.Flash); // 使用闪光灯?
printf("Flash returned light : %d\n", result.FlashReturnedLight); // 闪光返回灯
printf("Flash mode : %d\n", result.FlashMode); // Flash模式
printf("Metering mode : %d\n", result.MeteringMode); // 测光模式
printf("Lens focal length : %f mm\n", result.FocalLength); // 透镜焦距
printf("35mm focal length : %u mm\n", result.FocalLengthIn35mm); // 35mm焦距
printf("GPS Latitude : %f deg (%f deg, %f min, %f sec %c)\n",
result.GeoLocation.Latitude, // GPS纬度
result.GeoLocation.LatComponents.degrees, // GPS纬度 - 度
result.GeoLocation.LatComponents.minutes, // GPS纬度 - 分
result.GeoLocation.LatComponents.seconds, // GPS纬度 - 秒
result.GeoLocation.LatComponents.direction); // GPS纬度 - 方向
printf("GPS Longitude : %f deg (%f deg, %f min, %f sec %c)\n",
result.GeoLocation.Longitude, // GPS经度
result.GeoLocation.LonComponents.degrees, // GPS经度 - 度
result.GeoLocation.LonComponents.minutes, // GPS经度 - 分
result.GeoLocation.LonComponents.seconds, // GPS经度 - 秒
result.GeoLocation.LonComponents.direction); // GPS经度 - 方向
printf("GPS Altitude : %f m\n", result.GeoLocation.Altitude); // GPS高度
printf("GPS Precision (DOP) : %f\n", result.GeoLocation.DOP); // GPS精度(DOP)
printf("Lens min focal length: %f mm\n", result.LensInfo.FocalLengthMin); // 最小焦距(mm)
printf("Lens max focal length: %f mm\n", result.LensInfo.FocalLengthMax); // 最大焦距(mm)
printf("Lens f-stop min : f/%.1f\n", result.LensInfo.FStopMin); // 最小光圈(f-stop)
printf("Lens f-stop max : f/%.1f\n", result.LensInfo.FStopMax); // 最大光圈(f-stop)
printf("Lens make : %s\n", result.LensInfo.Make.c_str()); // 透镜制造商
printf("Lens model : %s\n", result.LensInfo.Model.c_str()); // 透镜模型
printf("Focal plane XRes : %f\n", result.LensInfo.FocalPlaneXResolution); // 焦平面X分辨率
printf("Focal plane YRes : %f\n", result.LensInfo.FocalPlaneYResolution); // 焦平面Y分辨率
system("pause");
return 0;
}
关注
笔者 - jxd