3#include <QtCore/QBuffer>
4#include <QtCore/QFileInfo>
5#include <QtCore/QSaveFile>
7#include <QtGui/QTransform>
8#include <QtMultimedia/QVideoFrame>
18bool isJpegFile(
const QString& file)
20 const QString suffix = QFileInfo(file).suffix();
21 return (suffix.compare(QStringLiteral(
"jpg"), Qt::CaseInsensitive) == 0) ||
22 (suffix.compare(QStringLiteral(
"jpeg"), Qt::CaseInsensitive) == 0);
35 if (exif.
taken.isValid()) {
43 exif_data_unref(data);
56 if (!frame.isValid()) {
62 QImage image = frame.toImage();
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()));
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));
79 image = image.flipped(Qt::Horizontal);
82 rotateAndMirror(frame.rotation(), frame.mirrored());
83 rotateAndMirror(outputRotation, outputMirrored);
90 qCWarning(VideoScreenshotLog) <<
"No video frame to take a screenshot from";
94 if (!isJpegFile(file) || (!exif.
taken.isValid() && !exif.
position.isValid())) {
95 if (!image.save(file)) {
96 qCWarning(VideoScreenshotLog) <<
"Failed to save screenshot" << file;
99 qCDebug(VideoScreenshotLog) <<
"Screenshot saved:" << file;
104 QBuffer buffer(&jpeg);
105 if (!buffer.open(QIODevice::WriteOnly) || !image.save(&buffer,
"JPG")) {
106 qCWarning(VideoScreenshotLog) <<
"Failed to encode screenshot" << file;
111 if (!addExif(jpeg, exif)) {
112 qCWarning(VideoScreenshotLog) <<
"Failed to add EXIF data to screenshot" << file;
115 if (!out.open(QIODevice::WriteOnly) || (out.write(jpeg) != jpeg.size()) || !out.commit()) {
116 qCWarning(VideoScreenshotLog) <<
"Failed to save screenshot" << file << out.errorString();
120 qCDebug(VideoScreenshotLog) <<
"Screenshot saved:" << file;
125 QtVideo::Rotation outputRotation,
bool outputMirrored)
#define QGC_LOGGING_CATEGORY(name, categoryStr)
bool write(QByteArray &buffer, const GeoTagData &geotag)
ExifData * loadFromBuffer(const QByteArray &buffer)
bool writeDateTimeOriginal(ExifData *data, const QDateTime &dateTime)
bool saveToBuffer(ExifData *data, QByteArray &buffer)
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