MauiKit Image Tools

exiv2extractor.cpp
1// SPDX-License-Identifier: LGPL-3.0-or-later
2
3/*
4 * Copyright (C) 2012-15 Vishesh Handa <vhanda@kde.org>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include "exiv2extractor.h"
22
23#include <KExiv2/KExiv2>
24
25#include <QGeoAddress>
26#include <QFileInfo>
27#include <QDateTime>
28#include <QDebug>
29#include <QFile>
30
31// #include <QTextCodec>
32
33#include "geolocation/cities.h"
34#include "geolocation/city.h"
35
36Exiv2Extractor::Exiv2Extractor(const QUrl &url, QObject *parent) : QObject(parent)
37 , m_error(true)
38 , m_exiv2(new KExiv2Iface::KExiv2()) //remeember to delete it
39{
40 // if(!KExiv2Iface::KExiv2::initializeExiv2())
41 // qWarning() << "failed to initialized exiv2 library";
42 this->setUrl(url);
43}
44
45Exiv2Extractor::Exiv2Extractor(QObject *parent) : QObject(parent)
46 , m_error(true)
47 , m_exiv2(new KExiv2Iface::KExiv2()) //remeember to delete it
48
49{
50}
51
52Exiv2Extractor::~Exiv2Extractor()
53{
54 // clearData();
55 delete m_exiv2;
56}
57
58void Exiv2Extractor::setUrl(const QUrl &url)
59{
60 qDebug() << "Start parsing image file url for metadata";
61 // clearData();
62 m_url = url;
63 if (!QFileInfo::exists(m_url.toLocalFile()) || m_url.isEmpty() || !m_url.isValid()) {
64 qDebug() << "Image file is not valid or does not exists.";
65 return;
66 }
67
68 m_error = !m_exiv2->load(m_url.toLocalFile());
69 if(m_error)
70 qWarning() << "Failed to load Exiv2 metadata";
71}
72
73Coordinates Exiv2Extractor::extractGPS() const
74{
75 if(error())
76 return{};
77
78 if(!m_exiv2->initializeGPSInfo(true))
79 {
80 qWarning() << "failed to initialized GPS data";
81 return {};
82 }
83
84 double latitude = 0;
85 double longitude = 0;
86 double altitude = 0;
87
88 m_exiv2->getGPSLatitudeNumber(&latitude);
89 m_exiv2->getGPSLongitudeNumber(&longitude);
90 m_exiv2->getGPSAltitude(&altitude);
91
92 return {altitude, latitude, longitude};
93}
94
95
96bool Exiv2Extractor::error() const
97{
98 return m_error;
99}
100
101QString Exiv2Extractor::getExifTagString(const char* exifTagName, bool escapeCR) const
102{
103 if(error())
104 return {};
105
106 return m_exiv2->getExifTagString(exifTagName, escapeCR);
107}
108
109QByteArray Exiv2Extractor::getExifTagData(const char* exifTagName) const
110{
111 if(error())
112 return {};
113
114 return m_exiv2->getExifTagData(exifTagName);
115}
116
117QVariant Exiv2Extractor::getExifTagVariant(const char* exifTagName, bool rationalAsListOfInts, bool stringEscapeCR, int component) const
118{
119 if(error())
120 return {};
121 return m_exiv2->getExifTagVariant(exifTagName, rationalAsListOfInts, stringEscapeCR, component);
122}
123
124MetaDataMap Exiv2Extractor::getExifTagsDataList(const QStringList& exifKeysFilter, bool invertSelection) const
125{
126 if(error())
127 return {};
128 return m_exiv2->getExifTagsDataList(exifKeysFilter, invertSelection);
129}
130
131QString Exiv2Extractor::getExifComment() const
132{
133 if(error())
134 return {};
135 return m_exiv2->getExifComment();
136}
137
138QString Exiv2Extractor::GPSString() const
139{
140 if(error())
141 return {};
142
143 City m_city(city());
144
145 if(!m_city.isValid())
146 {
147 return QString();
148 }
149
150 return m_city.name();
151}
152
153QString Exiv2Extractor::cityId() const
154{
155 if(error())
156 return {};
157
158 return city().id();
159}
160
161City Exiv2Extractor::city() const
162{
163 if(error())
164 return {};
165
166 auto c = extractGPS();
167
168 if(c.latitude == 0.0 || c.longitude == 0.0)
169 {
170 return City();
171 }
172
173 return Cities::getInstance()->findCity(c.latitude, c.longitude);
174}
175
176QSize Exiv2Extractor::getPixelSize()
177{
178 if(error())
179 return {};
180
181 return m_exiv2->getPixelSize();
182}
183
184bool Exiv2Extractor::applyChanges()
185{
186 if(error())
187 return false;
188 return m_exiv2->applyChanges();
189}
190
191bool Exiv2Extractor::setGpsData(const double latitude, const double longitude, const double altitude)
192{
193 if(error())
194 return false;
195 qDebug() << "Setting gps data as:" << latitude << longitude << altitude;
196 return m_exiv2->setGPSInfo(altitude,latitude, longitude);
197
198 // return m_exiv2->setGPSInfo(0.0, 6.224958, -75.573983);
199}
200
201bool Exiv2Extractor::removeGpsData()
202{
203 if(error())
204 return false;
205 return m_exiv2->removeGPSInfo();
206}
207
208bool Exiv2Extractor::clearData()
209{
210 if(error())
211 return false;
212
213 if(m_exiv2->isEmpty())
214 return false;
215
217 ok = m_exiv2->clearExif ( );
218 return ok;
219}
220
221bool Exiv2Extractor::writeTag(const char *tagName, const QVariant &value)
222{
223 if(error())
224 return false;
225 return m_exiv2->setExifTagVariant(tagName, value);
226}
227
228bool Exiv2Extractor::removeTag(const char *tagName)
229{
230 if(error())
231 return false;
232 return m_exiv2->removeExifTag(tagName);
233}
234
235bool Exiv2Extractor::setComment(const QString &comment)
236{
237 if(error())
238 return false;
239 return m_exiv2->setExifComment(comment);
240}
241
242QString Exiv2Extractor::getComments() const
243{
244 if(error())
245 return {};
246 return m_exiv2->getCommentsDecoded();
247}
248
249bool Exiv2Extractor::removeComment() const
250{
251 if(error())
252 return false;
253 auto ok = m_exiv2->removeExifTag("Exif.Image.ImageDescription");
254 ok = m_exiv2->removeExifTag("Exif.Photo.UserComment");
255 return ok;
256}
A class for representing the GPS coordinates and information of a city.
static bool cleanupExiv2()
bool exists() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Apr 11 2025 11:57:09 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.