Sample program to print Exif data from an image.
#include <exiv2/exiv2.hpp>
#include <iostream>
#include <iomanip>
#include <set>
#include <cassert>
#include <regex>
#if defined(EXV_UNICODE_PATH) && defined(__MINGW__)
#undef EXV_UNICODE_PATH
#endif
#ifdef EXV_UNICODE_PATH
#define _tchar wchar_t
#define _tstrcmp wcscmp
#define _t(s) L##s
#define _tcout wcout
#define _tmain wmain
#else
#define _tchar char
#define _tstrcmp strcmp
#define _t(s) s
#define _tcout cout
#define _tmain main
#endif
{
while ( pList->
tag_ != 0xffff && pList->
tag_ != tag ) pList++;
return pList->
tag_ != 0xffff ? pList : NULL;
}
int _tmain(int argc, _tchar* const argv[])
try {
#ifdef EXV_ENABLE_BMFF
Exiv2::enableBMFF();
#endif
const _tchar* prog = argv[0];
if (argc == 1) {
std::_tcout << _t("Usage: ") << prog << _t(" [ [--lint] path | --version | --version-test ]") << std::endl;
return 1;
}
int rc = 0 ;
const _tchar* file = argv[1];
bool bLint = _tstrcmp(file,_t("--lint")) == 0 && argc == 3;
if ( bLint ) file= argv[2];
if ( _tstrcmp(file,_t("--version")) == 0 ) {
std::vector<std::regex> keys;
return rc;
} else if ( _tstrcmp(file,_t("--version-test")) == 0 ) {
std::cout << "EXV_PACKAGE_VERSION " << EXV_PACKAGE_VERSION << std::endl
<<
"strlen(Exiv2::version()) " << ::strlen(
Exiv2::version()) << std::endl
;
#if EXIV2_TEST_VERSION(0,15,0)
std::cout << "Available Exiv2 version is equal to or greater than 0.13\n";
} else {
std::cout << "Installed Exiv2 version is less than 0.13\n";
}
#else
std::cout << "Compile-time Exiv2 version doesn't have Exiv2::testVersion()\n";
#endif
return rc;
}
assert(image.get() != 0);
image->readMetadata();
std::string error("No Exif data found in file");
}
std::set<std::string> shortLong;
shortLong.insert("Exif.Photo.PixelXDimension");
shortLong.insert("Exif.Photo.PixelYDimension");
shortLong.insert("Exif.Photo.ImageLength");
shortLong.insert("Exif.Photo.ImageWidth");
shortLong.insert("Exif.Photo.RowsPerStrip");
shortLong.insert("Exif.Photo.StripOffsets");
shortLong.insert("Exif.Photo.StripByteCounts");
auto end = exifData.
end();
for (
auto i = exifData.
begin(); i != end; ++i) {
if ( ! bLint ) {
const char* tn = i->typeName();
std::cout << std::setw(44) << std::setfill(' ') << std::left
<< i->key() << " "
<< "0x" << std::setw(4) << std::setfill('0') << std::right
<< std::hex << i->tag() << " "
<< std::setw(9) << std::setfill(' ') << std::left
<< (tn ? tn : "Unknown") << " "
<< std::dec << std::setw(3)
<< std::setfill(' ') << std::right
<< i->count() << " "
<< std::dec << i->toString()
<< "\n";
} else {
if ( tagInfo ) {
) {
std::cerr << i->key()
<< " type " << i->typeName() << " (" << type << ")"
<< std::endl;
rc=2;
}
}
}
}
return rc;
}
std::cout <<
"Caught Exiv2 exception '" << e.
what() <<
"'\n";
return -1;
}
Simple error class used for exceptions. An output operator is provided to print errors to a stream.
Definition: error.hpp:264
const char * what() const noexcept override
Return the error message as a C-string. The pointer returned by what() is valid only as long as the B...
Definition: error.hpp:379
A container for Exif data. This is a top-level class of the Exiv2 library. The container holds Exifda...
Definition: exif.hpp:434
bool empty() const
Return true if there is no Exif metadata.
Definition: exif.hpp:512
iterator begin()
Begin of the metadata.
Definition: exif.hpp:490
iterator end()
End of the metadata.
Definition: exif.hpp:492
static Image::UniquePtr open(const std::string &path, bool useCurl=true)
Create an Image subclass of the appropriate type by reading the specified file. Image type is derived...
Definition: image.cpp:894
std::unique_ptr< Image > UniquePtr
Image auto_ptr type.
Definition: image.hpp:81
static const char * typeName(TypeId typeId)
Return the name of the type, 0 if unknown.
Definition: types.cpp:102
static bool initialize(XmpParser::XmpLockFct xmpLockFct=nullptr, void *pLockData=nullptr)
Initialize the XMP Toolkit.
Definition: xmp.cpp:641
static void terminate()
Terminate the XMP Toolkit and unregister custom namespaces.
Definition: xmp.cpp:703
const TagInfo * tagInfo(uint16_t tag, IfdId ifdId)
Return the tag info for tag and ifdId.
Definition: tags_int.cpp:2519
EXIV2API const char * version()
Return the version of Exiv2 as "C" string eg "0.27.0.2".
Definition: version.cpp:105
EXIV2API void dumpLibraryInfo(std::ostream &os, const std::vector< std::regex > &keys)
dumpLibraryInfo implements the exiv2 option –version –verbose used by exiv2 test suite to inspect lib...
Definition: version.cpp:224
EXIV2API std::string versionNumberHexString()
Return the version of Exiv2 as hex string of fixed length 6.
Definition: version.cpp:98
TypeId
Exiv2 value type identifiers.
Definition: types.hpp:100
@ unsignedShort
Exif SHORT type, 16-bit (2-byte) unsigned integer.
Definition: types.hpp:103
@ unsignedLong
Exif LONG type, 32-bit (4-byte) unsigned integer.
Definition: types.hpp:104
@ comment
Exiv2 type for the Exif user comment.
Definition: types.hpp:120
@ undefined
Exif UNDEFINED type, an 8-bit byte that may contain anything.
Definition: types.hpp:107
EXIV2API std::string versionString()
Return the version string Example: "0.25.0" (major.minor.patch)
Definition: version.cpp:90
EXIV2API int versionNumber()
Return the version of Exiv2 available at runtime as an integer.
Definition: version.cpp:85
EXIV2API bool testVersion(int major, int minor, int patch)
Test the version of the available Exiv2 library at runtime. Return true if it is the same as or newer...
Definition: version.cpp:110
BasicError< char > Error
Error class used for exceptions (std::string based)
Definition: error.hpp:324
Tag information.
Definition: tags.hpp:74
uint16_t tag_
Tag.
Definition: tags.hpp:75
TypeId typeId_
Type id.
Definition: tags.hpp:81