Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
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
81 : QObject(parent), d_ptr(new QImageCapturePrivate)
82{
83 Q_D(QImageCapture);
84 d->q_ptr = this;
85
86 auto maybeControl = QPlatformMediaIntegration::instance()->createImageCapture(this);
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();
104 connect(d->control, SIGNAL(error(int,int,QString)),
105 this, SLOT(_q_error(int,int,QString)));
106}
107
117void QImageCapture::setCaptureSession(QMediaCaptureSession *session)
118{
119 Q_D(QImageCapture);
120 d->captureSession = session;
121}
122
128{
129 if (d_ptr->captureSession)
130 d_ptr->captureSession->setImageCapture(nullptr);
131 delete d_ptr;
132}
133
138{
139 return d_func()->control && d_func()->captureSession && d_func()->captureSession->camera();
140}
141
153
163{
164 return d_func()->error;
165}
166
176{
177 return d_func()->errorString;
178}
179
188{
189 Q_D(const QImageCapture);
190 return d->metaData;
191}
192
198{
199 Q_D(QImageCapture);
200 d->metaData = metaData;
201 if (d->control)
202 d->control->setMetaData(d->metaData);
204}
205
211{
212 Q_D(QImageCapture);
213 auto data = d->metaData;
214 for (auto k : metaData.keys())
215 data.insert(k, metaData.value(k));
217}
218
227{
228 Q_D(const QImageCapture);
229 if (!d->control || !d->captureSession || !d->control->isReadyForCapture())
230 return false;
231 auto *camera = d->captureSession->camera();
232 if (!camera || !camera->isActive())
233 return false;
234 return true;
235}
236
266{
267 Q_D(QImageCapture);
268 if (!d->control) {
269 d->_q_error(-1, d->error, d->errorString);
270 return -1;
271 }
272
273 d->unsetError();
274
275 if (!isReadyForCapture()) {
276 d->_q_error(-1, NotReadyError, tr("Could not capture in stopped state"));
277 return -1;
278 }
279
280 return d->control->capture(file);
281}
282
296{
297 Q_D(QImageCapture);
298 if (!d->control) {
299 d->_q_error(-1, d->error, d->errorString);
300 return -1;
301 } else {
302 d->unsetError();
303 return d->control->captureToBuffer();
304 }
305}
306
371{
372 Q_D(const QImageCapture);
373 return d->control ? d->control->imageSettings().format() : UnspecifiedFormat;
374}
375
380{
381 Q_D(QImageCapture);
382 if (!d->control)
383 return;
384 auto fmt = d->control->imageSettings();
385 if (fmt.format() == format)
386 return;
387 fmt.setFormat(format);
388 d->control->setImageSettings(fmt);
390}
391
397QList<QImageCapture::FileFormat> QImageCapture::supportedFormats()
398{
399 return QPlatformMediaIntegration::instance()->formatInfo()->imageFormats;
400}
401
406{
407 const char *name = nullptr;
408 switch (f) {
410 name = "Unspecified image format";
411 break;
412 case JPEG:
413 name = "JPEG";
414 break;
415 case PNG:
416 name = "PNG";
417 break;
418 case WebP:
419 name = "WebP";
420 break;
421 case Tiff:
422 name = "Tiff";
423 break;
424 }
425 return QString::fromUtf8(name);
426}
427
432{
433 const char *name = nullptr;
434 switch (f) {
436 name = "Unspecified image format";
437 break;
438 case JPEG:
439 name = "JPEG";
440 break;
441 case PNG:
442 name = "PNG";
443 break;
444 case WebP:
445 name = "WebP";
446 break;
447 case Tiff:
448 name = "Tiff";
449 break;
450 }
451 return QString::fromUtf8(name);
452}
453
459{
460 Q_D(const QImageCapture);
461 return d->control ? d->control->imageSettings().resolution() : QSize{};
462}
463
476void QImageCapture::setResolution(const QSize &resolution)
477{
478 Q_D(QImageCapture);
479 if (!d->control)
480 return;
481 auto fmt = d->control->imageSettings();
482 if (fmt.resolution() == resolution)
483 return;
484 fmt.setResolution(resolution);
485 d->control->setImageSettings(fmt);
487}
488
498
516{
517 Q_D(const QImageCapture);
518 return d->control ? d->control->imageSettings().quality() : NormalQuality;
519}
520
525{
526 Q_D(QImageCapture);
527 if (!d->control)
528 return;
529 auto fmt = d->control->imageSettings();
530 if (fmt.quality() == quality)
531 return;
532 fmt.setQuality(quality);
533 d->control->setImageSettings(fmt);
535}
536
540QPlatformImageCapture *QImageCapture::platformImageCapture()
541{
542 Q_D(QImageCapture);
543 return d->control;
544}
545
547
548#include "moc_qimagecapture.cpp"
The QCamera class provides interface for system camera devices.
Definition qcamera.h:28
QMediaCaptureSession * captureSession() const
Returns the capture session this camera is connected to, or a nullptr if the camera is not connected ...
Definition qcamera.cpp:391
bool isActive() const
Returns true if the camera is currently active.
Definition qcamera.cpp:259
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.
The QMediaCaptureSession class allows capturing of audio and video content.
void setImageCapture(QImageCapture *imageCapture)
QCamera * camera
\qmlproperty Camera QtMultimedia::CaptureSession::camera
\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:103
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
void imageCaptured(int requestId, const QImage &preview)
void imageSaved(int requestId, const QString &fileName)
void imageAvailable(int requestId, const QVideoFrame &buffer)
void imageMetadataAvailable(int id, const QMediaMetaData &)
void imageExposed(int requestId)
void readyForCaptureChanged(bool ready)
static QPlatformMediaIntegration * instance()
\inmodule QtCore
Definition qsize.h:25
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
void clear()
Clears the contents of the string and makes it null.
Definition qstring.h:1252
static QString fromUtf8(QByteArrayView utf8)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:6018
QCamera * camera
Definition camera.cpp:19
Combined button and popup list for selecting options.
DBusConnection const char DBusError * error
#define qWarning
Definition qlogging.h:166
#define SLOT(a)
Definition qobjectdefs.h:52
#define SIGNAL(a)
Definition qobjectdefs.h:53
GLint GLsizei GLsizei height
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLfloat GLfloat f
GLint GLsizei width
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]