1
// ***************************************************************** -*- C++ -*-
2
// addmoddel.cpp, $Rev: 1271 $
3
// Sample program showing how to add, modify and delete Exif metadata.
5
#include <exiv2/image.hpp>
6
#include <exiv2/exif.hpp>
11
int main(int argc, char* const argv[])
14
std::cout << "Usage: " << argv[0] << " file\n";
17
std::string file(argv[1]);
19
// Container for exif metadata. This is an example of creating
20
// exif metadata from scratch. If you want to add, modify, delete
21
// metadata that exists in an image, start with ImageFactory::open
22
Exiv2::ExifData exifData;
24
// *************************************************************************
25
// Add to the Exif data
27
// This is the quickest way to add (simple) Exif data. If a metadatum for
28
// a given key already exists, its value is overwritten. Otherwise a new
30
exifData["Exif.Image.Model"] = "Test 1"; // AsciiValue
31
exifData["Exif.Image.SamplesPerPixel"] = uint16_t(162); // UShortValue
32
exifData["Exif.Image.XResolution"] = int32_t(-2); // LongValue
33
exifData["Exif.Image.YResolution"] = Exiv2::Rational(-2, 3); // RationalValue
34
std::cout << "Added a few tags the quick way.\n";
36
// Create a ASCII string value (note the use of create)
37
Exiv2::Value::AutoPtr v = Exiv2::Value::create(Exiv2::asciiString);
38
// Set the value to a string
39
v->read("1999:12:31 23:59:59");
40
// Add the value together with its key to the Exif data container
41
Exiv2::ExifKey key("Exif.Photo.DateTimeOriginal");
42
exifData.add(key, v.get());
43
std::cout << "Added key \"" << key << "\", value \"" << *v << "\"\n";
45
// Now create a more interesting value (without using the create method)
46
Exiv2::URationalValue::AutoPtr rv(new Exiv2::URationalValue);
47
// Set two rational components from a string
49
// Add more elements through the extended interface of rational value
50
rv->value_.push_back(std::make_pair(2,3));
51
rv->value_.push_back(std::make_pair(3,4));
52
// Add the key and value pair to the Exif data
53
key = Exiv2::ExifKey("Exif.Image.PrimaryChromaticities");
54
exifData.add(key, rv.get());
55
std::cout << "Added key \"" << key << "\", value \"" << *rv << "\"\n";
57
// *************************************************************************
60
// Since we know that the metadatum exists (or we don't mind creating a new
61
// tag if it doesn't), we can simply do this:
62
Exiv2::Exifdatum& tag = exifData["Exif.Photo.DateTimeOriginal"];
63
std::string date = tag.toString();
64
date.replace(0, 4, "2000");
66
std::cout << "Modified key \"" << key
67
<< "\", new value \"" << tag.value() << "\"\n";
69
// Alternatively, we can use findKey()
70
key = Exiv2::ExifKey("Exif.Image.PrimaryChromaticities");
71
Exiv2::ExifData::iterator pos = exifData.findKey(key);
72
if (pos == exifData.end()) throw Exiv2::Error(1, "Key not found");
73
// Get a pointer to a copy of the value
75
// Downcast the Value pointer to its actual type
76
Exiv2::URationalValue* prv = dynamic_cast<Exiv2::URationalValue*>(v.release());
77
if (prv == 0) throw Exiv2::Error(1, "Downcast failed");
78
rv = Exiv2::URationalValue::AutoPtr(prv);
79
// Modify the value directly through the interface of URationalValue
80
rv->value_[2] = std::make_pair(88,77);
81
// Copy the modified value back to the metadatum
82
pos->setValue(rv.get());
83
std::cout << "Modified key \"" << key
84
<< "\", new value \"" << pos->value() << "\"\n";
86
// *************************************************************************
87
// Delete metadata from the Exif data container
89
// Delete the metadatum at iterator position pos
90
key = Exiv2::ExifKey("Exif.Image.PrimaryChromaticities");
91
pos = exifData.findKey(key);
92
if (pos == exifData.end()) throw Exiv2::Error(1, "Key not found");
94
std::cout << "Deleted key \"" << key << "\"\n";
96
// *************************************************************************
97
// Finally, write the remaining Exif data to the image file
98
Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(file);
99
assert(image.get() != 0);
101
image->setExifData(exifData);
102
image->writeMetadata();
106
catch (Exiv2::AnyError& e) {
107
std::cout << "Caught Exiv2 exception '" << e << "'\n";