exifcomment.cpp

Sample program showing how to set the Exif comment of an image.

00001 // ***************************************************************** -*- C++ -*-
00002 /*
00003   Abstract : Sample program showing how to set the Exif comment of an image,
00004              Exif.Photo.UserComment
00005 
00006   File:      exifcomment.cpp
00007   Version  : $Rev: 560 $
00008   Author(s): Andreas Huggel (ahu) <ahuggel@gmx.net>
00009   History  : 10-May-04, ahu: created
00010              16-Jan-05, ahu: updated using CommentValue and operator trickery
00011  */
00012 // *****************************************************************************
00013 // included header files
00014 #include "image.hpp"
00015 #include "exif.hpp"
00016 #include <iostream>
00017 #include <iomanip>
00018 #include <cstring>
00019 #include <cassert>
00020 
00021 // *****************************************************************************
00022 // Main
00023 int main(int argc, char* const argv[])
00024 try {
00025 
00026     if (argc != 2) {
00027         std::cout << "Usage: " << argv[0] << " file\n";
00028         return 1;
00029     }
00030 
00031     Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(argv[1]);
00032     assert (image.get() != 0);
00033     image->readMetadata();
00034     Exiv2::ExifData &exifData = image->exifData();
00035 
00036     /*
00037       Exiv2 uses a CommentValue for Exif user comments. The format of the 
00038       comment string includes an optional charset specification at the beginning:
00039 
00040       [charset=["]Ascii|Jis|Unicode|Undefined["] ]comment
00041 
00042       Undefined is used as a default if the comment doesn't start with a charset
00043       definition.
00044 
00045       Following are a few examples of valid comments. The last one is written to
00046       the file.
00047      */
00048     exifData["Exif.Photo.UserComment"] 
00049         = "charset=\"Unicode\" An Unicode Exif comment added with Exiv2";
00050     exifData["Exif.Photo.UserComment"] 
00051         = "charset=\"Undefined\" An undefined Exif comment added with Exiv2";
00052     exifData["Exif.Photo.UserComment"] 
00053         = "Another undefined Exif comment added with Exiv2";
00054     exifData["Exif.Photo.UserComment"] 
00055         = "charset=Ascii An ASCII Exif comment added with Exiv2";
00056 
00057     std::cout << "Writing user comment '"
00058               << exifData["Exif.Photo.UserComment"]
00059               << "' back to the image\n";
00060 
00061     image->writeMetadata();
00062 
00063     return 0;
00064 }
00065 catch (Exiv2::AnyError& e) {
00066     std::cout << "Caught Exiv2 exception '" << e << "'\n";
00067     return -1;
00068 }

Generated on Sun Feb 26 04:28:27 2006 for Exiv2 by  doxygen 1.4.6