-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
imagedata.cpp
169 lines (145 loc) · 4.66 KB
/
imagedata.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/** @file imagedata.cpp
* Soubor s tridou ImageData dedici ze tridy QObject obsahujici data nactena z fotografie
*/
#include "imagedata.h"
#include <QDebug>
ImageData::ImageData(QObject* parent)
: QObject(parent)
{
isGps = 0;
gpsSource = 1;
isDateTimeSaved = 1;
isGpsSaved = 1;
latitude = 1000;
longitude = 1001;
altitude = -1000;
direction = qQNaN();
angleOfView = qQNaN();
objLatitude = 1000;
objLongitude = 1001;
dateTime = new QDateTime;
*dateTime = QDateTime::fromString(QString("0000:00:00 00:00:00"), "yyyy:MM:dd hh:mm:ss");
watcher = new QFileSystemWatcher(this);
reloadTimer = new QTimer(this);
reloadTimer->setSingleShot(true);
reloadTimer->setInterval(RELOAD_TIMER_INTERVAL);
reloadTimer->setTimerType(Qt::CoarseTimer);
image_small = new QImage();
isDateTimeChanged = 0;
scaleSize = QSize(288, 212); // maximalni zobrazitelne velikost obrazku
exifRW = new ExifReaderWriter;
connect(exifRW, SIGNAL(setGps(double, double, double, double, double)),
this, SLOT(setGps(double, double, double, double, double)));
connect(exifRW, SIGNAL(setObjGps(double, double)),
this, SLOT(setObjGps(double, double)));
connect(exifRW, SIGNAL(setDateTime(QDateTime)), this, SLOT(setDateTime(QDateTime)));
connect(this, SIGNAL(readExif(QString)), exifRW, SLOT(readExif(QString)));
connect(watcher, SIGNAL(fileChanged(QString)), this, SLOT(watchedFileChanged(QString)));
connect(reloadTimer, SIGNAL(timeout()), this, SLOT(imageReload()));
}
int ImageData::loadData(QString pictureFName)
{
pictureName = pictureFName;
QByteArray type = QImageReader::imageFormat(pictureName);
if (type == "") { // neni obrazek
return 1;
}
if (watcher->files().indexOf(pictureName) == -1) { // add filename to watcher (only if wasn't added previously)
watcher->addPath(pictureName);
}
scaleImage(pictureName);
emit readExif(pictureName);
return 0;
}
void ImageData::scaleImage(QString pictureName)
{
QImage img(pictureName);
if (img.isNull()) { // img je null
return;
}
std::unique_ptr<Exiv2::Image> image;
bool isExif = false;
try {
#ifdef _WIN32
image = Exiv2::ImageFactory::open(pictureName.toStdString());
#else
image = Exiv2::ImageFactory::open(pictureName.toStdString());
#endif
isExif = true;
} catch (Exiv2::Error& e) {
qDebug() << pictureName << e.what();
isExif = false;
}
if (isExif) {
image.get();
image->readMetadata();
Exiv2::ExifData& exifData = image->exifData();
if (!exifData.empty()) {
Exiv2::ExifKey key("Exif.Image.Orientation");
Exiv2::ExifData::iterator pos = exifData.findKey(key);
QTransform rm;
if (pos != exifData.end()) {
QString str = exifData["Exif.Image.Orientation"].toString().data();
switch (str.toInt()) {
case 3: // obraz otoceny o 180stupnu
img = img.transformed(rm.rotate(180), Qt::SmoothTransformation);
break;
case 6: // obraz otoceny o 90stupnu
img = img.transformed(rm.rotate(90), Qt::SmoothTransformation);
break;
case 8: // obraz otoceny o 280stupnu
img = img.transformed(rm.rotate(280), Qt::SmoothTransformation);
break;
default:
break;
}
}
}
}
*image_small = img.scaled(scaleSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
/**
*z EXIF
*/
void ImageData::setDateTime(QDateTime dateTimeNew)
{
*(this->dateTime) = dateTimeNew;
this->originalDateTime = dateTimeNew;
this->lastDateTimeSaved = dateTimeNew;
isDateTimeChanged = 0;
}
void ImageData::setGps(double _lat, double _lon, double _alt, double _direction, double _angleOfView)
{
latitude = _lat;
longitude = _lon;
altitude = _alt;
direction = _direction;
angleOfView = _angleOfView;
gpsSource = 1;
isGps = 1;
}
void ImageData::setObjGps(double _lat, double _lon)
{
objLatitude = _lat;
objLongitude = _lon;
}
/**
* @brief ImageData::imageFileChanged
* start timer when file changed to avoid repeated reload
* imageReload function will be triggered when timeout
* @param filename
*/
void ImageData::watchedFileChanged(const QString filename)
{
reloadTimer->start();
}
/**
* @brief ImageData::imageReload
* we want to reload thumbnail when file
*/
void ImageData::imageReload()
{
qDebug() << "reload " << pictureName;
loadData(pictureName);
emit imageReloadDone();
}