Sample usage of high-level XMP classes.
#include <exiv2/exiv2.hpp>
#include "unused.h"
#include <string>
#include <iostream>
#include <iomanip>
#include <cassert>
#include <cmath>
bool isEqual(float a, float b)
{
double d = std::fabs(a - b);
return d < 0.00001;
}
int main()
try {
#ifdef EXV_ENABLE_BMFF
Exiv2::enableBMFF();
#endif
xmpData["Xmp.dc.source"] = "xmpsample.cpp";
xmpData["Xmp.dc.subject"] = "Palmtree";
xmpData["Xmp.dc.subject"] = "Rubbertree";
xmpData["Xmp.dc.title"] = "lang=de-DE Sonnenuntergang am Strand";
xmpData["Xmp.dc.title"] = "lang=en-US Sunset on the beach";
xmpData["Xmp.dc.one"] = -1;
xmpData["Xmp.dc.two"] = 3.1415;
xmpData["Xmp.dc.four"] = uint16_t(255);
xmpData["Xmp.dc.five"] = 256;
xmpData["Xmp.dc.six"] = false;
xmpData["Xmp.dc.seven"] = val;
xmpData["Xmp.dc.eight"] = true;
assert(xmpData["Xmp.dc.one"].toLong() == -1);
assert(xmpData["Xmp.dc.one"].value().ok());
UNUSED(getv1);
assert(isEqual(getv1.
toFloat(), -1));
UNUSED(getv2);
assert(isEqual(getv2.
toFloat(), 3.1415f));
UNUSED(R);
assert(isEqual(static_cast<float>(R.first) / R.second, 3.1415f ));
const Exiv2::Value &getv3 = xmpData[
"Xmp.dc.three"].value();
UNUSED(getv3);
assert(isEqual(getv3.
toFloat(), 5.0f/7.0f));
UNUSED(getv6);
const Exiv2::Value &getv7 = xmpData[
"Xmp.dc.seven"].value();
const Exiv2::Value &getv8 = xmpData[
"Xmp.dc.eight"].value();
UNUSED(getv8);
if (pos == xmpData.
end())
throw Exiv2::Error(Exiv2::kerErrorMessage,
"Key not found");
v->read("image/jpeg");
v->read("1) The first creator");
v->read("2) The second creator");
v->read("3) And another one");
v->read("lang=de-DE Hallo, Welt");
v->read("Hello, World");
xmpData["Xmp.tiff.ImageDescription"] = "TIFF image description";
xmpData["Xmp.tiff.ImageDescription"] = "lang=de-DE TIFF Bildbeschreibung";
xmpData["Xmp.ns.myProperty"] = "myValue";
tv.read("9");
tv.read("inch");
xmpData["Xmp.dc.publisher"] = "James Bond";
xmpData["Xmp.dc.publisher[1]/?ns:role"] = "secret agent";
tv.read("programmer");
tv.read("");
tv.setXmpArrayType(Exiv2::XmpValue::xaBag);
tv.setXmpArrayType(Exiv2::XmpValue::xaNone);
tv.read("Birthday party");
tv.read("Photographer");
tv.read("Wedding ceremony");
tv.read("Best man");
xmpData["Xmp.iptc.CreatorContactInfo/Iptc4xmpCore:CiAdrCity"] = "Kuala Lumpur";
xmpData["Xmp.iptc.CreatorContactInfo/Iptc4xmpCore:CiAdrCtry"] = "Malaysia";
xmpData["Xmp.iptc.CreatorContactInfo/Iptc4xmpCore:CiUrlWork"] = "http://www.exiv2.org";
md != xmpData.
end(); ++md) {
std::cout << std::setfill(' ') << std::left
<< std::setw(44)
<< md->key() << " "
<< std::setw(9) << std::setfill(' ') << std::left
<< md->typeName() << " "
<< std::dec << std::setw(3)
<< std::setfill(' ') << std::right
<< std::dec << md->value()
<< std::endl;
}
std::string xmpPacket;
throw Exiv2::Error(Exiv2::kerErrorMessage,
"Failed to serialize XMP data");
}
std::cout << xmpPacket << "\n";
return 0;
}
std::cout << "Caught Exiv2 exception '" << e << "'\n";
return -1;
}
Error class interface. Allows the definition and use of a hierarchy of error classes which can all be...
Definition: error.hpp:174
Common interface for all types of values used with metadata.
Definition: value.hpp:51
std::auto_ptr< Value > AutoPtr
Shortcut for a Value auto pointer.
Definition: value.hpp:54
virtual float toFloat(long n=0) const =0
Convert the n-th component of the value to a float. The behaviour of this method may be undefined if ...
virtual long toLong(long n=0) const =0
Convert the n-th component of the value to a long. The behaviour of this method may be undefined if t...
bool ok() const
Check the ok status indicator. After a to<Type> conversion, this indicator shows whether the conversi...
Definition: value.hpp:188
virtual Rational toRational(long n=0) const =0
Convert the n-th component of the value to a Rational. The behaviour of this method may be undefined ...
static AutoPtr create(TypeId typeId)
A (simple) factory to create a Value type.
Definition: value.cpp:93
A container for XMP data. This is a top-level class of the Exiv2 library.
Definition: xmp_exiv2.hpp:166
iterator end()
End of the metadata.
Definition: xmp.cpp:543
long count() const
Get the number of metadata entries.
Definition: xmp.cpp:533
XmpMetadata::iterator iterator
XmpMetadata iterator type.
Definition: xmp_exiv2.hpp:172
int add(const XmpKey &key, const Value *value)
Add an Xmpdatum from the supplied key and value pair. This method copies (clones) the value.
Definition: xmp.cpp:485
iterator findKey(const XmpKey &key)
Find the first Xmpdatum with the given key, return an iterator to it.
Definition: xmp.cpp:502
XmpMetadata::const_iterator const_iterator
XmpMetadata const iterator type.
Definition: xmp_exiv2.hpp:174
iterator begin()
Begin of the metadata.
Definition: xmp.cpp:538
Concrete keys for XMP metadata.
Definition: properties.hpp:231
static bool initialize(XmpParser::XmpLockFct xmpLockFct=0, void *pLockData=0)
Initialize the XMP Toolkit.
Definition: xmp.cpp:641
static int encode(std::string &xmpPacket, const XmpData &xmpData, uint16_t formatFlags=useCompactFormat, uint32_t padding=0)
Encode (serialize) XMP metadata from xmpData into a string xmpPacket. The XMP packet returned in the ...
Definition: xmp.cpp:1004
static void terminate()
Terminate the XMP Toolkit and unregister custom namespaces.
Definition: xmp.cpp:703
static void registerNs(const std::string &ns, const std::string &prefix)
Register namespace ns with preferred prefix prefix.
Value type suitable for simple XMP properties and XMP nodes of complex types which are not parsed int...
Definition: value.hpp:715
@ langAlt
XMP language alternative type.
Definition: types.hpp:145
@ xmpText
XMP text type.
Definition: types.hpp:141
@ xmpSeq
XMP sequence type.
Definition: types.hpp:144
std::pair< int32_t, int32_t > Rational
8 byte signed rational type.
Definition: types.hpp:99
BasicError< char > Error
Error class used for exceptions (std::string based)
Definition: error.hpp:324