1 #include <iostream>
2 #include <exiv2/exiv2.hpp>
3 #pragma comment(lib,"libexiv2.lib")
4 #pragma comment(lib,"xmpsdk.lib")
5 #pragma comment(lib,"libexpat.lib")
6 #pragma comment(lib,"zlib1.lib")
7
8 //查找一个ExifKey避免没有的Key产生段错误
9 std::string FindExifKey(Exiv2::ExifData &ed, std::string key) {
10 Exiv2::ExifKey tmp = Exiv2::ExifKey(key);
11 Exiv2::ExifData::iterator pos = ed.findKey(tmp);
12 if (pos == ed.end()) {
13 return "Unknow";
14 }
15 return pos->value().toString();
16 }
17
18 int main(int argc, char *argv[]) {
19
20 if (argc < 2) {
21 std::cout << "Need a File.";
22 return 0;
23 }
24
25 //判断文件是否有效
26 if (!Exiv2::fileExists(argv[1], true)) {
27 std::cout << "File Path Error." << std::endl;
28 return -1;
29 }
30
31 //获取文件大小
32 FILE *FP = fopen(argv[1], "rb");
33 fseek(FP, 0, SEEK_END); //定位到文件末
34 int FileSize = ftell(FP); //文件长度
35 fclose(FP);
36
37 Exiv2::Image::AutoPtr &image = Exiv2::ImageFactory::open(argv[1]);
38 if (image.get() == 0)
39 {
40 std::cout << "Read Exif Error." << std::endl;
41 return -2;
42 }
43
44 //读取照片的exif信息
45 image->readMetadata();
46 Exiv2::ExifData ed = image->exifData();//得到exif信息
47
48 if (ed.empty())
49 {
50 std::cout << "Not Find ExifInfo" << std::endl;
51 return -2;
52 }
53
54 std::cout << "文件名称 :" << argv[1] << std::endl;
55 std::cout << "文件大小 :" << FileSize << std::endl;
56 std::cout << "版权 :" << FindExifKey(ed, "Exif.Image.Copyright") << std::endl;
57 std::cout << "创作人 :" << FindExifKey(ed, "Exif.Image.Artist") << std::endl;
58 std::cout << "相机品牌 :" << FindExifKey(ed, "Exif.Image.Make") << std::endl;
59 std::cout << "相机型号 :" << FindExifKey(ed, "Exif.Image.Model") << std::endl;
60 std::cout << "镜头型号 :" << FindExifKey(ed, "Exif.Photo.LensModel") << std::endl;
61 std::cout << "镜头序列号 :" << FindExifKey(ed, "Exif.Photo.LensSerialNumber") << std::endl;
62 std::cout << "ISO :" << FindExifKey(ed, "Exif.Photo.ISOSpeedRatings") << std::endl;
63 std::cout << "光圈 :" << FindExifKey(ed, "Exif.Photo.FNumber") << std::endl;
64 std::cout << "焦距 :" << FindExifKey(ed, "Exif.Photo.FocalLength") << std::endl;
65 std::cout << "快门 :" << FindExifKey(ed, "Exif.Photo.ExposureTime") << std::endl;
66 std::cout << "拍摄时间 :" << FindExifKey(ed, "Exif.Image.DateTime") << std::endl;
67 std::cout << "闪光灯 :" << FindExifKey(ed, "Exif.CanonCs.FlashMode") << std::endl;
68 std::cout << "水平分辨率 :" << FindExifKey(ed, "Exif.Image.XResolution") << std::endl;
69 std::cout << "垂直分辨率 :" << FindExifKey(ed, "Exif.Image.YResolution") << std::endl;
70 std::cout << "照片尺寸 :" << FindExifKey(ed, "Exif.Photo.PixelYDimension") << " x " << FindExifKey(ed, "Exif.Photo.PixelXDimension") << std::endl;
71
72 std::cout << "\n下面是原始信息:\n" << std::endl;
73 for (Exiv2::ExifData::iterator tmp = ed.begin(); tmp != ed.end(); tmp++) {
74 std::cout << tmp->tagName() << " (" << tmp->key() << ") " << tmp->value() <<std::endl;
75 }
76 return 0;
77 }