29 #include <exiv2/exiv2.hpp>
31 using namespace Nepomuk2::Vocabulary;
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");
66 QString toString(
const Exiv2::Value& value) {
67 std::string str = value.toString();
68 return QString::fromUtf8( str.c_str(), str.length() );
71 QVariant toVariantDateTime(
const Exiv2::Value& value) {
72 if( value.typeId() == Exiv2::asciiString ) {
77 return QVariant( val );
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 );
90 QString str( toString(value) );
92 int val = str.toInt(&ok);
94 return QVariant( val );
99 QVariant toVariantFloat(
const Exiv2::Value& value) {
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()) );
104 QString str( toString(value) );
106 double val = str.toFloat(&ok);
108 return QVariant( val );
113 QVariant toVariantString(
const Exiv2::Value& value) {
114 QString str = toString(value);
116 return QVariant( str );
124 Q_UNUSED( mimeType );
126 QByteArray arr = fileUrl.toLocalFile().toUtf8();
127 std::string fileString( arr.data(), arr.length() );
129 Exiv2::Image::AutoPtr image;
131 image = Exiv2::ImageFactory::open( fileString );
133 catch (
const std::exception&) {
141 image->readMetadata();
143 catch (
const std::exception&) {
146 const Exiv2::ExifData &data = image->exifData();
150 fileRes.
addType( NFO::RasterImage() );
152 if( image->pixelHeight() ) {
153 fileRes.
setProperty( NFO::height(), image->pixelHeight() );
156 if( image->pixelWidth() ) {
157 fileRes.
setProperty( NFO::width(), image->pixelWidth() );
160 std::string comment = image->comment();
161 if( !comment.empty() ) {
162 fileRes.
setProperty( NIE::comment(), QString::fromUtf8( comment.c_str(), comment.length() ) );
173 Exiv2::ExifData::const_iterator it;
175 it = data.findKey( Exiv2::ExifKey(
"Exif.Photo.Flash") );
176 if( it != data.end() ) {
177 QVariant value = toVariantLong( it->value() );
178 if( !value.isNull() )
197 it = data.findKey( Exiv2::ExifKey(
"Exif.Image.Make") );
198 if( it != data.end() ) {
199 QVariant value = toVariantString( it->value() );
200 if( !value.isNull() )
204 it = data.findKey( Exiv2::ExifKey(
"Exif.Image.Model") );
205 if( it != data.end() ) {
206 QVariant value = toVariantString( it->value() );
207 if( !value.isNull() )
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 );
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 );
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 );
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 );
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 );
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 );
253 it = data.findKey( Exiv2::ExifKey(
"Exif.Photo.FNumber") );
254 if( it != data.end() ) {
255 QVariant value = toVariantFloat( it->value() );
256 if( !value.isNull() )
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 );
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 );
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 );
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 );
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 );
295 it = data.findKey( Exiv2::ExifKey(
"Exif.Photo.Saturation") );
296 if( it != data.end() ) {
297 QVariant value = toVariantLong( it->value() );
298 if( !value.isNull() )
302 it = data.findKey( Exiv2::ExifKey(
"Exif.Photo.Sharpness") );
303 if( it != data.end() ) {
304 QVariant value = toVariantLong( it->value() );
305 if( !value.isNull() )
309 fileRes.
addType( NEXIF::Photo() );
void setProperty(const QUrl &property, const QVariant &value)
Set a property overwriting existing values.
Represents a snapshot of one Nepomuk resource.
void addType(const QUrl &type)
A convenience method which adds a property of type rdf:type.