Loading...
Searching...
No Matches
exifprint.cpp

Sample program to print Exif data from an image.

// ***************************************************************** -*- C++ -*-
/*
* Copyright (C) 2004-2021 Exiv2 authors
* This program is part of the Exiv2 distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
*/
#include <exiv2/exiv2.hpp>
#include <iostream>
#include <iomanip>
#include <cassert>
// https://github.com/Exiv2/exiv2/issues/468
#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
int _tmain(int argc, _tchar* const argv[])
try {
Exiv2::XmpParser::initialize();
::atexit(Exiv2::XmpParser::terminate);
#ifdef EXV_ENABLE_BMFF
Exiv2::enableBMFF();
#endif
const _tchar* prog = argv[0];
const _tchar* file = argv[1];
if (argc != 2) {
std::_tcout << _t("Usage: ") << prog << _t(" [ path | --version | --version-test ]") << std::endl;
return 1;
}
if ( _tstrcmp(file,_t("--version")) == 0 ) {
exv_grep_keys_t keys;
Exiv2::dumpLibraryInfo(std::cout,keys);
return 0;
} else if ( _tstrcmp(file,_t("--version-test")) == 0 ) {
// verifies/test macro EXIV2_TEST_VERSION
// described in include/exiv2/version.hpp
std::cout << "EXV_PACKAGE_VERSION " << EXV_PACKAGE_VERSION << std::endl
<< "Exiv2::version() " << Exiv2::version() << std::endl
<< "strlen(Exiv2::version()) " << ::strlen(Exiv2::version()) << std::endl
<< "Exiv2::versionNumber() " << Exiv2::versionNumber() << std::endl
<< "Exiv2::versionString() " << Exiv2::versionString() << std::endl
<< "Exiv2::versionNumberHexString() " << Exiv2::versionNumberHexString() << std::endl
;
// Test the Exiv2 version available at runtime but compile the if-clause only if
// the compile-time version is at least 0.15. Earlier versions didn't have a
// testVersion() function:
#if EXIV2_TEST_VERSION(0,15,0)
if (Exiv2::testVersion(0,13,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 0;
}
Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(file);
assert(image.get() != 0);
image->readMetadata();
Exiv2::ExifData &exifData = image->exifData();
if (exifData.empty()) {
std::string error("No Exif data found in file");
throw Exiv2::Error(Exiv2::kerErrorMessage, error);
}
Exiv2::ExifData::const_iterator end = exifData.end();
for (Exiv2::ExifData::const_iterator i = exifData.begin(); i != end; ++i) {
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";
}
return 0;
}
//catch (std::exception& e) {
//catch (Exiv2::AnyError& e) {
catch (Exiv2::Error& e) {
std::cout << "Caught Exiv2 exception '" << e.what() << "'\n";
return -1;
}
Exiv2::BasicError
Simple error class used for exceptions. An output operator is provided to print errors to a stream.
Definition error.hpp:264
Exiv2::BasicError::what
virtual const char * what() const
Return the error message as a C-string. The pointer returned by what() is valid only as long as the B...
Definition error.hpp:381
Exiv2::ExifData
A container for Exif data. This is a top-level class of the Exiv2 library. The container holds Exifda...
Definition exif.hpp:434
Exiv2::ExifData::empty
bool empty() const
Return true if there is no Exif metadata.
Definition exif.hpp:512
Exiv2::ExifData::const_iterator
ExifMetadata::const_iterator const_iterator
ExifMetadata const iterator type.
Definition exif.hpp:439
Exiv2::ExifData::begin
iterator begin()
Begin of the metadata.
Definition exif.hpp:490
Exiv2::ExifData::end
iterator end()
End of the metadata.
Definition exif.hpp:492
Exiv2::ImageFactory::open
static Image::AutoPtr 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:924
Exiv2::Image::AutoPtr
std::auto_ptr< Image > AutoPtr
Image auto_ptr type.
Definition image.hpp:81
Exiv2::XmpParser::initialize
static bool initialize(XmpParser::XmpLockFct xmpLockFct=0, void *pLockData=0)
Initialize the XMP Toolkit.
Definition xmp.cpp:641
Exiv2::XmpParser::terminate
static void terminate()
Terminate the XMP Toolkit and unregister custom namespaces.
Definition xmp.cpp:703
Exiv2::version
EXIV2API const char * version()
Return the version of Exiv2 as "C" string eg "0.27.0.2".
Definition version.cpp:103
Exiv2::versionNumberHexString
EXIV2API std::string versionNumberHexString()
Return the version of Exiv2 as hex string of fixed length 6.
Definition version.cpp:96
Exiv2::versionString
EXIV2API std::string versionString()
Return the version string Example: "0.25.0" (major.minor.patch)
Definition version.cpp:89
Exiv2::dumpLibraryInfo
EXIV2API void dumpLibraryInfo(std::ostream &os, const exv_grep_keys_t &keys)
dumpLibraryInfo implements the exiv2 option –version –verbose used by exiv2 test suite to inspect lib...
Definition version.cpp:235
Exiv2::versionNumber
EXIV2API int versionNumber()
Return the version of Exiv2 available at runtime as an integer.
Definition version.cpp:84
Exiv2::testVersion
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:108
Exiv2::Error
BasicError< char > Error
Error class used for exceptions (std::string based)
Definition error.hpp:324