<div class="fragment"><div class="line"><span class="comment">// ***************************************************************** -*- C++ -*-</span></div>
<div class="line"><span class="comment">// exifprint.cpp, $Rev: 3090 $</span></div>
<div class="line"><span class="comment">// Sample program to print the Exif metadata of an image</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"></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"></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="a0"></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>(argv[1]);</div>
<div class="line">    assert(image.get() != 0);</div>
<div class="line">    image-&gt;readMetadata();</div>
<div class="line"></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> &amp;exifData = image-&gt;exifData();</div>
<div class="line">    <span class="keywordflow">if</span> (exifData.<a name="a2"></a><a class="code" href="doc/classExiv2_1_1ExifData.html#a4993c68fbb50731014c307852875c731" title="Return true if there is no Exif metadata.">empty</a>()) {</div>
<div class="line">        std::string error(argv[1]);</div>
<div class="line">        error += <span class="stringliteral">&quot;: No Exif data found in the file&quot;</span>;</div>
<div class="line">        <span class="keywordflow">throw</span> <a name="a4"></a><a class="code" href="doc/namespaceExiv2.html#accd3e49cafe9db52c1e0e6f648753cae" title="Error class used for exceptions (std::string based)">Exiv2::Error</a>(1, error);</div>
<div class="line">    }</div>
<div class="line">    <a class="code" href="doc/classExiv2_1_1ExifData.html#a2b8ac7a474d6527c0f3f6a0a9cebef77" title="ExifMetadata const iterator type.">Exiv2::ExifData::const_iterator</a> end = exifData.<a name="a5"></a><a class="code" href="doc/classExiv2_1_1ExifData.html#a9c15177b03489e3d4bb81e9acc1165fe" title="End of the metadata.">end</a>();</div>
<div class="line">    <span class="keywordflow">for</span> (<a class="code" href="doc/classExiv2_1_1ExifData.html#a2b8ac7a474d6527c0f3f6a0a9cebef77" title="ExifMetadata const iterator type.">Exiv2::ExifData::const_iterator</a> i = exifData.<a name="a6"></a><a class="code" href="doc/classExiv2_1_1ExifData.html#a53bce2980ee060fc2da5fe6751f51db9" title="Begin of the metadata.">begin</a>(); i != end; ++i) {</div>
<div class="line">        <span class="keyword">const</span> <span class="keywordtype">char</span>* tn = i-&gt;typeName();</div>
<div class="line">        std::cout &lt;&lt; std::setw(44) &lt;&lt; std::setfill(<span class="charliteral">&#39; &#39;</span>) &lt;&lt; std::left</div>
<div class="line">                  &lt;&lt; i-&gt;key() &lt;&lt; <span class="stringliteral">&quot; &quot;</span></div>
<div class="line">                  &lt;&lt; <span class="stringliteral">&quot;0x&quot;</span> &lt;&lt; std::setw(4) &lt;&lt; std::setfill(<span class="charliteral">&#39;0&#39;</span>) &lt;&lt; std::right</div>
<div class="line">                  &lt;&lt; std::hex &lt;&lt; i-&gt;tag() &lt;&lt; <span class="stringliteral">&quot; &quot;</span></div>
<div class="line">                  &lt;&lt; std::setw(9) &lt;&lt; std::setfill(<span class="charliteral">&#39; &#39;</span>) &lt;&lt; std::left</div>
<div class="line">                  &lt;&lt; (tn ? tn : <span class="stringliteral">&quot;Unknown&quot;</span>) &lt;&lt; <span class="stringliteral">&quot; &quot;</span></div>
<div class="line">                  &lt;&lt; std::dec &lt;&lt; std::setw(3)</div>
<div class="line">                  &lt;&lt; std::setfill(<span class="charliteral">&#39; &#39;</span>) &lt;&lt; std::right</div>
<div class="line">                  &lt;&lt; i-&gt;count() &lt;&lt; <span class="stringliteral">&quot;  &quot;</span></div>
<div class="line">                  &lt;&lt; std::dec &lt;&lt; i-&gt;value()</div>
<div class="line">                  &lt;&lt; <span class="stringliteral">&quot;\n&quot;</span>;</div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    <span class="keywordflow">return</span> 0;</div>
<div class="line">}</div>
<div class="line"><span class="comment">//catch (std::exception&amp; e) {</span></div>
<div class="line"><span class="comment">//catch (Exiv2::AnyError&amp; e) {</span></div>
<div class="line"><span class="keywordflow">catch</span> (<a name="_a7"></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>&amp; e) {</div>
<div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;Caught Exiv2 exception &#39;&quot;</span> &lt;&lt; e.<a name="a8"></a><a class="code" href="doc/classExiv2_1_1BasicError.html#a72e9f29e45d6f59125fa3de232641504" title="Return the error message as a C-string. The pointer returned by what() is valid only as long as the B...">what</a>() &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 -->
