- Main Page
- Namespaces
- Classes
- Files
- Examples

# 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: 631 $
    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 <cassert>
    00018
    00019 // *****************************************************************************
    00020 // Main
    00021 int main(int argc, char* const argv[])
    00022 try {
    00023
    00024     if (argc != 2) {
    00025         std::cout << "Usage: " << argv[0] << " file\n";
    00026         return 1;
    00027     }
    00028
    00029     Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(argv[1]);
    00030     assert (image.get() != 0);
    00031     image->readMetadata();
    00032     Exiv2::ExifData &exifData = image->exifData();
    00033
    00034     /*
    00035       Exiv2 uses a CommentValue for Exif user comments. The format of the
    00036       comment string includes an optional charset specification at the beginning:
    00037
    00038       [charset=["]Ascii|Jis|Unicode|Undefined["] ]comment
    00039
    00040       Undefined is used as a default if the comment doesn't start with a charset
    00041       definition.
    00042
    00043       Following are a few examples of valid comments. The last one is written to
    00044       the file.
    00045      */
    00046     exifData["Exif.Photo.UserComment"]
    00047         = "charset=\"Unicode\" An Unicode Exif comment added with Exiv2";
    00048     exifData["Exif.Photo.UserComment"]
    00049         = "charset=\"Undefined\" An undefined Exif comment added with Exiv2";
    00050     exifData["Exif.Photo.UserComment"]
    00051         = "Another undefined Exif comment added with Exiv2";
    00052     exifData["Exif.Photo.UserComment"]
    00053         = "charset=Ascii An ASCII Exif comment added with Exiv2";
    00054
    00055     std::cout << "Writing user comment '"
    00056               << exifData["Exif.Photo.UserComment"]
    00057               << "' back to the image\n";
    00058
    00059     image->writeMetadata();
    00060
    00061     return 0;
    00062 }
    00063 catch (Exiv2::AnyError& e) {
    00064     std::cout << "Caught Exiv2 exception '" << e << "'\n";
    00065     return -1;
    00066 }