<div class="fragment"><div class="line"><span class="comment">// ***************************************************************** -*- C++ -*-</span></div>
<div class="line"><span class="comment">// addmoddel.cpp, $Rev: 3353 $</span></div>
<div class="line"><span class="comment">// Sample program showing how to add, modify and delete Exif metadata.</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#include &lt;<a class="code" href="doc/exiv2_8hpp.html" title="Include all Exiv2 header files.">exiv2/exiv2.hpp</a>&gt;</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;iomanip&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;cassert&gt;</span></div>
<div class="line"></div>
<div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>* <span class="keyword">const</span> argv[])</div>
<div class="line"><span class="keyword">try</span> {</div>
<div class="line">    <span class="keywordflow">if</span> (argc != 2) {</div>
<div class="line">        std::cout &lt;&lt; <span class="stringliteral">&quot;Usage: &quot;</span> &lt;&lt; argv[0] &lt;&lt; <span class="stringliteral">&quot; file\n&quot;</span>;</div>
<div class="line">        <span class="keywordflow">return</span> 1;</div>
<div class="line">    }</div>
<div class="line">    std::string file(argv[1]);</div>
<div class="line"></div>
<div class="line">    <span class="comment">// Container for exif metadata. This is an example of creating</span></div>
<div class="line">    <span class="comment">// exif metadata from scratch. If you want to add, modify, delete</span></div>
<div class="line">    <span class="comment">// metadata that exists in an image, start with ImageFactory::open</span></div>
<div class="line">    <a name="_a1"></a><a class="code" href="doc/classExiv2_1_1ExifData.html" title="A container for Exif data. This is a top-level class of the Exiv2 library. The container holds Exifda...">Exiv2::ExifData</a> exifData;</div>
<div class="line"></div>
<div class="line">    <span class="comment">// *************************************************************************</span></div>
<div class="line">    <span class="comment">// Add to the Exif data</span></div>
<div class="line"></div>
<div class="line">    <span class="comment">// This is the quickest way to add (simple) Exif data. If a metadatum for</span></div>
<div class="line">    <span class="comment">// a given key already exists, its value is overwritten. Otherwise a new</span></div>
<div class="line">    <span class="comment">// tag is added.</span></div>
<div class="line">    exifData[<span class="stringliteral">&quot;Exif.Image.Model&quot;</span>] = <span class="stringliteral">&quot;Test 1&quot;</span>;                     <span class="comment">// AsciiValue</span></div>
<div class="line">    exifData[<span class="stringliteral">&quot;Exif.Image.SamplesPerPixel&quot;</span>] = uint16_t(162);      <span class="comment">// UShortValue</span></div>
<div class="line">    exifData[<span class="stringliteral">&quot;Exif.Image.XResolution&quot;</span>] = int32_t(-2);            <span class="comment">// LongValue</span></div>
<div class="line">    exifData[<span class="stringliteral">&quot;Exif.Image.YResolution&quot;</span>] = <a name="a2"></a><a class="code" href="doc/namespaceExiv2.html#a95756f3f7fa19103f83addf5fa088a30" title="8 byte signed rational type.">Exiv2::Rational</a>(-2, 3); <span class="comment">// RationalValue</span></div>
<div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;Added a few tags the quick way.\n&quot;</span>;</div>
<div class="line"></div>
<div class="line">    <span class="comment">// Create a ASCII string value (note the use of create)</span></div>
<div class="line">    <a class="code" href="doc/classExiv2_1_1Value.html#a0f62e585b82c97738858b743e60dff21" title="Shortcut for a Value auto pointer.">Exiv2::Value::AutoPtr</a> v = <a name="a3"></a><a class="code" href="doc/classExiv2_1_1Value.html#ad6ff043921cd1a5c399a9a4fc8257006" title="A (simple) factory to create a Value type.">Exiv2::Value::create</a>(<a name="a4"></a><a class="code" href="doc/namespaceExiv2.html#a5153319711f35fe81cbc13f4b852450ca773cf6dde5caaabb3dcf9fb161fa7dfd" title="Exif ASCII type, 8-bit byte.">Exiv2::asciiString</a>);</div>
<div class="line">    <span class="comment">// Set the value to a string</span></div>
<div class="line">    v-&gt;read(<span class="stringliteral">&quot;1999:12:31 23:59:59&quot;</span>);</div>
<div class="line">    <span class="comment">// Add the value together with its key to the Exif data container</span></div>
<div class="line">    <a name="_a5"></a><a class="code" href="doc/classExiv2_1_1ExifKey.html" title="Concrete keys for Exif metadata and access to Exif tag reference data.">Exiv2::ExifKey</a> key(<span class="stringliteral">&quot;Exif.Photo.DateTimeOriginal&quot;</span>);</div>
<div class="line">    exifData.<a name="a6"></a><a class="code" href="doc/classExiv2_1_1ExifData.html#a91d231cd1b9fefc311c5166e30ab66eb" title="Add an Exifdatum from the supplied key and value pair. This method copies (clones) key and value...">add</a>(key, v.get());</div>
<div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;Added key \&quot;&quot;</span> &lt;&lt; key &lt;&lt; <span class="stringliteral">&quot;\&quot;, value \&quot;&quot;</span> &lt;&lt; *v &lt;&lt; <span class="stringliteral">&quot;\&quot;\n&quot;</span>;</div>
<div class="line"></div>
<div class="line">    <span class="comment">// Now create a more interesting value (without using the create method)</span></div>
<div class="line">    <a class="code" href="doc/classExiv2_1_1ValueType.html#a0c76c512468a47f6eac463f4af278a14" title="Shortcut for a ValueType&lt;T&gt; auto pointer.">Exiv2::URationalValue::AutoPtr</a> rv(<span class="keyword">new</span> <a name="_a7"></a><a class="code" href="doc/classExiv2_1_1ValueType.html" title="Template for a Value of a basic type. This is used for unsigned and signed short, long and rationals...">Exiv2::URationalValue</a>);</div>
<div class="line">    <span class="comment">// Set two rational components from a string</span></div>
<div class="line">    rv-&gt;read(<span class="stringliteral">&quot;1/2 1/3&quot;</span>);</div>
<div class="line">    <span class="comment">// Add more elements through the extended interface of rational value</span></div>
<div class="line">    rv-&gt;value_.push_back(std::make_pair(2,3));</div>
<div class="line">    rv-&gt;value_.push_back(std::make_pair(3,4));</div>
<div class="line">    <span class="comment">// Add the key and value pair to the Exif data</span></div>
<div class="line">    key = <a class="code" href="doc/classExiv2_1_1ExifKey.html" title="Concrete keys for Exif metadata and access to Exif tag reference data.">Exiv2::ExifKey</a>(<span class="stringliteral">&quot;Exif.Image.PrimaryChromaticities&quot;</span>);</div>
<div class="line">    exifData.<a class="code" href="doc/classExiv2_1_1ExifData.html#a91d231cd1b9fefc311c5166e30ab66eb" title="Add an Exifdatum from the supplied key and value pair. This method copies (clones) key and value...">add</a>(key, rv.get());</div>
<div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;Added key \&quot;&quot;</span> &lt;&lt; key &lt;&lt; <span class="stringliteral">&quot;\&quot;, value \&quot;&quot;</span> &lt;&lt; *rv &lt;&lt; <span class="stringliteral">&quot;\&quot;\n&quot;</span>;</div>
<div class="line"></div>
<div class="line">    <span class="comment">// *************************************************************************</span></div>
<div class="line">    <span class="comment">// Modify Exif data</span></div>
<div class="line"></div>
<div class="line">    <span class="comment">// Since we know that the metadatum exists (or we don&#39;t mind creating a new</span></div>
<div class="line">    <span class="comment">// tag if it doesn&#39;t), we can simply do this:</span></div>
<div class="line">    <a name="_a8"></a><a class="code" href="doc/classExiv2_1_1Exifdatum.html" title="An Exif metadatum, consisting of an ExifKey and a Value and methods to manipulate these...">Exiv2::Exifdatum</a>&amp; tag = exifData[<span class="stringliteral">&quot;Exif.Photo.DateTimeOriginal&quot;</span>];</div>
<div class="line">    std::string date = tag.<a name="a10"></a><a class="code" href="doc/classExiv2_1_1Exifdatum.html#a73d1e5346411c2adf520fec405f2e536" title="Return the value as a string.">toString</a>();</div>
<div class="line">    date.replace(0, 4, <span class="stringliteral">&quot;2000&quot;</span>);</div>
<div class="line">    tag.<a name="a11"></a>setValue(date);</div>
<div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;Modified key \&quot;&quot;</span> &lt;&lt; tag.<a name="a12"></a><a class="code" href="doc/classExiv2_1_1Exifdatum.html#a6651602de3d217dd622d33ab67289c11" title="Return the key of the Exifdatum.">key</a>()</div>
<div class="line">              &lt;&lt; <span class="stringliteral">&quot;\&quot;, new value \&quot;&quot;</span> &lt;&lt; tag.<a name="a13"></a><a class="code" href="doc/classExiv2_1_1Exifdatum.html#ad4a621c1399e02648f1fb1fb550e7a53" title="Return a constant reference to the value.">value</a>() &lt;&lt; <span class="stringliteral">&quot;\&quot;\n&quot;</span>;</div>
<div class="line"></div>
<div class="line">    <span class="comment">// Alternatively, we can use findKey()</span></div>
<div class="line">    key = <a class="code" href="doc/classExiv2_1_1ExifKey.html" title="Concrete keys for Exif metadata and access to Exif tag reference data.">Exiv2::ExifKey</a>(<span class="stringliteral">&quot;Exif.Image.PrimaryChromaticities&quot;</span>);</div>
<div class="line">    <a class="code" href="doc/classExiv2_1_1ExifData.html#a02e2a2acb4cfeb0f7755c1a45f94106f" title="ExifMetadata iterator type.">Exiv2::ExifData::iterator</a> pos = exifData.<a name="a14"></a><a class="code" href="doc/classExiv2_1_1ExifData.html#a96c38cbd300ebdfa05f849864b380690" title="Find the first Exifdatum with the given key, return an iterator to it.">findKey</a>(key);</div>
<div class="line">    <span class="keywordflow">if</span> (pos == exifData.<a name="a15"></a><a class="code" href="doc/classExiv2_1_1ExifData.html#a9c15177b03489e3d4bb81e9acc1165fe" title="End of the metadata.">end</a>()) <span class="keywordflow">throw</span> <a name="_a16"></a><a class="code" href="doc/classExiv2_1_1BasicError.html" title="Simple error class used for exceptions. An output operator is provided to print errors to a stream...">Exiv2::Error</a>(1, <span class="stringliteral">&quot;Key not found&quot;</span>);</div>
<div class="line">    <span class="comment">// Get a pointer to a copy of the value</span></div>
<div class="line">    v = pos-&gt;getValue();</div>
<div class="line">    <span class="comment">// Downcast the Value pointer to its actual type</span></div>
<div class="line">    <a class="code" href="doc/classExiv2_1_1ValueType.html" title="Template for a Value of a basic type. This is used for unsigned and signed short, long and rationals...">Exiv2::URationalValue</a>* prv = <span class="keyword">dynamic_cast&lt;</span><a class="code" href="doc/classExiv2_1_1ValueType.html" title="Template for a Value of a basic type. This is used for unsigned and signed short, long and rationals...">Exiv2::URationalValue</a>*<span class="keyword">&gt;</span>(v.release());</div>
<div class="line">    <span class="keywordflow">if</span> (prv == 0) <span class="keywordflow">throw</span> <a name="a17"></a><a class="code" href="doc/namespaceExiv2.html#accd3e49cafe9db52c1e0e6f648753cae" title="Error class used for exceptions (std::string based)">Exiv2::Error</a>(1, <span class="stringliteral">&quot;Downcast failed&quot;</span>);</div>
<div class="line">    rv = <a name="a18"></a><a class="code" href="doc/classExiv2_1_1ValueType.html#a0c76c512468a47f6eac463f4af278a14" title="Shortcut for a ValueType&lt;T&gt; auto pointer.">Exiv2::URationalValue::AutoPtr</a>(prv);</div>
<div class="line">    <span class="comment">// Modify the value directly through the interface of URationalValue</span></div>
<div class="line">    rv-&gt;value_[2] = std::make_pair(88,77);</div>
<div class="line">    <span class="comment">// Copy the modified value back to the metadatum</span></div>
<div class="line">    pos-&gt;setValue(rv.get());</div>
<div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;Modified key \&quot;&quot;</span> &lt;&lt; key</div>
<div class="line">              &lt;&lt; <span class="stringliteral">&quot;\&quot;, new value \&quot;&quot;</span> &lt;&lt; pos-&gt;value() &lt;&lt; <span class="stringliteral">&quot;\&quot;\n&quot;</span>;</div>
<div class="line"></div>
<div class="line">    <span class="comment">// *************************************************************************</span></div>
<div class="line">    <span class="comment">// Delete metadata from the Exif data container</span></div>
<div class="line"></div>
<div class="line">    <span class="comment">// Delete the metadatum at iterator position pos</span></div>
<div class="line">    key = <a class="code" href="doc/classExiv2_1_1ExifKey.html" title="Concrete keys for Exif metadata and access to Exif tag reference data.">Exiv2::ExifKey</a>(<span class="stringliteral">&quot;Exif.Image.PrimaryChromaticities&quot;</span>);</div>
<div class="line">    pos = exifData.<a class="code" href="doc/classExiv2_1_1ExifData.html#a96c38cbd300ebdfa05f849864b380690" title="Find the first Exifdatum with the given key, return an iterator to it.">findKey</a>(key);</div>
<div class="line">    <span class="keywordflow">if</span> (pos == exifData.<a class="code" href="doc/classExiv2_1_1ExifData.html#a9c15177b03489e3d4bb81e9acc1165fe" title="End of the metadata.">end</a>()) <span class="keywordflow">throw</span> <a class="code" href="doc/classExiv2_1_1BasicError.html" title="Simple error class used for exceptions. An output operator is provided to print errors to a stream...">Exiv2::Error</a>(1, <span class="stringliteral">&quot;Key not found&quot;</span>);</div>
<div class="line">    exifData.<a name="a19"></a><a class="code" href="doc/classExiv2_1_1ExifData.html#a710a66ca8be51192c15729c541b72fb5" title="Delete the Exifdatum at iterator position pos, return the position of the next exifdatum. Note that iterators into the metadata, including pos, are potentially invalidated by this call.">erase</a>(pos);</div>
<div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;Deleted key \&quot;&quot;</span> &lt;&lt; key &lt;&lt; <span class="stringliteral">&quot;\&quot;\n&quot;</span>;</div>
<div class="line"></div>
<div class="line">    <span class="comment">// *************************************************************************</span></div>
<div class="line">    <span class="comment">// Finally, write the remaining Exif data to the image file</span></div>
<div class="line">    <a class="code" href="doc/classExiv2_1_1Image.html#a89ad3ffe7a4e8a943d267d77843415fb" title="Image auto_ptr type.">Exiv2::Image::AutoPtr</a> image = <a name="a20"></a><a class="code" href="doc/classExiv2_1_1ImageFactory.html#aba929c4ca4a71625d12bcb97bcc28161" title="Create an Image subclass of the appropriate type by reading the specified file. Image type is derived...">Exiv2::ImageFactory::open</a>(file);</div>
<div class="line">    assert(image.get() != 0);</div>
<div class="line"></div>
<div class="line">    image-&gt;setExifData(exifData);</div>
<div class="line">    image-&gt;writeMetadata();</div>
<div class="line"></div>
<div class="line">    <span class="keywordflow">return</span> 0;</div>
<div class="line">}</div>
<div class="line"><span class="keywordflow">catch</span> (<a name="_a21"></a><a class="code" href="doc/classExiv2_1_1AnyError.html" title="Error class interface. Allows the definition and use of a hierarchy of error classes which can all be...">Exiv2::AnyError</a>&amp; e) {</div>
<div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;Caught Exiv2 exception &#39;&quot;</span> &lt;&lt; e &lt;&lt; <span class="stringliteral">&quot;&#39;\n&quot;</span>;</div>
<div class="line">    <span class="keywordflow">return</span> -1;</div>
<div class="line">}</div>
</div><!-- fragment -->
