• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

Nepomuk-Core

  • sources
  • kde-4.12
  • kdelibs
  • nepomuk-core
  • services
  • fileindexer
  • indexer
exiv2extractor.cpp
Go to the documentation of this file.
1 /*
2  <one line to give the library's name and an idea of what it does.>
3  Copyright (C) 2012 Vishesh Handa <me@vhanda.in>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 
20 
21 #include "exiv2extractor.h"
22 
23 #include "nfo.h"
24 #include "nexif.h"
25 #include "nie.h"
26 
27 #include <KDebug>
28 
29 #include <exiv2/exiv2.hpp>
30 
31 using namespace Nepomuk2::Vocabulary;
32 
33 namespace Nepomuk2 {
34 
35 Exiv2Extractor::Exiv2Extractor(QObject* parent, const QVariantList&)
36 : ExtractorPlugin(parent)
37 {
38 
39 }
40 
41 QStringList Exiv2Extractor::mimetypes()
42 {
43  QStringList types;
44 
45  types << QLatin1String("image/jp2")
46  << QLatin1String("image/jpeg")
47  << QLatin1String("image/pgf")
48  << QLatin1String("image/png")
49  << QLatin1String("image/tiff")
50  << QLatin1String("image/x-exv")
51  << QLatin1String("image/x-canon-cr2")
52  << QLatin1String("image/x-canon-crw")
53  << QLatin1String("image/x-fuji-raf")
54  << QLatin1String("image/x-minolta-mrw")
55  << QLatin1String("image/x-nikon-nef")
56  << QLatin1String("image/x-olympus-orf")
57  << QLatin1String("image/x-panasonic-rw2")
58  << QLatin1String("image/x-pentax-pef")
59  << QLatin1String("image/x-photoshop")
60  << QLatin1String("image/x-samsung-srw");
61 
62  return types;
63 }
64 
65 namespace {
66  QString toString(const Exiv2::Value& value) {
67  std::string str = value.toString();
68  return QString::fromUtf8( str.c_str(), str.length() );
69  }
70 
71  QVariant toVariantDateTime(const Exiv2::Value& value) {
72  if( value.typeId() == Exiv2::asciiString ) {
73  QDateTime val = ExtractorPlugin::dateTimeFromString( value.toString().c_str() );
74  if( val.isValid() ) {
75  // Datetime is stored in exif as local time.
76  val.setUtcOffset(0);
77  return QVariant( val );
78  }
79  }
80 
81  return QVariant();
82  }
83 
84  QVariant toVariantLong(const Exiv2::Value& value) {
85  if( value.typeId() == Exiv2::unsignedLong || value.typeId() == Exiv2::signedLong ) {
86  qlonglong val = value.toLong();
87  return QVariant( val );
88  }
89 
90  QString str( toString(value) );
91  bool ok = false;
92  int val = str.toInt(&ok);
93  if( ok )
94  return QVariant( val );
95 
96  return QVariant();
97  }
98 
99  QVariant toVariantFloat(const Exiv2::Value& value) {
100  // WARNING: Dbus does not recognize float, cast to double
101  if( value.typeId() == Exiv2::tiffFloat || value.typeId() == Exiv2::tiffDouble || value.typeId() == Exiv2::unsignedRational || value.typeId() == Exiv2::signedRational )
102  return QVariant( static_cast<double>(value.toFloat()) );
103 
104  QString str( toString(value) );
105  bool ok = false;
106  double val = str.toFloat(&ok);
107  if( ok )
108  return QVariant( val );
109 
110  return QVariant();
111  }
112 
113  QVariant toVariantString(const Exiv2::Value& value) {
114  QString str = toString(value);
115  if( !str.isEmpty() )
116  return QVariant( str );
117 
118  return QVariant();
119  }
120 }
121 
122 SimpleResourceGraph Exiv2Extractor::extract(const QUrl& resUri, const QUrl& fileUrl, const QString& mimeType)
123 {
124  Q_UNUSED( mimeType );
125 
126  QByteArray arr = fileUrl.toLocalFile().toUtf8();
127  std::string fileString( arr.data(), arr.length() );
128 
129  Exiv2::Image::AutoPtr image;
130  try {
131  image = Exiv2::ImageFactory::open( fileString );
132  }
133  catch (const std::exception&) {
134  return SimpleResourceGraph();
135  }
136  if( !image.get() ) {
137  return SimpleResourceGraph();
138  }
139 
140  try {
141  image->readMetadata();
142  }
143  catch (const std::exception&) {
144  return SimpleResourceGraph();
145  }
146  const Exiv2::ExifData &data = image->exifData();
147 
148  SimpleResourceGraph graph;
149  SimpleResource fileRes( resUri );
150  fileRes.addType( NFO::RasterImage() );
151 
152  if( image->pixelHeight() ) {
153  fileRes.setProperty( NFO::height(), image->pixelHeight() );
154  }
155 
156  if( image->pixelWidth() ) {
157  fileRes.setProperty( NFO::width(), image->pixelWidth() );
158  }
159 
160  std::string comment = image->comment();
161  if( !comment.empty() ) {
162  fileRes.setProperty( NIE::comment(), QString::fromUtf8( comment.c_str(), comment.length() ) );
163  }
164 
165  /*
166  Exiv2::ExifData::const_iterator end = data.end();
167  Exiv2::ExifData::const_iterator i = data.begin();
168  for( ; i != end; i++ ) {
169  kDebug() << i->key().c_str();
170  kDebug() << i->value().toString().c_str();
171  }*/
172 
173  Exiv2::ExifData::const_iterator it;
174 
175  it = data.findKey( Exiv2::ExifKey("Exif.Photo.Flash") );
176  if( it != data.end() ) {
177  QVariant value = toVariantLong( it->value() );
178  if( !value.isNull() )
179  fileRes.setProperty( NEXIF::flash(), value );
180  }
181 
182  // The width and height have already been set above, this is not required
183  /*it = data.findKey( Exiv2::ExifKey("Exif.Photo.PixelXDimension") );
184  if( it != data.end() ) {
185  QVariant value = toVariantLong( it->value() );
186  if( !value.isNull() )
187  fileRes.setProperty( NFO::width(), value );
188  }
189 
190  it = data.findKey( Exiv2::ExifKey("Exif.Photo.PixelYDimension") );
191  if( it != data.end() ) {
192  QVariant value = toVariantLong( it->value() );
193  if( !value.isNull() )
194  fileRes.setProperty( NFO::height(), value );
195  }*/
196 
197  it = data.findKey( Exiv2::ExifKey("Exif.Image.Make") );
198  if( it != data.end() ) {
199  QVariant value = toVariantString( it->value() );
200  if( !value.isNull() )
201  fileRes.setProperty( NEXIF::make(), value );
202  }
203 
204  it = data.findKey( Exiv2::ExifKey("Exif.Image.Model") );
205  if( it != data.end() ) {
206  QVariant value = toVariantString( it->value() );
207  if( !value.isNull() )
208  fileRes.setProperty( NEXIF::model(), value );
209  }
210 
211  it = data.findKey( Exiv2::ExifKey("Exif.Image.DateTime") );
212  if( it != data.end() ) {
213  QVariant value = toVariantDateTime( it->value() );
214  if( !value.isNull() )
215  fileRes.setProperty( NIE::contentCreated(), value );
216  }
217 
218  it = data.findKey( Exiv2::ExifKey("Exif.Image.Orientation") );
219  if( it != data.end() ) {
220  QVariant value = toVariantLong( it->value() );
221  if( !value.isNull() )
222  fileRes.setProperty( NEXIF::orientation(), value );
223  }
224 
225  it = data.findKey( Exiv2::ExifKey("Exif.Photo.DateTimeOriginal") );
226  if( it != data.end() ) {
227  QVariant value = toVariantDateTime( it->value() );
228  if( !value.isNull() )
229  fileRes.setProperty( NEXIF::dateTimeOriginal(), value );
230  }
231 
232  it = data.findKey( Exiv2::ExifKey("Exif.Photo.FocalLength") );
233  if( it != data.end() ) {
234  QVariant value = toVariantFloat( it->value() );
235  if( !value.isNull() )
236  fileRes.setProperty( NEXIF::focalLength(), value );
237  }
238 
239  it = data.findKey( Exiv2::ExifKey("Exif.Photo.FocalLengthIn35mmFilm") );
240  if( it != data.end() ) {
241  QVariant value = toVariantFloat( it->value() );
242  if( !value.isNull() )
243  fileRes.setProperty( NEXIF::focalLengthIn35mmFilm(), value );
244  }
245 
246  it = data.findKey( Exiv2::ExifKey("Exif.Photo.ExposureTime") );
247  if( it != data.end() ) {
248  QVariant value = toVariantFloat( it->value() );
249  if( !value.isNull() )
250  fileRes.setProperty( NEXIF::exposureTime(), value );
251  }
252 
253  it = data.findKey( Exiv2::ExifKey("Exif.Photo.FNumber") );
254  if( it != data.end() ) {
255  QVariant value = toVariantFloat( it->value() );
256  if( !value.isNull() )
257  fileRes.setProperty( NEXIF::fNumber(), value );
258  }
259 
260  it = data.findKey( Exiv2::ExifKey("Exif.Photo.ApertureValue") );
261  if( it != data.end() ) {
262  QVariant value = toVariantFloat( it->value() );
263  if( !value.isNull() )
264  fileRes.setProperty( NEXIF::apertureValue(), value );
265  }
266 
267  it = data.findKey( Exiv2::ExifKey("Exif.Photo.ExposureBiasValue") );
268  if( it != data.end() ) {
269  QVariant value = toVariantFloat( it->value() );
270  if( !value.isNull() )
271  fileRes.setProperty( NEXIF::exposureBiasValue(), value );
272  }
273 
274  it = data.findKey( Exiv2::ExifKey("Exif.Photo.WhiteBalance") );
275  if( it != data.end() ) {
276  QVariant value = toVariantLong( it->value() );
277  if( !value.isNull() )
278  fileRes.setProperty( NEXIF::whiteBalance(), value );
279  }
280 
281  it = data.findKey( Exiv2::ExifKey("Exif.Photo.MeteringMode") );
282  if( it != data.end() ) {
283  QVariant value = toVariantLong( it->value() );
284  if( !value.isNull() )
285  fileRes.setProperty( NEXIF::meteringMode(), value );
286  }
287 
288  it = data.findKey( Exiv2::ExifKey("Exif.Photo.ISOSpeedRatings") );
289  if( it != data.end() ) {
290  QVariant value = toVariantLong( it->value() );
291  if( !value.isNull() )
292  fileRes.setProperty( NEXIF::isoSpeedRatings(), value );
293  }
294 
295  it = data.findKey( Exiv2::ExifKey("Exif.Photo.Saturation") );
296  if( it != data.end() ) {
297  QVariant value = toVariantLong( it->value() );
298  if( !value.isNull() )
299  fileRes.setProperty( NEXIF::saturation(), value );
300  }
301 
302  it = data.findKey( Exiv2::ExifKey("Exif.Photo.Sharpness") );
303  if( it != data.end() ) {
304  QVariant value = toVariantLong( it->value() );
305  if( !value.isNull() )
306  fileRes.setProperty( NEXIF::sharpness(), value );
307  }
308 
309  fileRes.addType( NEXIF::Photo() );
310 
311  graph << fileRes;
312  return graph;
313 }
314 
315 }
316 
317 NEPOMUK_EXPORT_EXTRACTOR( Nepomuk2::Exiv2Extractor, "nepomukexivextractor" )
Nepomuk2::SimpleResource::setProperty
void setProperty(const QUrl &property, const QVariant &value)
Set a property overwriting existing values.
Definition: simpleresource.cpp:186
Nepomuk2::ExtractorPlugin
The ExtractorPlugin is the base class for all file metadata extractors.
Definition: extractorplugin.h:60
Nepomuk2::SimpleResource
Represents a snapshot of one Nepomuk resource.
Definition: simpleresource.h:46
exiv2extractor.h
QObject
Nepomuk2::SimpleResourceGraph
Definition: simpleresourcegraph.h:48
Nepomuk2::Exiv2Extractor::Exiv2Extractor
Exiv2Extractor(QObject *parent, const QVariantList &)
Definition: exiv2extractor.cpp:35
NEPOMUK_EXPORT_EXTRACTOR
#define NEPOMUK_EXPORT_EXTRACTOR(classname, libname)
Export a Nepomuk file extractor.
Definition: extractorplugin.h:163
Nepomuk2::Exiv2Extractor
Definition: exiv2extractor.h:28
Nepomuk2::ExtractorPlugin::dateTimeFromString
static QDateTime dateTimeFromString(const QString &dateString)
Tries to extract a valid date time from the string provided.
Definition: extractorplugin.cpp:59
Nepomuk2::SimpleResource::addType
void addType(const QUrl &type)
A convenience method which adds a property of type rdf:type.
Definition: simpleresource.cpp:257
Nepomuk2::Exiv2Extractor::extract
virtual SimpleResourceGraph extract(const QUrl &resUri, const QUrl &fileUrl, const QString &mimeType)
The main function of the plugin that is responsible for extracting the data from the file url and ret...
Definition: exiv2extractor.cpp:122
Nepomuk2::Exiv2Extractor::mimetypes
virtual QStringList mimetypes()
Provide a list of mimetypes which are supported by this plugin.
Definition: exiv2extractor.cpp:41
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:08 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Nepomuk-Core

Skip menu "Nepomuk-Core"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal