QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
VideoScreenshot.cc
Go to the documentation of this file.
1#include "VideoScreenshot.h"
2
3#include <QtCore/QBuffer>
4#include <QtCore/QFileInfo>
5#include <QtCore/QSaveFile>
6#include <QtGui/QImage>
7#include <QtGui/QTransform>
8#include <QtMultimedia/QVideoFrame>
9
10#include "ExifParser.h"
11#include "ExifUtility.h"
12#include "QGCLoggingCategory.h"
13
14QGC_LOGGING_CATEGORY(VideoScreenshotLog, "Video.VideoScreenshot")
15
16namespace {
17
18bool isJpegFile(const QString& file)
19{
20 const QString suffix = QFileInfo(file).suffix();
21 return (suffix.compare(QStringLiteral("jpg"), Qt::CaseInsensitive) == 0) ||
22 (suffix.compare(QStringLiteral("jpeg"), Qt::CaseInsensitive) == 0);
23}
24
26bool addExif(QByteArray& jpeg, const VideoScreenshot::Exif& exif)
27{
28 if (exif.position.isValid()) {
29 GeoTagData geoTag;
30 geoTag.coordinate = exif.position;
31 if (!ExifParser::write(jpeg, geoTag)) {
32 return false;
33 }
34 }
35 if (exif.taken.isValid()) {
36 ExifData* data = ExifUtility::loadFromBuffer(jpeg);
37 if (!data) {
39 }
40 const bool written =
42 if (data) {
43 exif_data_unref(data);
44 }
45 if (!written) {
46 return false;
47 }
48 }
49 return true;
50}
51
52} // namespace
53
54QImage VideoScreenshot::displayedImage(const QVideoFrame& frame, QtVideo::Rotation outputRotation, bool outputMirrored)
55{
56 if (!frame.isValid()) {
57 return {};
58 }
59
60 // toImage() applies only the surface format's own transformation: not the frame's rotation or mirroring, nor the
61 // viewport that crops decoder padding.
62 QImage image = frame.toImage();
63 if (image.isNull()) {
64 return image;
65 }
66
67 const QRect viewport = frame.surfaceFormat().viewport();
68 if (viewport.isValid() && (image.size() == frame.size()) && (viewport != image.rect())) {
69 image = image.copy(viewport.intersected(image.rect()));
70 }
71
72 // Same order as Qt's presentation (qNormalizedFrameTransformation): the frame's rotation and mirroring, then the
73 // output's.
74 const auto rotateAndMirror = [&image](QtVideo::Rotation rotation, bool mirrored) {
75 if (const int degrees = static_cast<int>(rotation); degrees != 0) {
76 image = image.transformed(QTransform().rotate(degrees));
77 }
78 if (mirrored) {
79 image = image.flipped(Qt::Horizontal);
80 }
81 };
82 rotateAndMirror(frame.rotation(), frame.mirrored());
83 rotateAndMirror(outputRotation, outputMirrored);
84 return image;
85}
86
87bool VideoScreenshot::saveImage(const QImage& image, const QString& file, const Exif& exif)
88{
89 if (image.isNull()) {
90 qCWarning(VideoScreenshotLog) << "No video frame to take a screenshot from";
91 return false;
92 }
93
94 if (!isJpegFile(file) || (!exif.taken.isValid() && !exif.position.isValid())) {
95 if (!image.save(file)) {
96 qCWarning(VideoScreenshotLog) << "Failed to save screenshot" << file;
97 return false;
98 }
99 qCDebug(VideoScreenshotLog) << "Screenshot saved:" << file;
100 return true;
101 }
102
103 QByteArray jpeg;
104 QBuffer buffer(&jpeg);
105 if (!buffer.open(QIODevice::WriteOnly) || !image.save(&buffer, "JPG")) {
106 qCWarning(VideoScreenshotLog) << "Failed to encode screenshot" << file;
107 return false;
108 }
109 buffer.close();
110 // The picture matters more than its tags.
111 if (!addExif(jpeg, exif)) {
112 qCWarning(VideoScreenshotLog) << "Failed to add EXIF data to screenshot" << file;
113 }
114 QSaveFile out(file);
115 if (!out.open(QIODevice::WriteOnly) || (out.write(jpeg) != jpeg.size()) || !out.commit()) {
116 qCWarning(VideoScreenshotLog) << "Failed to save screenshot" << file << out.errorString();
117 return false;
118 }
119
120 qCDebug(VideoScreenshotLog) << "Screenshot saved:" << file;
121 return true;
122}
123
124bool VideoScreenshot::save(const QVideoFrame& frame, const QString& file, const Exif& exif,
125 QtVideo::Rotation outputRotation, bool outputMirrored)
126{
127 return saveImage(displayedImage(frame, outputRotation, outputMirrored), file, exif);
128}
#define QGC_LOGGING_CATEGORY(name, categoryStr)
bool write(QByteArray &buffer, const GeoTagData &geotag)
Definition ExifParser.cc:32
ExifData * loadFromBuffer(const QByteArray &buffer)
bool writeDateTimeOriginal(ExifData *data, const QDateTime &dateTime)
bool saveToBuffer(ExifData *data, QByteArray &buffer)
ExifData * createNew()
bool saveImage(const QImage &image, const QString &file, const Exif &exif={})
QImage displayedImage(const QVideoFrame &frame, QtVideo::Rotation outputRotation=QtVideo::Rotation::None, bool outputMirrored=false)
bool save(const QVideoFrame &frame, const QString &file, const Exif &exif={}, QtVideo::Rotation outputRotation=QtVideo::Rotation::None, bool outputMirrored=false)
displayedImage() of frame, then saveImage().
QGeoCoordinate coordinate
Definition GeoTagData.h:17
QGeoCoordinate position