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
qwasmimagecapture.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 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
5#include <qimagewriter.h>
7#include "qwasmcamera_p.h"
8#include "qwasmvideosink_p.h"
9
11
12Q_LOGGING_CATEGORY(qWasmImageCapture, "qt.multimedia.wasm.imagecapture")
13/* TODO
14signals:
15imageExposed
16*/
18
20
22{
23 if (!isReadyForCapture()) {
25 return -1;
26 }
27
28 // TODO if fileName.isEmpty() we choose filename and location
29
30 QImage image = takePicture();
31 if (image.isNull())
32 return -1;
33
34 QImageWriter writer(fileName);
35 // TODO
36 // writer.setQuality(quality);
37 // writer.setFormat("png");
38
39 if (writer.write(image)) {
40 qCDebug(qWasmImageCapture) << Q_FUNC_INFO << "image saved";
41 emit imageSaved(m_lastId, fileName);
42 } else {
46
47 emit error(m_lastId, err, writer.errorString());
48 }
49
50 return m_lastId;
51}
52
54{
55 if (!isReadyForCapture()) {
57 return -1;
58 }
59
60 QImage image = takePicture();
61 if (image.isNull())
62 return -1;
63
64 emit imageCaptured(m_lastId, image);
65 return m_lastId;
66}
67
68QImage QWasmImageCapture::takePicture()
69{
70 QVideoFrame thisFrame = m_captureSession->videoSink()->videoFrame();
71 if (!thisFrame.isValid())
72 return QImage();
73
74 m_lastId++;
75 emit imageAvailable(m_lastId, thisFrame);
76
77 QImage image = thisFrame.toImage();
78 if (image.isNull()) {
79 qCDebug(qWasmImageCapture) << Q_FUNC_INFO << "image is null";
80 emit error(m_lastId, QImageCapture::ResourceError, QStringLiteral("Resource error"));
81 return QImage();
82 }
83
84 emit imageCaptured(m_lastId, image);
85 if (m_settings.resolution().isValid() && m_settings.resolution() != image.size())
86 image = image.scaled(m_settings.resolution());
87
88 return image;
89}
90
92{
93 return m_isReadyForCapture;
94}
95
97{
98 return m_settings;
99}
100
105
107{
108 if (m_isReadyForCapture != isReady) {
109 m_isReadyForCapture = isReady;
110 emit readyForCaptureChanged(m_isReadyForCapture);
111 }
112}
113
115{
116 QWasmMediaCaptureSession *captureSession = static_cast<QWasmMediaCaptureSession *>(session);
117 // nullptr clears
118 if (m_captureSession == captureSession)
119 return;
120
121 m_isReadyForCapture = captureSession;
122 if (captureSession) {
123 m_lastId = 0;
124 m_captureSession = captureSession;
125 }
126 emit readyForCaptureChanged(m_isReadyForCapture);
127 m_captureSession = captureSession;
128}
129
\inmodule QtMultimedia
Error
\value NoError No Errors.
The QImageWriter class provides a format independent interface for writing images to files or other d...
\inmodule QtGui
Definition qimage.h:37
void imageCaptured(int requestId, const QImage &preview)
void imageSaved(int requestId, const QString &fileName)
void imageAvailable(int requestId, const QVideoFrame &buffer)
void readyForCaptureChanged(bool ready)
constexpr bool isValid() const noexcept
Returns true if both the width and height is equal to or greater than 0; otherwise returns false.
Definition qsize.h:127
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
The QVideoFrame class represents a frame of video data.
Definition qvideoframe.h:27
QVideoFrame videoFrame() const
Returns the current video frame.
int capture(const QString &fileName) override
void setReadyForCapture(bool isReady)
bool isReadyForCapture() const override
void setImageSettings(const QImageEncoderSettings &settings) override
void setCaptureSession(QPlatformMediaCaptureSession *session)
QImageEncoderSettings imageSettings() const override
int captureToBuffer() override
Combined button and popup list for selecting options.
Definition image.cpp:4
#define Q_FUNC_INFO
DBusConnection const char DBusError * error
#define Q_LOGGING_CATEGORY(name,...)
#define qCDebug(category,...)
#define QStringLiteral(str)
#define emit
QSettings settings("MySoft", "Star Runner")
[0]