Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qimagecapture.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3#include <qimagecapture.h>
4#include <private/qplatformimagecapture_p.h>
5#include <qmediametadata.h>
6#include <private/qplatformmediacapture_p.h>
7#include <private/qplatformmediaintegration_p.h>
8#include <private/qplatformmediaformatinfo_p.h>
10
11#include "private/qobject_p.h"
12#include <qcamera.h>
13#include <private/qplatformcamera_p.h>
14#include <QtCore/qdebug.h>
15#include <QtCore/qurl.h>
16#include <QtCore/qstringlist.h>
17#include <QtCore/qmetaobject.h>
18
20
42{
43 Q_DECLARE_PUBLIC(QImageCapture)
44public:
45 QCamera *camera = nullptr;
46
49
53
54 void _q_error(int id, int error, const QString &errorString);
55
57
59};
60
61void QImageCapturePrivate::_q_error(int id, int error, const QString &errorString)
62{
63 Q_Q(QImageCapture);
64
65 this->error = QImageCapture::Error(error);
66 this->errorString = errorString;
67
68 emit q->errorChanged();
69 emit q->errorOccurred(id, this->error, errorString);
70}
71
82{
83 Q_D(QImageCapture);
84 d->q_ptr = this;
85
87 if (!maybeControl) {
88 qWarning() << "Failed to initialize QImageCapture" << maybeControl.error();
89 d->errorString = maybeControl.error();
90 d->error = NotReadyError;
91 return;
92 }
93
94 d->control = maybeControl.value();
95 connect(d->control, SIGNAL(imageExposed(int)),
96 this, SIGNAL(imageExposed(int)));
97 connect(d->control, SIGNAL(imageCaptured(int,QImage)),
98 this, SIGNAL(imageCaptured(int,QImage)));
101 connect(d->control, SIGNAL(imageAvailable(int,QVideoFrame)),
102 this, SIGNAL(imageAvailable(int,QVideoFrame)));
103 connect(d->control, SIGNAL(imageSaved(int,QString)),
104 this, SIGNAL(imageSaved(int,QString)));
105 connect(d->control, SIGNAL(readyForCaptureChanged(bool)),
106 this, SIGNAL(readyForCaptureChanged(bool)));
107 connect(d->control, SIGNAL(error(int,int,QString)),
108 this, SLOT(_q_error(int,int,QString)));
109}
110
120void QImageCapture::setCaptureSession(QMediaCaptureSession *session)
121{
122 Q_D(QImageCapture);
123 d->captureSession = session;
124}
125
131{
132 if (d_ptr->captureSession)
133 d_ptr->captureSession->setImageCapture(nullptr);
134 delete d_ptr;
135}
136
141{
142 return d_func()->control && d_func()->captureSession && d_func()->captureSession->camera();
143}
144
153{
154 return d_ptr->captureSession;
155}
156
166{
167 return d_func()->error;
168}
169
179{
180 return d_func()->errorString;
181}
182
191{
192 Q_D(const QImageCapture);
193 return d->metaData;
194}
195
201{
202 Q_D(QImageCapture);
203 d->metaData = metaData;
204 if (d->control)
205 d->control->setMetaData(d->metaData);
207}
208
214{
215 Q_D(QImageCapture);
216 auto data = d->metaData;
217 for (auto k : metaData.keys())
218 data.insert(k, metaData.value(k));
220}
221
230{
231 Q_D(const QImageCapture);
232 if (!d->control || !d->captureSession || !d->control->isReadyForCapture())
233 return false;
234 auto *camera = d->captureSession->camera();
235 if (!camera || !camera->isActive())
236 return false;
237 return true;
238}
239
269{
270 Q_D(QImageCapture);
271 if (!d->control) {
272 d->_q_error(-1, d->error, d->errorString);
273 return -1;
274 }
275
276 d->unsetError();
277
278 if (!isReadyForCapture()) {
279 d->_q_error(-1, NotReadyError, tr("Could not capture in stopped state"));
280 return -1;
281 }
282
283 return d->control->capture(file);
284}
285
299{
300 Q_D(QImageCapture);
301 if (!d->control) {
302 d->_q_error(-1, d->error, d->errorString);
303 return -1;
304 } else {
305 d->unsetError();
306 return d->control->captureToBuffer();
307 }
308}
309
374{
375 Q_D(const QImageCapture);
376 return d->control ? d->control->imageSettings().format() : UnspecifiedFormat;
377}
378
383{
384 Q_D(QImageCapture);
385 if (!d->control)
386 return;
387 auto fmt = d->control->imageSettings();
388 if (fmt.format() == format)
389 return;
390 fmt.setFormat(format);
391 d->control->setImageSettings(fmt);
393}
394
401{
403}
404
409{
410 const char *name = nullptr;
411 switch (f) {
413 name = "Unspecified image format";
414 break;
415 case JPEG:
416 name = "JPEG";
417 break;
418 case PNG:
419 name = "PNG";
420 break;
421 case WebP:
422 name = "WebP";
423 break;
424 case Tiff:
425 name = "Tiff";
426 break;
427 }
428 return QString::fromUtf8(name);
429}
430
435{
436 const char *name = nullptr;
437 switch (f) {
439 name = "Unspecified image format";
440 break;
441 case JPEG:
442 name = "JPEG";
443 break;
444 case PNG:
445 name = "PNG";
446 break;
447 case WebP:
448 name = "WebP";
449 break;
450 case Tiff:
451 name = "Tiff";
452 break;
453 }
454 return QString::fromUtf8(name);
455}
456
462{
463 Q_D(const QImageCapture);
464 return d->control ? d->control->imageSettings().resolution() : QSize{};
465}
466
479void QImageCapture::setResolution(const QSize &resolution)
480{
481 Q_D(QImageCapture);
482 if (!d->control)
483 return;
484 auto fmt = d->control->imageSettings();
485 if (fmt.resolution() == resolution)
486 return;
487 fmt.setResolution(resolution);
488 d->control->setImageSettings(fmt);
490}
491
498{
500}
501
519{
520 Q_D(const QImageCapture);
521 return d->control ? d->control->imageSettings().quality() : NormalQuality;
522}
523
528{
529 Q_D(QImageCapture);
530 if (!d->control)
531 return;
532 auto fmt = d->control->imageSettings();
533 if (fmt.quality() == quality)
534 return;
535 fmt.setQuality(quality);
536 d->control->setImageSettings(fmt);
538}
539
543QPlatformImageCapture *QImageCapture::platformImageCapture()
544{
545 Q_D(QImageCapture);
546 return d->control;
547}
548
550
551#include "moc_qimagecapture.cpp"
The QCamera class provides interface for system camera devices.
Definition qcamera.h:28
bool isActive() const
Returns true if the camera is currently active.
Definition qcamera.cpp:272
QPlatformImageCapture * control
QMediaMetaData metaData
QImageCapture * q_ptr
void _q_error(int id, int error, const QString &errorString)
QMediaCaptureSession * captureSession
QImageCapture::Error error
\inmodule QtMultimedia
static QString fileFormatName(FileFormat c)
Returns the name of the given format, f.
int captureToFile(const QString &location=QString())
Capture the image and save it to file.
static QList< FileFormat > supportedFormats()
Returns a list of supported file formats.
Error
\value NoError No Errors.
QMediaCaptureSession * captureSession() const
Returns the capture session this camera is connected to, or a nullptr if the camera is not connected ...
void setMetaData(const QMediaMetaData &metaData)
Replaces any existing meta data, to be embedded into the captured image, with a set of metaData.
FileFormat
Choose one of the following image formats:
void imageSaved(int id, const QString &fileName)
Signal emitted when QImageCapture::CaptureToFile is set and the frame with request id was saved to fi...
bool isReadyForCapture() const
Quality quality
The image encoding quality.
void fileFormatChanged()
~QImageCapture()
Destroys images capture object.
QImageCapture(QObject *parent=nullptr)
Constructs a image capture object, from a parent, that can capture individual still images produced b...
void addMetaData(const QMediaMetaData &metaData)
Adds additional metaData to any existing meta data, that is embedded into the captured image.
QSize resolution() const
Returns the resolution of the encoded image.
QString errorString
Returns a string describing the current error state.
void metaDataChanged()
void imageAvailable(int id, const QVideoFrame &frame)
Signal emitted when the frame with request id is available.
void setQuality(Quality quality)
Sets the image encoding quality.
Error error
Returns the current error state.
QMediaMetaData metaData
The meta data that will get embedded into the image.
bool isAvailable() const
Returns true if the images capture service ready to use.
void imageExposed(int id)
Signal emitted when the frame with request id was exposed.
static QString fileFormatDescription(FileFormat c)
Returns the description of the given file format, f.
void imageMetadataAvailable(int id, const QMediaMetaData &metaData)
Signals that an image identified by id has metaData.
void resolutionChanged()
Signals when the image resolution changes.
void imageCaptured(int id, const QImage &preview)
Signal emitted when the frame with request id was captured, but not processed and saved yet.
Quality
Enumerates quality encoding levels.
void setResolution(const QSize &)
Sets the resolution of the encoded image.
void readyForCaptureChanged(bool ready)
Signals that a camera's ready for capture state has changed.
void setFileFormat(FileFormat format)
Sets the image format.
FileFormat fileFormat
The image format.
int capture()
Capture the image and make it available as a QImage.
\inmodule QtGui
Definition qimage.h:37
Definition qlist.h:74
The QMediaCaptureSession class allows capturing of audio and video content.
void setImageCapture(QImageCapture *imageCapture)
\inmodule QtMultimedia
Q_INVOKABLE QVariant value(Key k) const
\variable QMediaMetaData::NumMetaData
Q_INVOKABLE QList< Key > keys() const
\qmlmethod list<Key> QtMultimedia::mediaMetaData::keys() Returns a list of MediaMetaData....
\inmodule QtCore
Definition qobject.h:90
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2823
QList< QImageCapture::FileFormat > imageFormats
virtual QMaybe< QPlatformImageCapture * > createImageCapture(QImageCapture *)
static QPlatformMediaIntegration * instance()
virtual QPlatformMediaFormatInfo * formatInfo()=0
\inmodule QtCore
Definition qsize.h:25
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
void clear()
Clears the contents of the string and makes it null.
Definition qstring.h:1107
static QString fromUtf8(QByteArrayView utf8)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5857
The QVideoFrame class represents a frame of video data.
Definition qvideoframe.h:26
QCamera * camera
Definition camera.cpp:19
Combined button and popup list for selecting options.
DBusConnection const char DBusError * error
#define qWarning
Definition qlogging.h:162
#define SLOT(a)
Definition qobjectdefs.h:51
#define SIGNAL(a)
Definition qobjectdefs.h:52
GLint GLsizei GLsizei height
GLfloat GLfloat f
GLint GLsizei width
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLuint name
GLint GLsizei GLsizei GLenum format
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
#define tr(X)
#define emit
QVideoFrameFormat::PixelFormat fmt
QFile file
[0]
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent