8#include <private/qmediastoragelocation_p.h>
10#include <QtConcurrent/qtconcurrentrun.h>
11#include <QtGui/qimagewriter.h>
20QWindowsImageCapture::~QWindowsImageCapture() =
default;
24 if (!m_mediaDeviceSession)
31 auto ext = writerFormat(m_settings.format());
32 auto path = QMediaStorageLocation::generateFileName(fileName, QStandardPaths::PicturesLocation, ext);
33 return doCapture(path);
38 return doCapture(QString());
45 m_fileName = fileName;
57 m_settings = settings;
63 if (m_captureService == captureService)
67 if (m_mediaDeviceSession)
68 disconnect(m_mediaDeviceSession,
nullptr,
this,
nullptr);
70 m_captureService = captureService;
71 if (!m_captureService) {
73 emit readyForCaptureChanged(
false);
74 m_mediaDeviceSession =
nullptr;
79 Q_ASSERT(m_mediaDeviceSession);
82 emit readyForCaptureChanged(isReadyForCapture());
84 connect(m_mediaDeviceSession, &QWindowsMediaDeviceSession::readyForCaptureChanged,
85 this, &QWindowsImageCapture::readyForCaptureChanged);
87 connect(m_mediaDeviceSession, &QWindowsMediaDeviceSession::videoFrameChanged,
88 this, &QWindowsImageCapture::handleVideoFrameChanged);
95 QImage image = frame.toImage();
97 QSize size = m_settings.resolution();
98 if (size.isValid() && image.size() != size) {
99 image = image.scaled(size, Qt::KeepAspectRatioByExpanding);
100 if (image.size() != size) {
101 int xoff = (image.size().width() - size.width()) / 2;
102 int yoff = (image.size().height() - size.height()) / 2;
103 image = image.copy(xoff, yoff, size.width(), size.height());
107 emit imageExposed(m_captureId);
108 emit imageAvailable(m_captureId, frame);
109 emit imageCaptured(m_captureId, image);
111 QMediaMetaData metaData =
this->metaData();
112 metaData.insert(QMediaMetaData::Date, QDateTime::currentDateTime());
113 metaData.insert(QMediaMetaData::Resolution, size);
115 emit imageMetadataAvailable(m_captureId, metaData);
117 if (!m_fileName.isEmpty()) {
119 (
void)QtConcurrent::run(&QWindowsImageCapture::saveImage,
this,
120 m_captureId, m_fileName, image, metaData, m_settings);
129 const QImage &image,
const QMediaMetaData &metaData,
130 const QImageEncoderSettings &settings)
132 QImageWriter imageWriter;
133 imageWriter.setFileName(fileName);
135 QString format = writerFormat(settings.format());
136 imageWriter.setFormat(format.toUtf8());
138 int quality = writerQuality(format, settings.quality());
140 imageWriter.setQuality(quality);
142 for (
auto key : metaData.keys())
143 imageWriter.setText(QMediaMetaData::metaDataKeyToString(key),
144 metaData.stringValue(key));
146 imageWriter.write(image);
148 QMetaObject::invokeMethod(
this,
"imageSaved", Qt::QueuedConnection,
149 Q_ARG(
int, captureId), Q_ARG(QString, fileName));
157 case QImageCapture::FileFormat::JPEG:
158 format = QLatin1String(
"jpg");
160 case QImageCapture::FileFormat::PNG:
161 format = QLatin1String(
"png");
163 case QImageCapture::FileFormat::WebP:
164 format = QLatin1String(
"webp");
166 case QImageCapture::FileFormat::Tiff:
167 format = QLatin1String(
"tiff");
170 format = QLatin1String(
"jpg");
173 auto supported = QImageWriter::supportedImageFormats();
174 for (
const auto &f : supported)
175 if (format.compare(QString::fromUtf8(f), Qt::CaseInsensitive) == 0)
178 return QLatin1String(
"jpg");
182 QImageCapture::Quality quality)
184 if (writerFormat.compare(QLatin1String(
"jpg"), Qt::CaseInsensitive) == 0 ||
185 writerFormat.compare(QLatin1String(
"jpeg"), Qt::CaseInsensitive) == 0) {
188 case QImageCapture::Quality::VeryLowQuality:
190 case QImageCapture::Quality::LowQuality:
192 case QImageCapture::Quality::NormalQuality:
194 case QImageCapture::Quality::HighQuality:
196 case QImageCapture::Quality::VeryHighQuality:
207#include "moc_qwindowsimagecapture_p.cpp"
int capture(const QString &fileName) override
int captureToBuffer() override
QImageEncoderSettings imageSettings() const override
void setImageSettings(const QImageEncoderSettings &settings) override
bool isReadyForCapture() const override
Combined button and popup list for selecting options.