Blame samples/addmoddel.cpp

Packit 01d647
// ***************************************************************** -*- C++ -*-
Packit 01d647
// addmoddel.cpp
Packit 01d647
// Sample program showing how to add, modify and delete Exif metadata.
Packit 01d647
Packit 01d647
#include <exiv2/exiv2.hpp>
Packit 01d647
Packit 01d647
#include <iostream>
Packit 01d647
#include <iomanip>
Packit 01d647
#include <cassert>
Packit 01d647
Packit 01d647
int main(int argc, char* const argv[])
Packit 01d647
try {
Packit 01d647
    if (argc != 2) {
Packit 01d647
        std::cout << "Usage: " << argv[0] << " file\n";
Packit 01d647
        return 1;
Packit 01d647
    }
Packit 01d647
    std::string file(argv[1]);
Packit 01d647
Packit 01d647
    // Container for exif metadata. This is an example of creating
Packit 01d647
    // exif metadata from scratch. If you want to add, modify, delete
Packit 01d647
    // metadata that exists in an image, start with ImageFactory::open
Packit 01d647
    Exiv2::ExifData exifData;
Packit 01d647
Packit 01d647
    // *************************************************************************
Packit 01d647
    // Add to the Exif data
Packit 01d647
Packit 01d647
    // This is the quickest way to add (simple) Exif data. If a metadatum for
Packit 01d647
    // a given key already exists, its value is overwritten. Otherwise a new
Packit 01d647
    // tag is added.
Packit 01d647
    exifData["Exif.Image.Model"] = "Test 1";                     // AsciiValue
Packit 01d647
    exifData["Exif.Image.SamplesPerPixel"] = uint16_t(162);      // UShortValue
Packit 01d647
    exifData["Exif.Image.XResolution"] = -2;            // LongValue
Packit 01d647
    exifData["Exif.Image.YResolution"] = Exiv2::Rational(-2, 3); // RationalValue
Packit 01d647
    std::cout << "Added a few tags the quick way.\n";
Packit 01d647
Packit 01d647
    // Create a ASCII string value (note the use of create)
Packit 01d647
    Exiv2::Value::AutoPtr v = Exiv2::Value::create(Exiv2::asciiString);
Packit 01d647
    // Set the value to a string
Packit 01d647
    v->read("1999:12:31 23:59:59");
Packit 01d647
    // Add the value together with its key to the Exif data container
Packit 01d647
    Exiv2::ExifKey key("Exif.Photo.DateTimeOriginal");
Packit 01d647
    exifData.add(key, v.get());
Packit 01d647
    std::cout << "Added key \"" << key << "\", value \"" << *v << "\"\n";
Packit 01d647
Packit 01d647
    // Now create a more interesting value (without using the create method)
Packit 01d647
    Exiv2::URationalValue::AutoPtr rv(new Exiv2::URationalValue);
Packit 01d647
    // Set two rational components from a string
Packit 01d647
    rv->read("1/2 1/3");
Packit 01d647
    // Add more elements through the extended interface of rational value
Packit 01d647
    rv->value_.push_back(std::make_pair(2,3));
Packit 01d647
    rv->value_.push_back(std::make_pair(3,4));
Packit 01d647
    // Add the key and value pair to the Exif data
Packit 01d647
    key = Exiv2::ExifKey("Exif.Image.PrimaryChromaticities");
Packit 01d647
    exifData.add(key, rv.get());
Packit 01d647
    std::cout << "Added key \"" << key << "\", value \"" << *rv << "\"\n";
Packit 01d647
Packit 01d647
    // *************************************************************************
Packit 01d647
    // Modify Exif data
Packit 01d647
Packit 01d647
    // Since we know that the metadatum exists (or we don't mind creating a new
Packit 01d647
    // tag if it doesn't), we can simply do this:
Packit 01d647
    Exiv2::Exifdatum& tag = exifData["Exif.Photo.DateTimeOriginal"];
Packit 01d647
    std::string date = tag.toString();
Packit 01d647
    date.replace(0, 4, "2000");
Packit 01d647
    tag.setValue(date);
Packit 01d647
    std::cout << "Modified key \"" << tag.key()
Packit 01d647
              << "\", new value \"" << tag.value() << "\"\n";
Packit 01d647
Packit 01d647
    // Alternatively, we can use findKey()
Packit 01d647
    key = Exiv2::ExifKey("Exif.Image.PrimaryChromaticities");
Packit 01d647
    Exiv2::ExifData::iterator pos = exifData.findKey(key);
Packit 01d647
    if (pos == exifData.end()) throw Exiv2::Error(Exiv2::kerErrorMessage, "Key not found");
Packit 01d647
    // Get a pointer to a copy of the value
Packit 01d647
    v = pos->getValue();
Packit 01d647
    // Downcast the Value pointer to its actual type
Packit 01d647
    Exiv2::URationalValue* prv = dynamic_cast<Exiv2::URationalValue*>(v.release());
Packit 01d647
    if (prv == 0) throw Exiv2::Error(Exiv2::kerErrorMessage, "Downcast failed");
Packit 01d647
    rv = Exiv2::URationalValue::AutoPtr(prv);
Packit 01d647
    // Modify the value directly through the interface of URationalValue
Packit 01d647
    rv->value_[2] = std::make_pair(88,77);
Packit 01d647
    // Copy the modified value back to the metadatum
Packit 01d647
    pos->setValue(rv.get());
Packit 01d647
    std::cout << "Modified key \"" << key
Packit 01d647
              << "\", new value \"" << pos->value() << "\"\n";
Packit 01d647
Packit 01d647
    // *************************************************************************
Packit 01d647
    // Delete metadata from the Exif data container
Packit 01d647
Packit 01d647
    // Delete the metadatum at iterator position pos
Packit 01d647
    key = Exiv2::ExifKey("Exif.Image.PrimaryChromaticities");
Packit 01d647
    pos = exifData.findKey(key);
Packit 01d647
    if (pos == exifData.end()) throw Exiv2::Error(Exiv2::kerErrorMessage, "Key not found");
Packit 01d647
    exifData.erase(pos);
Packit 01d647
    std::cout << "Deleted key \"" << key << "\"\n";
Packit 01d647
Packit 01d647
    // *************************************************************************
Packit 01d647
    // Finally, write the remaining Exif data to the image file
Packit 01d647
    Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(file);
Packit 01d647
    assert(image.get() != 0);
Packit 01d647
Packit 01d647
    image->setExifData(exifData);
Packit 01d647
    image->writeMetadata();
Packit 01d647
Packit 01d647
    return 0;
Packit 01d647
}
Packit 01d647
catch (Exiv2::AnyError& e) {
Packit 01d647
    std::cout << "Caught Exiv2 exception '" << e << "'\n";
Packit 01d647
    return -1;
Packit 01d647
}