Project

General

Profile

RE: xmp-data created in a jpeg using exiv2 cannot be read... ยป xmpsample.cpp

"my version" of xmpsample.cpp - Silke Werner, 18 Aug 2011 02:43

 
1
// ***************************************************************** -*- C++ -*-
2
// xmpsample.cpp, $Rev: 2286 $
3
// Sample/test for high level XMP classes. See also addmoddel.cpp
4

    
5
#include <exiv2/exiv2.hpp>
6

    
7
#include <string>
8
#include <iostream>
9
#include <iomanip>
10
#include <cassert>
11
#include <cmath>
12

    
13
bool isEqual(float a, float b)
14
{
15
    double d = std::fabs(a - b);
16
    return d < 0.00001;
17
}
18

    
19
int main(int argc, char* const argv[])
20
try 
21
{
22
  if (argc != 2) 
23
  {
24
    std::cout << "Usage: " << argv[0] << " file\n";
25
    return 1;
26
  }
27

    
28
  std::string testFile = argv[1];
29

    
30
  //Open File and write some XMP
31
  Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(testFile);
32
  assert(image.get() != 0);
33

    
34
  Exiv2::XmpData xmpDataW;
35
  Exiv2::XmpProperties::registerNs("R&S/", "RS");
36
  xmpDataW["Xmp.RS.SerialNumber"] = "12345678";
37
  xmpDataW["Xmp.RS.SoftwareVersion"] = "9.9.9.9";
38
  
39
  image->setXmpData(xmpDataW);
40
  image->writeMetadata();
41
  image.release();
42

    
43
  Exiv2::Image::AutoPtr imageOpenSameFileAgain = Exiv2::ImageFactory::open(testFile);
44
  assert(imageOpenSameFileAgain.get() != 0);
45
  imageOpenSameFileAgain->readMetadata();
46

    
47
  Exiv2::XmpData &xmpDataR = imageOpenSameFileAgain->xmpData();
48
  if (xmpDataR.empty()) 
49
  {
50
    std::string error(argv[1]);
51
    error += ": No XMP data found in the file";
52
    throw Exiv2::Error(1, error);
53
  }
54
  else
55
  {
56
    Exiv2::XmpData::const_iterator pos = xmpDataR.findKey(Exiv2::XmpKey("Xmp.RS.SoftwareVersion"));
57
    if (pos == xmpDataR.end()) 
58
    {
59
      std::string error(argv[1]);
60
      error += ": SoftwareVersion not found in XMP data of the file";
61
      throw Exiv2::Error(1, error);
62
    }
63
    
64
    Exiv2::Value::AutoPtr v = pos->getValue();
65
    assert(v->ok());
66
    std::string sXMPValue = v->toString();
67
    std::cout << sXMPValue << "\n";
68
  }
69

    
70
  return 0;
71
}
72
catch (Exiv2::AnyError& e) 
73
{
74
    std::cout << "Caught Exiv2 exception '" << e << "'\n";
75
    return -1;
76
}
    (1-1/1)