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 The format is a combination of a
23 \l{QVideoFrameFormat::PixelFormat}{pixel format}, resolution and a range of frame
24 rates.
25
26 QCameraFormat objects can be queried from QCameraDevice to inspect the set of
27 supported video formats.
28
29 \sa QCameraDevice, QCamera
30*/
31
32/*!
33 \qmlvaluetype cameraFormat
34 \ingroup qmlvaluetypes
35 \inqmlmodule QtMultimedia
36 \since 6.2
37 \nativetype QCameraFormat
38 \brief Describes a video format supported by a camera device.
39 \ingroup multimedia_qml
40 \ingroup multimedia_video_qml
41
42 cameraFormat represents a certain video format supported by a camera device.
43
44 The format is a combination of a
45 \l{QVideoFrameFormat::PixelFormat}{pixel format}, resolution, and a range
46 of frame frates.
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_YUVY
87 but other formats could also be supported by the camera.
88
89 \sa QVideoFrameFormat::PixelFormat
90*/
91
92/*!
93 \property QCameraFormat::pixelFormat
94
95 Returns the pixel format.
96
97 Most commonly this is either QVideoFrameFormat::Format_Jpeg or QVideoFrameFormat::Format_YUVY
98 but other formats could also be supported by the camera.
99
100 \sa QVideoFrameFormat::PixelFormat
101*/
102QVideoFrameFormat::PixelFormat QCameraFormat::pixelFormat() const noexcept
103{
104 return d ? d->pixelFormat : QVideoFrameFormat::Format_Invalid;
105}
106
107/*!
108 \qmlproperty size QtMultimedia::cameraFormat::resolution
109
110 Returns the resolution.
111*/
112
113/*!
114 \property QCameraFormat::resolution
115
116 Returns the resolution.
117*/
118QSize QCameraFormat::resolution() const noexcept
119{
120 return d ? d->resolution : QSize();
121}
122
123/*!
124 \qmlproperty real QtMultimedia::cameraFormat::minFrameRate
125
126 Returns the lowest frame rate defined by this format.
127*/
128
129/*!
130 \property QCameraFormat::minFrameRate
131
132 Returns the lowest frame rate defined by this format.
133*/
134float QCameraFormat::minFrameRate() const noexcept
135{
136 return d ? d->minFrameRate : 0;
137}
138
139/*!
140 \qmlproperty real QtMultimedia::cameraFormat::maxFrameRate
141
142 Returns the highest frame rate defined by this format.
143
144 The camera will always try to use the maximum frame rate supported by a
145 certain video format.
146*/
147
148/*!
149 \property QCameraFormat::maxFrameRate
150
151 Returns the highest frame rate defined by this format.
152
153 The camera will always try to use the highest frame rate supported by a
154 certain video format.
155*/
156float QCameraFormat::maxFrameRate() const noexcept
157{
158 return d ? d->maxFrameRate : 0;
159}
160
161/*!
162 \internal
163*/
164QCameraFormat::QCameraFormat(QCameraFormatPrivate *p)
165 : d(p)
166{
167}
168
169/*!
170 Returns \c true if the \a other format is equal to this camera format, otherwise \c false.
171*/
172bool QCameraFormat::operator==(const QCameraFormat &other) const
173{
174 if (d == other.d)
175 return true;
176 if (!d || !other.d)
177 return false;
178 return d->pixelFormat == other.d->pixelFormat &&
179 d->minFrameRate == other.d->minFrameRate &&
180 d->maxFrameRate == other.d->maxFrameRate &&
181 d->resolution == other.d->resolution;
182}
183
184/*!
185 \fn bool QCameraFormat::operator!=(const QCameraFormat &other) const
186
187 Returns \c false if the \a other format is equal to this camera format, otherwise \c true.
188*/
189
190/*!
191 \class QCameraDevice
192 \brief The QCameraDevice class provides general information about camera devices.
193 \inmodule QtMultimedia
194 \ingroup multimedia
195 \ingroup multimedia_camera
196
197 QCameraDevice represents a physical camera device and its properties.
198
199 You can discover what cameras are available on a system using the
200 availableCameras() and defaultCamera() functions. These are contained within
201 QtMultimedia::MediaDevices.
202
203 The QCameraDevice instance retains its properties throughout its lifetime,
204 even if the corresponding physical device is disconnected or its settings are
205 modified. To keep track of updated properties, the user should load new instances
206 of QCameraDevice from \l{QMediaDevices} when the relevant signals are fired.
207
208 This example prints the name of all available cameras:
209
210 \snippet multimedia-snippets/camera.cpp Camera listing
211
212 A QCameraDevice can be used to construct a QCamera. The following example
213 instantiates a QCamera whose camera device is named \c {mycamera}:
214
215 \snippet multimedia-snippets/camera.cpp Camera selection
216
217 You can also use QCameraDevice to get general information about a camera
218 device such as description and physical position on the system.
219
220 \snippet multimedia-snippets/camera.cpp Camera info
221
222 \sa QCamera
223*/
224
225/*!
226 \qmlvaluetype cameraDevice
227 \ingroup qmlvaluetypes
228 \inqmlmodule QtMultimedia
229 \since 6.2
230 \nativetype QCameraDevice
231 \brief Describes a camera device.
232 \ingroup multimedia_qml
233 \ingroup multimedia_video_qml
234
235 The cameraDevice value type describes the properties of a camera device that
236 is connected to the system.
237
238 The cameraDevice instance retains its properties throughout its lifetime,
239 even if the corresponding physical device is disconnected or its settings are
240 modified. To keep track of updated properties, the user should load new instances
241 of cameraDevice from \l{MediaDevices} when the relevant signals are fired.
242
243 Two cameraDevice instances can be compared for equality. They are
244 considered equal if they represent the same physical device,
245 regardless of whether their properties are equal or up-to-date.
246
247 The list of camera devices can be queried from the \l{MediaDevices}
248 type. To select a certain camera device set it as the device
249 on \l{Camera}.
250
251 \qml
252 CaptureSession {
253 camera: Camera {
254 cameraDevice: mediaDevices.defaultVideoInput
255 }
256 }
257 MediaDevices {
258 id: mediaDevices
259 }
260 \endqml
261*/
262
263/*!
264 Constructs a null camera device
265*/
266QCameraDevice::QCameraDevice() = default;
267
268/*!
269 Constructs a copy of \a other.
270*/
271QCameraDevice::QCameraDevice(const QCameraDevice &other) = default;
272
273/*!
274 Destroys the QCameraDevice.
275*/
276QCameraDevice::~QCameraDevice() = default;
277
278/*!
279 Returns true if this \l QCameraDevice represents the same device as \a other.
280
281 Due to the behavior of the properties in QCameraDevice, two
282 QCameraDevice instances can be considered equal even if not
283 all the properties are equal.
284*/
285bool QCameraDevice::operator==(const QCameraDevice &other) const
286{
287 return id() == other.id();
288}
289
290/*!
291 Returns true if this QCameraDevice is null or invalid.
292*/
293bool QCameraDevice::isNull() const
294{
295 return !d;
296}
297
298/*!
299 \qmlproperty string QtMultimedia::cameraDevice::id
300
301 Holds the device id of the camera
302
303 This is a unique ID to identify the camera and may not be human-readable.
304*/
305
306/*!
307 \property QCameraDevice::id
308
309 Returns the device id of the camera
310
311 This is a unique ID to identify the camera and may not be human-readable.
312*/
313QByteArray QCameraDevice::id() const
314{
315 return d ? d->id : QByteArray();
316}
317
318/*!
319 \qmlproperty bool QtMultimedia::cameraDevice::isDefault
320
321 Is true if this is the default camera device.
322*/
323
324/*!
325 \property QCameraDevice::isDefault
326
327 Returns true if this is the default camera device.
328*/
329bool QCameraDevice::isDefault() const
330{
331 return d ? d->isDefault : false;
332}
333
334/*!
335 \since 6.7
336 \qmlproperty QtVideo::Rotation QtMultimedia::cameraDevice::correctionAngle
337
338 Returns the rotation angle needed to compensate for the physical camera rotation of the camera
339 compared to its native orientation. In other words, the property represents the clockwise angle
340 through which the output image needs to be rotated to be upright on the device screen in its
341 native orientation. Since \a correctionAngle is relative to the native orientation, this value
342 does not change with altering the device orientation (portrait/landscape). The correction angle
343 may be non-zero mostly on Android, where native and camera orientations are defined by the manufacturer.
344
345 \image camera_correctionAngle_90.png Example with 90 degrees \a correctionAngle
346*/
347
348/*!
349 \since 6.7
350 \property QCameraDevice::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*/
361QtVideo::Rotation QCameraDevice::correctionAngle() const
362{
363 return d ? QtVideo::Rotation(d->orientation) : QtVideo::Rotation::None;
364}
365
366/*!
367 \qmlproperty string QtMultimedia::cameraDevice::description
368
369 Holds a human readable name of the camera.
370
371 Use this string to present the device to the user.
372*/
373
374/*!
375 \property QCameraDevice::description
376
377 Returns the human-readable description of the camera.
378
379 Use this string to present the device to the user.
380*/
381QString QCameraDevice::description() const
382{
383 return d ? d->description : QString();
384}
385
386/*!
387 \enum QCameraDevice::Position
388
389 This enum specifies the physical position of the camera on the system hardware.
390
391 \value UnspecifiedPosition The camera position is unspecified or unknown.
392 \value BackFace The camera is on the back face of the system hardware. For example on a
393 mobile device, it means it is on the opposite side to that of the screen.
394 \value FrontFace The camera is on the front face of the system hardware. For example on a
395 mobile device, it means it is on the same side as that of the screen.
396 Front-facing cameras generate video frames with the property
397 \l QVideoFrame::mirrored set to \c true. This means that the presentation of these
398 frames is flipped around the vertical axis to display the video output as a mirror,
399 whereas recording only considers the transformations of the surface specified in
400 \l QVideoFrame::surfaceFormat.
401
402 \sa position()
403*/
404
405/*!
406 \qmlproperty enumeration QtMultimedia::cameraDevice::position
407
408 Returns the physical position of the camera on the hardware system.
409
410 The returned value can be one of the following:
411
412 \value cameraDevice.UnspecifiedPosition The camera position is unspecified or unknown.
413 \value cameraDevice.BackFace The camera is on the back face of the system hardware. For example on a
414 mobile device, it means it is on the opposite side to that of the screen.
415 \value cameraDevice.FrontFace The camera is on the front face of the system hardware. For example on a
416 mobile device, it means it is on the same side as that of the screen.
417 Preview of front-facing cameras is flipped around the vertical axis
418 to display the video output as a mirror, whereas this flipping is not
419 performed during recording.
420*/
421
422/*!
423 \property QCameraDevice::position
424
425 Returns the physical position of the camera on the hardware system.
426*/
427QCameraDevice::Position QCameraDevice::position() const
428{
429 return d ? d->position : QCameraDevice::UnspecifiedPosition;
430}
431
432/*!
433 Returns a list of resolutions that the camera can use to
434 capture still images.
435
436 \sa QImageCapture
437 */
438QList<QSize> QCameraDevice::photoResolutions() const
439{
440 return d ? d->photoResolutions : QList<QSize>{};
441}
442
443/*!
444 \qmlproperty CameraFormat QtMultimedia::cameraDevice::videoFormats
445
446 Holds the video formats supported by the camera.
447*/
448
449/*!
450 \property QCameraDevice::videoFormats
451
452 Returns the video formats supported by the camera.
453*/
454QList<QCameraFormat> QCameraDevice::videoFormats() const
455{
456 return d ? d->videoFormats : QList<QCameraFormat>{};
457}
458
459QCameraDevice::QCameraDevice(QCameraDevicePrivate *p)
460 : d(p)
461{}
462
463/*!
464 Sets the QCameraDevice object to be equal to \a other.
465*/
466QCameraDevice& QCameraDevice::operator=(const QCameraDevice& other) = default;
467
468/*!
469 \fn QCameraDevice::operator!=(const QCameraDevice &other) const
470
471 Returns true if this QCameraDevice does not represent the same
472 device as \a other.
473*/
474
475#ifndef QT_NO_DEBUG_STREAM
476QDebug operator<<(QDebug d, const QCameraFormat &format)
477{
478 if (format.isNull())
479 d.maybeSpace() << u"QCameraFormat(null)"_s;
480 else {
481 const char *pixelFormatCharPtr = QMetaEnum::fromType<QVideoFrameFormat::PixelFormat>()
482 .valueToKey(format.pixelFormat());
483 QString pixelFormatString = pixelFormatCharPtr
484 ? QString::fromUtf8(pixelFormatCharPtr)
485 : u"Invalid"_s;
486
487 d.maybeSpace()
488 << u"QCameraFormat(resolution=%1x%2, pixelFormat=%3, minFrameRate=%4, maxFrameRate=%5"_s
489 .arg(format.resolution().width())
490 .arg(format.resolution().height())
491 .arg(pixelFormatString)
492 .arg(format.minFrameRate())
493 .arg(format.maxFrameRate());
494 }
495
496 return d.space();
497}
498
499QDebug operator<<(QDebug d, const QCameraDevice &camera)
500{
501 d.maybeSpace() << u"QCameraDevice(name=%1, id=%2, position=%3)"_s
502 .arg(camera.description())
503 .arg(QLatin1StringView(camera.id()))
504 .arg(QLatin1StringView(
505 QMetaEnum::fromType<QCameraDevice::Position>().valueToKey(
506 camera.position())));
507 return d.space();
508}
509#endif
510
511QT_END_NAMESPACE
512
513#include "moc_qcameradevice.cpp"
Combined button and popup list for selecting options.
QDebug operator<<(QDebug d, const QCameraDevice &camera)
QDebug operator<<(QDebug dbg, const QFileInfo &fi)