1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
|
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <qcameraimagecapture.h>
#include <qcameraimagecapturecontrol.h>
#include <qmediaencodersettings.h>
#include "qmediasource_p.h"
#include <qmediaservice.h>
#include <qcamera.h>
#include <qcameracontrol.h>
#include <QtCore/qdebug.h>
#include <QtCore/qurl.h>
#include <QtCore/qstringlist.h>
#include <QtCore/qmetaobject.h>
QT_BEGIN_NAMESPACE
/*!
\class QCameraImageCapture
\inmodule QtMultimedia
\ingroup multimedia
\ingroup multimedia_camera
\brief The QCameraImageCapture class is used for the recording of media content.
The QCameraImageCapture class is a high level images recording class.
It's not intended to be used alone but for accessing the media
recording functions of other media objects, like QCamera.
\snippet multimedia-snippets/camera.cpp Camera
\snippet multimedia-snippets/camera.cpp Camera keys
\sa QCamera
*/
/*!
\enum QCameraImageCapture::CaptureDestination
\value CaptureToFile Capture the image to a file.
\value CaptureToBuffer Capture the image to a buffer for further processing.
*/
class QCameraImageCapturePrivate
{
Q_DECLARE_NON_CONST_PUBLIC(QCameraImageCapture)
public:
QCamera *camera = nullptr;
QCameraImageCaptureControl *control = nullptr;
QCameraImageCapture::Error error = QCameraImageCapture::NoError;
QString errorString;
void _q_error(int id, int error, const QString &errorString);
void _q_readyChanged(bool);
void _q_serviceDestroyed();
void unsetError() { error = QCameraImageCapture::NoError; errorString.clear(); }
QCameraImageCapture *q_ptr;
};
void QCameraImageCapturePrivate::_q_error(int id, int error, const QString &errorString)
{
Q_Q(QCameraImageCapture);
this->error = QCameraImageCapture::Error(error);
this->errorString = errorString;
emit q->error(id, this->error, errorString);
}
void QCameraImageCapturePrivate::_q_readyChanged(bool ready)
{
Q_Q(QCameraImageCapture);
emit q->readyForCaptureChanged(ready);
}
void QCameraImageCapturePrivate::_q_serviceDestroyed()
{
camera = nullptr;
control = nullptr;
}
/*!
Constructs a media recorder which records the media produced by \a camera.
The \a camera is also used as the parent of this object.
*/
QCameraImageCapture::QCameraImageCapture(QCamera *camera)
: QObject(camera), d_ptr(new QCameraImageCapturePrivate)
{
Q_ASSERT(camera);
Q_D(QCameraImageCapture);
d->q_ptr = this;
d->camera = camera;
QMediaService *service = camera->service();
if (service) {
d->control = qobject_cast<QCameraImageCaptureControl*>(service->requestControl(QCameraImageCaptureControl_iid));
if (d->control) {
connect(d->control, SIGNAL(imageExposed(int)),
this, SIGNAL(imageExposed(int)));
connect(d->control, SIGNAL(imageCaptured(int,QImage)),
this, SIGNAL(imageCaptured(int,QImage)));
connect(d->control, SIGNAL(imageMetadataAvailable(int,QString,QVariant)),
this, SIGNAL(imageMetadataAvailable(int,QString,QVariant)));
connect(d->control, SIGNAL(imageAvailable(int,QVideoFrame)),
this, SIGNAL(imageAvailable(int,QVideoFrame)));
connect(d->control, SIGNAL(imageSaved(int,QString)),
this, SIGNAL(imageSaved(int,QString)));
connect(d->control, SIGNAL(readyForCaptureChanged(bool)),
this, SLOT(_q_readyChanged(bool)));
connect(d->control, SIGNAL(error(int,int,QString)),
this, SLOT(_q_error(int,int,QString)));
connect(service, SIGNAL(destroyed()), this, SLOT(_q_serviceDestroyed()));
return;
}
}
// without QCameraImageCaptureControl discard the camera
d->camera = nullptr;
d->control = nullptr;
}
/*!
Destroys images capture object.
*/
QCameraImageCapture::~QCameraImageCapture()
{
delete d_ptr;
}
/*!
\reimp
*/
QCamera *QCameraImageCapture::camera() const
{
return d_func()->camera;
}
/*!
Returns true if the images capture service ready to use.
*/
bool QCameraImageCapture::isAvailable() const
{
return d_func()->control != nullptr;
}
/*!
Returns the availability of this functionality.
*/
QMultimedia::AvailabilityStatus QCameraImageCapture::availability() const
{
if (d_func()->control != nullptr)
return QMultimedia::Available;
return QMultimedia::ServiceMissing;
}
/*!
Returns the current error state.
\sa errorString()
*/
QCameraImageCapture::Error QCameraImageCapture::error() const
{
return d_func()->error;
}
/*!
Returns a string describing the current error state.
\sa error()
*/
QString QCameraImageCapture::errorString() const
{
return d_func()->errorString;
}
/*!
Returns the image encoder settings being used.
\sa setEncodingSettings()
*/
QImageEncoderSettings QCameraImageCapture::encodingSettings() const
{
return d_func()->control ?
d_func()->control->imageSettings() : QImageEncoderSettings();
}
/*!
Sets the image encoding \a settings.
If some parameters are not specified, or null settings are passed,
the encoder choose the default encoding parameters.
\sa encodingSettings()
*/
void QCameraImageCapture::setEncodingSettings(const QImageEncoderSettings &settings)
{
Q_D(QCameraImageCapture);
if (d->control) {
QCamera *camera = qobject_cast<QCamera*>(d->camera);
if (camera && camera->captureMode() == QCamera::CaptureStillImage) {
QMetaObject::invokeMethod(camera,
"_q_preparePropertyChange",
Qt::DirectConnection,
Q_ARG(int, QCameraControl::ImageEncodingSettings));
}
d->control->setImageSettings(settings);
}
}
/*!
Returns the image capture destination being used.
\sa isCaptureDestinationSupported(), setCaptureDestination()
*/
QCameraImageCapture::CaptureDestinations QCameraImageCapture::captureDestination() const
{
return d_func()->control->captureDestination();
}
/*!
Sets the capture \a destination to be used.
\sa isCaptureDestinationSupported(), captureDestination()
*/
void QCameraImageCapture::setCaptureDestination(QCameraImageCapture::CaptureDestinations destination)
{
Q_D(QCameraImageCapture);
auto old = d->control->captureDestination();
if (old == destination)
return;
d->control->setCaptureDestination(destination);
emit captureDestinationChanged(destination);
}
/*!
\property QCameraImageCapture::readyForCapture
\brief whether the service is ready to capture a an image immediately.
Calling capture() while \e readyForCapture is \c false is not permitted and
results in an error.
*/
bool QCameraImageCapture::isReadyForCapture() const
{
if (d_func()->control)
return d_func()->control->isReadyForCapture();
return false;
}
/*!
\fn QCameraImageCapture::readyForCaptureChanged(bool ready)
Signals that a camera's \a ready for capture state has changed.
*/
/*!
Capture the image and save it to \a file.
This operation is asynchronous in majority of cases,
followed by signals QCameraImageCapture::imageExposed(),
QCameraImageCapture::imageCaptured(), QCameraImageCapture::imageSaved()
or QCameraImageCapture::error().
If an empty \a file is passed, the camera backend choses
the default location and naming scheme for photos on the system,
if only file name without full path is specified, the image will be saved to
the default directory, with a full path reported with imageCaptured() and imageSaved() signals.
QCamera saves all the capture parameters like exposure settings or
image processing parameters, so changes to camera parameters after
capture() is called do not affect previous capture requests.
QCameraImageCapture::capture returns the capture Id parameter, used with
imageExposed(), imageCaptured() and imageSaved() signals.
\sa isReadyForCapture()
*/
int QCameraImageCapture::capture(const QString &file)
{
Q_D(QCameraImageCapture);
d->unsetError();
if (d->control)
return d->control->capture(file);
d->error = NotSupportedFeatureError;
d->errorString = tr("Device does not support images capture.");
emit error(-1, d->error, d->errorString);
return -1;
}
/*!
Cancel incomplete capture requests.
Already captured and queused for proicessing images may be discarded.
*/
void QCameraImageCapture::cancelCapture()
{
Q_D(QCameraImageCapture);
d->unsetError();
if (d->control) {
d->control->cancelCapture();
} else {
d->error = NotSupportedFeatureError;
d->errorString = tr("Device does not support images capture.");
emit error(-1, d->error, d->errorString);
}
}
/*!
\enum QCameraImageCapture::Error
\value NoError No Errors.
\value NotReadyError The service is not ready for capture yet.
\value ResourceError Device is not ready or not available.
\value OutOfSpaceError No space left on device.
\value NotSupportedFeatureError Device does not support stillimages capture.
\value FormatError Current format is not supported.
*/
/*!
\fn QCameraImageCapture::error(int id, QCameraImageCapture::Error error, const QString &errorString)
Signals that the capture request \a id has failed with an \a error
and \a errorString description.
*/
/*!
\fn QCameraImageCapture::bufferFormatChanged(QVideoFrame::PixelFormat format)
Signal emitted when the buffer \a format for the buffer image capture has changed.
*/
/*!
\fn QCameraImageCapture::captureDestinationChanged(CaptureDestinations destination)
Signal emitted when the capture \a destination has changed.
*/
/*!
\fn QCameraImageCapture::imageExposed(int id)
Signal emitted when the frame with request \a id was exposed.
*/
/*!
\fn QCameraImageCapture::imageCaptured(int id, const QImage &preview);
Signal emitted when QAbstractVideoSurface is used as a viewfinder and
the frame with request \a id was captured, but not processed and saved yet.
Frame \a preview can be displayed to user.
*/
/*!
\fn QCameraImageCapture::imageMetadataAvailable(int id, const QString &key, const QVariant &value)
Signals that a metadata for an image with request \a id is available. Also
includes the \a key and \a value of the metadata.
This signal is emitted between imageExposed and imageSaved signals.
*/
/*!
\fn QCameraImageCapture::imageAvailable(int id, const QVideoFrame &frame)
Signal emitted when QCameraImageCapture::CaptureToBuffer is set and
the \a frame with request \a id is available.
*/
/*!
\fn QCameraImageCapture::imageSaved(int id, const QString &fileName)
Signal emitted when QCameraImageCapture::CaptureToFile is set and
the frame with request \a id was saved to \a fileName.
*/
QT_END_NAMESPACE
#include "moc_qcameraimagecapture.cpp"
|