# exifprint.cpp

Sample program to print Exif data from an image.

    00001 // ***************************************************************** -*- C++ -*-
    00002 // exifprint.cpp, $Rev: 631 $
    00003 // Sample program to print the Exif metadata of an image
    00004
    00005 #include "image.hpp"
    00006 #include "exif.hpp"
    00007 #include <iostream>
    00008 #include <iomanip>
    00009 #include <cassert>
    00010
    00011 int main(int argc, char* const argv[])
    00012 try {
    00013
    00014     if (argc != 2) {
    00015         std::cout << "Usage: " << argv[0] << " file\n";
    00016         return 1;
    00017     }
    00018
    00019     Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(argv[1]);
    00020     assert(image.get() != 0);
    00021     image->readMetadata();
    00022
    00023     Exiv2::ExifData &exifData = image->exifData();
    00024     if (exifData.empty()) {
    00025         std::string error(argv[1]);
    00026         error += ": No Exif data found in the file";
    00027         throw Exiv2::Error(1, error);
    00028     }
    00029     Exiv2::ExifData::const_iterator end = exifData.end();
    00030     for (Exiv2::ExifData::const_iterator i = exifData.begin(); i != end; ++i) {
    00031         std::cout << std::setw(44) << std::setfill(' ') << std::left
    00032                   << i->key() << " "
    00033                   << "0x" << std::setw(4) << std::setfill('0') << std::right
    00034                   << std::hex << i->tag() << " "
    00035                   << std::setw(9) << std::setfill(' ') << std::left
    00036                   << i->typeName() << " "
    00037                   << std::dec << std::setw(3)
    00038                   << std::setfill(' ') << std::right
    00039                   << i->count() << "  "
    00040                   << std::dec << i->value()
    00041                   << "\n";
    00042     }
    00043
    00044     return 0;
    00045 }
    00046 catch (Exiv2::AnyError& e) {
    00047     std::cout << "Caught Exiv2 exception '" << e << "'\n";
    00048     return -1;
    00049 }