Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qcameradevice.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 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
6#include "qcamera_p.h"
7
9
10using namespace Qt::StringLiterals;
11
12/*!
13 \class QCameraFormat
14 \since 6.2
15 \brief The QCameraFormat class describes a video format supported by a camera device.
16 \inmodule QtMultimedia
17 \ingroup multimedia
18 \ingroup multimedia_camera
19
20 QCameraFormat represents a certain video format supported by a camera device.
21
22 //! [format-description]
23 The format is a combination of a
24 \l{QVideoFrameFormat::PixelFormat}{pixel format}, resolution and a range of frame
25 rates.
26 //! [format-description]
27
28 QCameraFormat objects can be queried from QCameraDevice to inspect the set of
29 supported video formats.
30
31 \sa QCameraDevice, QCamera
32*/
33
34/*!
35 \qmlvaluetype cameraFormat
36 \ingroup qmlvaluetypes
37 \inqmlmodule QtMultimedia
38 \since 6.2
39 \nativetype QCameraFormat
40 \brief Describes a video format supported by a camera device.
41 \ingroup multimedia_qml
42 \ingroup multimedia_video_qml
43
44 cameraFormat represents a certain video format supported by a camera device.
45
46 \include qcameradevice.cpp format-description
47
48 cameraFormat objects can be queried from \l cameraDevice to inspect the set of
49 supported video formats.
50
51 \sa cameraDevice, Camera
52*/
53
54/*!
55 Constructs a null camera format.
56
57 \sa isNull()
58*/
59QCameraFormat::QCameraFormat() noexcept = default;
60
61/*!
62 Copy constructs a camera format from the \a other format.
63*/
64QCameraFormat::QCameraFormat(const QCameraFormat &other) noexcept = default;
65
66/*!
67 Assign \a other to this.
68*/
69QCameraFormat &QCameraFormat::operator=(const QCameraFormat &other) noexcept = default;
70
71/*!
72 Destructs the camera format object.
73*/
74QCameraFormat::~QCameraFormat() = default;
75
76/*! \fn bool QCameraFormat::isNull() const noexcept
77
78 Returns true if this is a default constructed QCameraFormat.
79*/
80
81/*!
82 \qmlproperty enumeration QtMultimedia::cameraFormat::pixelFormat
83
84 Holds the pixel format.
85
86 Most commonly this is either QVideoFrameFormat::Format_Jpeg or QVideoFrameFormat::Format_YUYV
87 but other formats could also be supported by the camera.
88
89 \include qcameradevice.cpp android-yuv-note
90
91 \sa QVideoFrameFormat::PixelFormat
92*/
93
94/*!
95 \property QCameraFormat::pixelFormat
96
97 Returns the pixel format.
98
99 Most commonly this is either QVideoFrameFormat::Format_Jpeg or QVideoFrameFormat::Format_YUYV
100 but other formats could also be supported by the camera.
101
102 //! [android-yuv-note]
103 \note On Android with the FFmpeg media backend, camera formats are reported as
104 QVideoFrameFormat::Format_YUV420P, but the delivered video frames can be either
105 fully planar YUV420P or semi-planar NV12 or NV21. The memory layout is chosen by
106 the camera implementation of the device, and Android provides no way to query
107 it in advance. Check the pixel format of the received video frames instead.
108 //! [android-yuv-note]
109
110 \sa QVideoFrameFormat::PixelFormat
111*/
112QVideoFrameFormat::PixelFormat QCameraFormat::pixelFormat() const noexcept
113{
114 return d ? d->pixelFormat : QVideoFrameFormat::Format_Invalid;
115}
116
117/*!
118 \qmlproperty size QtMultimedia::cameraFormat::resolution
119
120 Returns the resolution.
121*/
122
123/*!
124 \property QCameraFormat::resolution
125
126 Returns the resolution.
127*/
128QSize QCameraFormat::resolution() const noexcept
129{
130 return d ? d->resolution : QSize();
131}
132
133/*!
134 \qmlproperty real QtMultimedia::cameraFormat::minFrameRate
135
136 Returns the lowest frame rate defined by this format.
137*/
138
139/*!
140 \property QCameraFormat::minFrameRate
141
142 Returns the lowest frame rate defined by this format.
143*/
144float QCameraFormat::minFrameRate() const noexcept
145{
146 return d ? d->minFrameRate : 0;
147}
148
149/*!
150 \qmlproperty real QtMultimedia::cameraFormat::maxFrameRate
151
152 Returns the highest frame rate defined by this format.
153
154 The camera will always try to use the maximum frame rate supported by a
155 certain video format.
156*/
157
158/*!
159 \property QCameraFormat::maxFrameRate
160
161 Returns the highest frame rate defined by this format.
162
163 The camera will always try to use the highest frame rate supported by a
164 certain video format.
165*/
166float QCameraFormat::maxFrameRate() const noexcept
167{
168 return d ? d->maxFrameRate : 0;
169}
170
171/*!
172 \internal
173*/
174QCameraFormat::QCameraFormat(QCameraFormatPrivate *p)
175 : d(p)
176{
177}
178
179[[nodiscard]] static bool fuzzyCompareFrameRates(float a, float b)
180{
181 if (qFuzzyIsNull(a) || qFuzzyIsNull(b))
182 return qFuzzyIsNull(a) && qFuzzyIsNull(b);
183 return qFuzzyCompare(a, b);
184}
185
186/*!
187 Returns \c true if the \a other format is equal to this camera format, otherwise \c false.
188
189 Frame rates are compared with a small relative tolerance, since camera devices can
190 report the same frame rate with slightly different floating-point values.
191*/
192bool QCameraFormat::operator==(const QCameraFormat &other) const
193{
194 if (d == other.d)
195 return true;
196 if (!d || !other.d)
197 return false;
198 return d->pixelFormat == other.d->pixelFormat
199 && fuzzyCompareFrameRates(d->minFrameRate, other.d->minFrameRate)
200 && fuzzyCompareFrameRates(d->maxFrameRate, other.d->maxFrameRate)
201 && d->resolution == other.d->resolution;
202}
203
204/*!
205 \fn bool QCameraFormat::operator!=(const QCameraFormat &other) const
206
207 Returns \c false if the \a other format is equal to this camera format, otherwise \c true.
208*/
209
210/*!
211 \class QCameraDevice
212 \brief The QCameraDevice class provides general information about camera devices.
213 \inmodule QtMultimedia
214 \ingroup multimedia
215 \ingroup multimedia_camera
216
217 QCameraDevice represents a physical camera device and its properties.
218
219 You can discover what cameras are available on a system using the
220 availableCameras() and defaultCamera() functions. These are contained within
221 QtMultimedia::MediaDevices.
222
223 \include qdevice-retains-properties.qdocinc {retains-properties} {QCameraDevice} {QMediaDevices}
224
225 This example prints the name of all available cameras:
226
227 \snippet multimedia-snippets/camerasnippets.cpp Camera listing
228
229 A QCameraDevice can be used to construct a QCamera. The following example
230 instantiates a QCamera whose camera device is named \c {mycamera}:
231
232 \snippet multimedia-snippets/camerasnippets.cpp Camera selection
233
234 You can also use QCameraDevice to get general information about a camera
235 device such as description and physical position on the system.
236
237 \snippet multimedia-snippets/camerasnippets.cpp Camera info
238
239 \sa QCamera
240*/
241
242/*!
243 \qmlvaluetype cameraDevice
244 \ingroup qmlvaluetypes
245 \inqmlmodule QtMultimedia
246 \since 6.2
247 \nativetype QCameraDevice
248 \brief Describes a camera device.
249 \ingroup multimedia_qml
250 \ingroup multimedia_video_qml
251
252 The cameraDevice value type describes the properties of a camera device that
253 is connected to the system.
254
255 \include qdevice-retains-properties.qdocinc {retains-properties} {cameraDevice} {MediaDevices}
256
257 Two cameraDevice instances can be compared for equality. They are
258 considered equal if they represent the same physical device,
259 regardless of whether their properties are equal or up-to-date.
260
261 The list of camera devices can be queried from the \l{MediaDevices}
262 type. To select a certain camera device set it as the device
263 on \l{Camera}.
264
265 \qml
266 CaptureSession {
267 camera: Camera {
268 cameraDevice: mediaDevices.defaultVideoInput
269 }
270 }
271 MediaDevices {
272 id: mediaDevices
273 }
274 \endqml
275*/
276
277/*!
278 Constructs a null camera device
279*/
280QCameraDevice::QCameraDevice() = default;
281
282/*!
283 Constructs a copy of \a other.
284*/
285QCameraDevice::QCameraDevice(const QCameraDevice &other) = default;
286
287/*!
288 Destroys the QCameraDevice.
289*/
290QCameraDevice::~QCameraDevice() = default;
291
292/*!
293 Returns true if this \l QCameraDevice represents the same device as \a other.
294
295 Due to the behavior of the properties in QCameraDevice, two
296 QCameraDevice instances can be considered equal even if not
297 all the properties are equal.
298*/
299bool QCameraDevice::operator==(const QCameraDevice &other) const
300{
301 return id() == other.id();
302}
303
304/*!
305 Returns true if this QCameraDevice is null or invalid.
306*/
307bool QCameraDevice::isNull() const
308{
309 return !d;
310}
311
312/*!
313 \qmlproperty string QtMultimedia::cameraDevice::id
314
315 Holds the device id of the camera
316
317 This is a unique ID to identify the camera and may not be human-readable.
318*/
319
320/*!
321 \property QCameraDevice::id
322
323 Returns the device id of the camera
324
325 This is a unique ID to identify the camera and may not be human-readable.
326*/
327QByteArray QCameraDevice::id() const
328{
329 return d ? d->id : QByteArray();
330}
331
332/*!
333 \qmlproperty bool QtMultimedia::cameraDevice::isDefault
334
335 Is true if this is the default camera device.
336*/
337
338/*!
339 \property QCameraDevice::isDefault
340
341 Returns true if this is the default camera device.
342*/
343bool QCameraDevice::isDefault() const
344{
345 return d ? d->isDefault : false;
346}
347
348/*!
349 \since 6.7
350 \qmlproperty QtVideo::Rotation QtMultimedia::cameraDevice::correctionAngle
351
352 Returns the rotation angle needed to compensate for the physical camera rotation of the camera
353 compared to its native orientation. In other words, the property represents the clockwise angle
354 through which the output image needs to be rotated to be upright on the device screen in its
355 native orientation. Since \a correctionAngle is relative to the native orientation, this value
356 does not change with altering the device orientation (portrait/landscape). The correction angle
357 may be non-zero mostly on Android, where native and camera orientations are defined by the manufacturer.
358
359 \image camera_correctionAngle_90.png Example with 90 degrees \a correctionAngle
360*/
361
362/*!
363 \since 6.7
364 \property QCameraDevice::correctionAngle
365
366 Returns the rotation angle needed to compensate for the physical camera rotation of the camera
367 compared to its native orientation. In other words, the property represents the clockwise angle
368 through which the output image needs to be rotated to be upright on the device screen in its
369 native orientation. Since \a correctionAngle is relative to the native orientation, this value
370 does not change with altering the device orientation (portrait/landscape). The correction angle
371 may be non-zero mostly on Android, where native and camera orientations are defined by the manufacturer.
372
373 \image camera_correctionAngle_90.png Example with 90 degrees \a correctionAngle
374*/
375QtVideo::Rotation QCameraDevice::correctionAngle() const
376{
377 return d ? QtVideo::Rotation(d->orientation) : QtVideo::Rotation::None;
378}
379
380/*!
381 \qmlproperty string QtMultimedia::cameraDevice::description
382
383 Holds a human readable name of the camera.
384
385 Use this string to present the device to the user.
386*/
387
388/*!
389 \property QCameraDevice::description
390
391 Returns the human-readable description of the camera.
392
393 Use this string to present the device to the user.
394*/
395QString QCameraDevice::description() const
396{
397 return d ? d->description : QString();
398}
399
400/*!
401 \enum QCameraDevice::Position
402
403 This enum specifies the physical position of the camera on the system hardware.
404
405 \value UnspecifiedPosition The camera position is unspecified or unknown.
406 \value BackFace The camera is on the back face of the system hardware. For example on a
407 mobile device, it means it is on the opposite side to that of the screen.
408 \value FrontFace The camera is on the front face of the system hardware. For example on a
409 mobile device, it means it is on the same side as that of the screen.
410 Front-facing cameras generate video frames with the property
411 \l QVideoFrame::mirrored set to \c true. This means that the presentation of these
412 frames is flipped around the vertical axis to display the video output as a mirror,
413 whereas recording only considers the transformations of the surface specified in
414 \l QVideoFrame::surfaceFormat.
415
416 \sa position()
417*/
418
419/*!
420 \qmlproperty enumeration QtMultimedia::cameraDevice::position
421
422 Returns the physical position of the camera on the hardware system.
423
424 The returned value can be one of the following:
425
426 \value cameraDevice.UnspecifiedPosition The camera position is unspecified or unknown.
427 \value cameraDevice.BackFace The camera is on the back face of the system hardware. For example on a
428 mobile device, it means it is on the opposite side to that of the screen.
429 \value cameraDevice.FrontFace The camera is on the front face of the system hardware. For example on a
430 mobile device, it means it is on the same side as that of the screen.
431 Preview of front-facing cameras is flipped around the vertical axis
432 to display the video output as a mirror, whereas this flipping is not
433 performed during recording.
434*/
435
436/*!
437 \property QCameraDevice::position
438
439 Returns the physical position of the camera on the hardware system.
440*/
441QCameraDevice::Position QCameraDevice::position() const
442{
443 return d ? d->position : QCameraDevice::UnspecifiedPosition;
444}
445
446/*!
447 \qmlproperty list<size> QtMultimedia::cameraDevice::photoResolutions
448 \since 6.13
449
450 Holds the resolutions the camera can use to capture still images.
451
452 \sa ImageCapture::resolution, videoFormats
453*/
454
455/*!
456 \property QCameraDevice::photoResolutions
457
458 Returns the resolutions the camera can use to capture still images.
459
460 The list can be empty if the camera or platform does not report still capture resolutions.
461 The order of the sizes is unspecified. Not every size is guaranteed to be usable together
462 with every QCameraFormat.
463
464 \sa QImageCapture::resolution, videoFormats()
465*/
466
467QList<QSize> QCameraDevice::photoResolutions() const
468{
469 return d ? d->photoResolutions : QList<QSize>{};
470}
471
472/*!
473 \qmlproperty CameraFormat QtMultimedia::cameraDevice::videoFormats
474
475 Holds the video formats supported by the camera.
476*/
477
478/*!
479 \property QCameraDevice::videoFormats
480
481 Returns the video formats supported by the camera.
482*/
483QList<QCameraFormat> QCameraDevice::videoFormats() const
484{
485 return d ? d->videoFormats : QList<QCameraFormat>{};
486}
487
488QCameraDevice::QCameraDevice(QCameraDevicePrivate *p)
489 : d(p)
490{}
491
492/*!
493 Sets the QCameraDevice object to be equal to \a other.
494*/
495QCameraDevice& QCameraDevice::operator=(const QCameraDevice& other) = default;
496
497/*!
498 \fn QCameraDevice::operator!=(const QCameraDevice &other) const
499
500 Returns true if this QCameraDevice does not represent the same
501 device as \a other.
502*/
503
504#ifndef QT_NO_DEBUG_STREAM
505QDebug operator<<(QDebug d, const QCameraFormat &format)
506{
507 if (format.isNull())
508 d.maybeSpace() << u"QCameraFormat(null)"_s;
509 else {
510 const char *pixelFormatCharPtr = QMetaEnum::fromType<QVideoFrameFormat::PixelFormat>()
511 .valueToKey(format.pixelFormat());
512 QString pixelFormatString = pixelFormatCharPtr
513 ? QString::fromUtf8(pixelFormatCharPtr)
514 : u"Invalid"_s;
515
516 d.maybeSpace()
517 << u"QCameraFormat(resolution=%1x%2, pixelFormat=%3, minFrameRate=%4, maxFrameRate=%5"_s
518 .arg(format.resolution().width())
519 .arg(format.resolution().height())
520 .arg(pixelFormatString)
521 .arg(format.minFrameRate())
522 .arg(format.maxFrameRate());
523 }
524
525 return d.space();
526}
527
528QDebug operator<<(QDebug d, const QCameraDevice &camera)
529{
530 d.maybeSpace() << u"QCameraDevice(name=%1, id=%2, position=%3)"_s
531 .arg(camera.description())
532 .arg(QLatin1StringView(camera.id()))
533 .arg(QLatin1StringView(
534 QMetaEnum::fromType<QCameraDevice::Position>().valueToKey(
535 camera.position())));
536 return d.space();
537}
538#endif
539
540QT_END_NAMESPACE
541
542#include "moc_qcameradevice.cpp"
Combined button and popup list for selecting options.
QDebug operator<<(QDebug d, const QCameraDevice &camera)
static bool fuzzyCompareFrameRates(float a, float b)
QDebug operator<<(QDebug dbg, const QFileInfo &fi)