Exiv2 照片EXIF信息库(c++)

发布时间 2023-06-27 15:56:07作者: 一杯清酒邀明月

前一段时间要做基于flicker提供的带有exif信息的照片库给无地理信息的photos进行定位。
大部分exif的库都不是很好用,exiv2 (http://www.exiv2.org) 挺不错。但在windows下编译很烦。

 1 //need Qt4/Qt5 support
 2 #include <QtCore>
 3 #include <iostream>
 4 #include <exiv2/exiv2.hpp>
 5 using namespace std;
 6 using namespace Exiv2;
 7 
 8 QStringList loadFromFile(const char* sfilename)
 9 {
10     QStringList slist;
11     slist.clear();
12 
13     QFile _file(sfilename);
14     if (!_file.open(QIODevice::ReadOnly))
15     {
16         cout<<"open file failed. error."<<endl;
17         return slist;
18     }
19 
20     QTextStream _in(&_file);
21 
22     while(!_in.atEnd())
23     {
24         QString str = _in.readLine().trimmed();
25         if(str.isEmpty()) continue;
26         slist.append(str);
27     }
28 
29     cout<<"slist size = "<<slist.size()<<endl;
30     return slist;
31 }
32 
33 
34 int main()
35 {
36 
37     Image::AutoPtr image = ImageFactory::open("/Users/Yaoyao/Desktop/IMG_2428.JPG");
38     if(image.get() == 0)
39     {
40         cout<<"read file error."<<endl;
41         return -1;
42     }
43     image->readMetadata();
44     ExifData ed = image->exifData();
45 
46     if(ed.empty())
47     {
48         cout<<"ed empty error!"<<endl;
49         return -2;
50     }
51 
52     //some tag location
53     cout<<ed["Exif.Image.GPSTag"]<<endl;
54     cout<<ed["Exif.GPSInfo.GPSLatitudeRef"]<<endl;
55     cout<<ed["Exif.GPSInfo.GPSLatitude"]<<endl;
56     cout<<ed["Exif.GPSInfo.GPSLongitudeRef"]<<endl;
57     cout<<ed["Exif.GPSInfo.GPSLongitude"]<<endl;
58     cout<<ed["Exif.GPSInfo.GPSAltitudeRef"]<<endl;
59     cout<<ed["Exif.GPSInfo.GPSAltitude"]<<endl;
60     cout<<ed["Exif.GPSInfo.GPSDateStamp"]<<endl;
61 
62 
63     //test
64     QStringList slist = loadFromFile("/Users/Yaoyao/Documents/Code/ReadPhotoExif/ReadPhotoExif/exif_tags.txt");
65     if(slist.size() == 0)
66         return -1;
67 
68     foreach(QString str, slist)
69     {
70         try
71         {
72             cout<<str.toStdString().data()<<": "<<ed[str.toStdString().data()]<<endl;
73         }
74         catch (...)
75         {
76             //cout<<"[ EXCEPTION !!! ]"<<str.toStdString().data()<<endl;
77             continue;
78         }
79     }
80 
81 
82     image->clearExifData();
83 
84     return 1;
85 }

其中tag标签支持:
http://www.exiv2.org/tags.html

  1 Exif.Image.ProcessingSoftware
  2 Exif.Image.NewSubfileType
  3 Exif.Image.SubfileType
  4 Exif.Image.ImageWidth
  5 Exif.Image.ImageLength
  6 Exif.Image.BitsPerSample
  7 Exif.Image.Compression
  8 Exif.Image.PhotometricInterpretation
  9 Exif.Image.Thresholding
 10 Exif.Image.CellWidth
 11 Exif.Image.CellLength
 12 Exif.Image.FillOrder
 13 Exif.Image.DocumentName
 14 Exif.Image.ImageDescription
 15 Exif.Image.Make
 16 Exif.Image.Model
 17 Exif.Image.StripOffsets
 18 Exif.Image.Orientation
 19 Exif.Image.SamplesPerPixel
 20 Exif.Image.RowsPerStrip
 21 Exif.Image.StripByteCounts
 22 Exif.Image.XResolution
 23 Exif.Image.YResolution
 24 Exif.Image.PlanarConfiguration
 25 Exif.Image.GrayResponseUnit
 26 Exif.Image.GrayResponseCurve
 27 Exif.Image.T4Options
 28 Exif.Image.T6Options
 29 Exif.Image.ResolutionUnit
 30 Exif.Image.PageNumber
 31 Exif.Image.TransferFunction
 32 Exif.Image.Software
 33 Exif.Image.DateTime
 34 Exif.Image.Artist
 35 Exif.Image.HostComputer
 36 Exif.Image.Predictor
 37 Exif.Image.WhitePoint
 38 Exif.Image.PrimaryChromaticities
 39 Exif.Image.ColorMap
 40 Exif.Image.HalftoneHints
 41 Exif.Image.TileWidth
 42 Exif.Image.TileLength
 43 Exif.Image.TileOffsets
 44 Exif.Image.TileByteCounts
 45 Exif.Image.SubIFDs
 46 Exif.Image.InkSet
 47 Exif.Image.InkNames
 48 Exif.Image.NumberOfInks
 49 Exif.Image.DotRange
 50 Exif.Image.TargetPrinter
 51 Exif.Image.ExtraSamples
 52 Exif.Image.SampleFormat
 53 Exif.Image.SMinSampleValue
 54 Exif.Image.SMaxSampleValue
 55 Exif.Image.TransferRange
 56 Exif.Image.ClipPath
 57 Exif.Image.XClipPathUnits
 58 Exif.Image.YClipPathUnits
 59 Exif.Image.Indexed
 60 Exif.Image.JPEGTables
 61 Exif.Image.OPIProxy
 62 Exif.Image.JPEGProc
 63 Exif.Image.JPEGInterchangeFormat
 64 Exif.Image.JPEGInterchangeFormatLength
 65 Exif.Image.JPEGRestartInterval
 66 Exif.Image.JPEGLosslessPredictors
 67 Exif.Image.JPEGPointTransforms
 68 Exif.Image.JPEGQTables
 69 Exif.Image.JPEGDCTables
 70 Exif.Image.JPEGACTables
 71 Exif.Image.YCbCrCoefficients
 72 Exif.Image.YCbCrSubSampling
 73 Exif.Image.YCbCrPositioning
 74 Exif.Image.ReferenceBlackWhite
 75 Exif.Image.XMLPacket
 76 Exif.Image.Rating
 77 Exif.Image.RatingPercent
 78 Exif.Image.ImageID
 79 Exif.Image.CFARepeatPatternDim
 80 Exif.Image.CFAPattern
 81 Exif.Image.BatteryLevel
 82 Exif.Image.Copyright
 83 Exif.Image.ExposureTime
 84 Exif.Image.FNumber
 85 Exif.Image.IPTCNAA
 86 Exif.Image.ImageResources
 87 Exif.Image.ExifTag
 88 Exif.Image.InterColorProfile
 89 Exif.Image.ExposureProgram
 90 Exif.Image.SpectralSensitivity
 91 Exif.Image.GPSTag
 92 Exif.Image.ISOSpeedRatings
 93 Exif.Image.OECF
 94 Exif.Image.Interlace
 95 Exif.Image.TimeZoneOffset
 96 Exif.Image.SelfTimerMode
 97 Exif.Image.DateTimeOriginal
 98 Exif.Image.CompressedBitsPerPixel
 99 Exif.Image.ShutterSpeedValue
100 Exif.Image.ApertureValue
101 Exif.Image.BrightnessValue
102 Exif.Image.ExposureBiasValue
103 Exif.Image.MaxApertureValue
104 Exif.Image.SubjectDistance
105 Exif.Image.MeteringMode
106 Exif.Image.LightSource
107 Exif.Image.Flash
108 Exif.Image.FocalLength
109 Exif.Image.FlashEnergy
110 Exif.Image.SpatialFrequencyResponse
111 Exif.Image.Noise
112 Exif.Image.FocalPlaneXResolution
113 Exif.Image.FocalPlaneYResolution
114 Exif.Image.FocalPlaneResolutionUnit
115 Exif.Image.ImageNumber
116 Exif.Image.SecurityClassification
117 Exif.Image.ImageHistory
118 Exif.Image.SubjectLocation
119 Exif.Image.ExposureIndex
120 Exif.Image.TIFFEPStandardID
121 Exif.Image.SensingMethod
122 Exif.Image.XPTitle
123 Exif.Image.XPComment
124 Exif.Image.XPAuthor
125 Exif.Image.XPKeywords
126 Exif.Image.XPSubject
127 Exif.Image.PrintImageMatching
128 Exif.Image.DNGVersion
129 Exif.Image.DNGBackwardVersion
130 Exif.Image.UniqueCameraModel
131 Exif.Image.LocalizedCameraModel
132 Exif.Image.CFAPlaneColor
133 Exif.Image.CFALayout
134 Exif.Image.LinearizationTable
135 Exif.Image.BlackLevelRepeatDim
136 Exif.Image.BlackLevel
137 Exif.Image.BlackLevelDeltaH
138 Exif.Image.BlackLevelDeltaV
139 Exif.Image.WhiteLevel
140 Exif.Image.DefaultScale
141 Exif.Image.DefaultCropOrigin
142 Exif.Image.DefaultCropSize
143 Exif.Image.ColorMatrix1
144 Exif.Image.ColorMatrix2
145 Exif.Image.CameraCalibration1
146 Exif.Image.CameraCalibration2
147 Exif.Image.ReductionMatrix1
148 Exif.Image.ReductionMatrix2
149 Exif.Image.AnalogBalance
150 Exif.Image.AsShotNeutral
151 Exif.Image.AsShotWhiteXY
152 Exif.Image.BaselineExposure
153 Exif.Image.BaselineNoise
154 Exif.Image.BaselineSharpness
155 Exif.Image.BayerGreenSplit
156 Exif.Image.LinearResponseLimit
157 Exif.Image.CameraSerialNumber
158 Exif.Image.LensInfo
159 Exif.Image.ChromaBlurRadius
160 Exif.Image.AntiAliasStrength
161 Exif.Image.ShadowScale
162 Exif.Image.DNGPrivateData
163 Exif.Image.MakerNoteSafety
164 Exif.Image.CalibrationIlluminant1
165 Exif.Image.CalibrationIlluminant2
166 Exif.Image.BestQualityScale
167 Exif.Image.RawDataUniqueID
168 Exif.Image.OriginalRawFileName
169 Exif.Image.OriginalRawFileData
170 Exif.Image.ActiveArea
171 Exif.Image.MaskedAreas
172 Exif.Image.AsShotICCProfile
173 Exif.Image.AsShotPreProfileMatrix
174 Exif.Image.CurrentICCProfile
175 Exif.Image.CurrentPreProfileMatrix
176 Exif.Image.ColorimetricReference
177 Exif.Image.CameraCalibrationSignature
178 Exif.Image.ProfileCalibrationSignature
179 Exif.Image.AsShotProfileName
180 Exif.Image.NoiseReductionApplied
181 Exif.Image.ProfileName
182 Exif.Image.ProfileHueSatMapDims
183 Exif.Image.ProfileHueSatMapData1
184 Exif.Image.ProfileHueSatMapData2
185 Exif.Image.ProfileToneCurve
186 Exif.Image.ProfileEmbedPolicy
187 Exif.Image.ProfileCopyright
188 Exif.Image.ForwardMatrix1
189 Exif.Image.ForwardMatrix2
190 Exif.Image.PreviewApplicationName
191 Exif.Image.PreviewApplicationVersion
192 Exif.Image.PreviewSettingsName
193 Exif.Image.PreviewSettingsDigest
194 Exif.Image.PreviewColorSpace
195 Exif.Image.PreviewDateTime
196 Exif.Image.RawImageDigest
197 Exif.Image.OriginalRawFileDigest
198 Exif.Image.SubTileBlockSize
199 Exif.Image.RowInterleaveFactor
200 Exif.Image.ProfileLookTableDims
201 Exif.Image.ProfileLookTableData
202 Exif.Image.OpcodeList1
203 Exif.Image.OpcodeList2
204 Exif.Image.OpcodeList3
205 Exif.Image.NoiseProfile
206 Exif.Photo.ExposureTime
207 Exif.Photo.FNumber
208 Exif.Photo.ExposureProgram
209 Exif.Photo.SpectralSensitivity
210 Exif.Photo.ISOSpeedRatings
211 Exif.Photo.OECF
212 Exif.Photo.SensitivityType
213 Exif.Photo.StandardOutputSensitivity
214 Exif.Photo.RecommendedExposureIndex
215 Exif.Photo.ISOSpeed
216 Exif.Photo.ISOSpeedLatitudeyyy
217 Exif.Photo.ISOSpeedLatitudezzz
218 Exif.Photo.ExifVersion
219 Exif.Photo.DateTimeOriginal
220 Exif.Photo.DateTimeDigitized
221 Exif.Photo.ComponentsConfiguration
222 Exif.Photo.CompressedBitsPerPixel
223 Exif.Photo.ShutterSpeedValue
224 Exif.Photo.ApertureValue
225 Exif.Photo.BrightnessValue
226 Exif.Photo.ExposureBiasValue
227 Exif.Photo.MaxApertureValue
228 Exif.Photo.SubjectDistance
229 Exif.Photo.MeteringMode
230 Exif.Photo.LightSource
231 Exif.Photo.Flash
232 Exif.Photo.FocalLength
233 Exif.Photo.SubjectArea
234 Exif.Photo.MakerNote
235 Exif.Photo.UserComment
236 Exif.Photo.SubSecTime
237 Exif.Photo.SubSecTimeOriginal
238 Exif.Photo.SubSecTimeDigitized
239 Exif.Photo.FlashpixVersion
240 Exif.Photo.ColorSpace
241 Exif.Photo.PixelXDimension
242 Exif.Photo.PixelYDimension
243 Exif.Photo.RelatedSoundFile
244 Exif.Photo.InteroperabilityTag
245 Exif.Photo.FlashEnergy
246 Exif.Photo.SpatialFrequencyResponse
247 Exif.Photo.FocalPlaneXResolution
248 Exif.Photo.FocalPlaneYResolution
249 Exif.Photo.FocalPlaneResolutionUnit
250 Exif.Photo.SubjectLocation
251 Exif.Photo.ExposureIndex
252 Exif.Photo.SensingMethod
253 Exif.Photo.FileSource
254 Exif.Photo.SceneType
255 Exif.Photo.CFAPattern
256 Exif.Photo.CustomRendered
257 Exif.Photo.ExposureMode
258 Exif.Photo.WhiteBalance
259 Exif.Photo.DigitalZoomRatio
260 Exif.Photo.FocalLengthIn35mmFilm
261 Exif.Photo.SceneCaptureType
262 Exif.Photo.GainControl
263 Exif.Photo.Contrast
264 Exif.Photo.Saturation
265 Exif.Photo.Sharpness
266 Exif.Photo.DeviceSettingDescription
267 Exif.Photo.SubjectDistanceRange
268 Exif.Photo.ImageUniqueID
269 Exif.Photo.CameraOwnerName
270 Exif.Photo.BodySerialNumber
271 Exif.Photo.LensSpecification
272 Exif.Photo.LensMake
273 Exif.Photo.LensModel
274 Exif.Photo.LensSerialNumber
275 Exif.Iop.InteroperabilityIndex
276 Exif.Iop.InteroperabilityVersion
277 Exif.Iop.RelatedImageFileFormat
278 Exif.Iop.RelatedImageWidth
279 Exif.Iop.RelatedImageLength
280 Exif.GPSInfo.GPSVersionID
281 Exif.GPSInfo.GPSLatitudeRef
282 Exif.GPSInfo.GPSLatitude
283 Exif.GPSInfo.GPSLongitudeRef
284 Exif.GPSInfo.GPSLongitude
285 Exif.GPSInfo.GPSAltitudeRef
286 Exif.GPSInfo.GPSAltitude
287 Exif.GPSInfo.GPSTimeStamp
288 Exif.GPSInfo.GPSSatellites
289 Exif.GPSInfo.GPSStatus
290 Exif.GPSInfo.GPSMeasureMode
291 Exif.GPSInfo.GPSDOP
292 Exif.GPSInfo.GPSSpeedRef
293 Exif.GPSInfo.GPSSpeed
294 Exif.GPSInfo.GPSTrackRef
295 Exif.GPSInfo.GPSTrack
296 Exif.GPSInfo.GPSImgDirectionRef
297 Exif.GPSInfo.GPSImgDirection
298 Exif.GPSInfo.GPSMapDatum
299 Exif.GPSInfo.GPSDestLatitudeRef
300 Exif.GPSInfo.GPSDestLatitude
301 Exif.GPSInfo.GPSDestLongitudeRef
302 Exif.GPSInfo.GPSDestLongitude
303 Exif.GPSInfo.GPSDestBearingRef
304 Exif.GPSInfo.GPSDestBearing
305 Exif.GPSInfo.GPSDestDistanceRef
306 Exif.GPSInfo.GPSDestDistance
307 Exif.GPSInfo.GPSProcessingMethod
308 Exif.GPSInfo.GPSAreaInformation
309 Exif.GPSInfo.GPSDateStamp
310 Exif.GPSInfo.GPSDifferential

运行结果: