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_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 \include qdevice-retains-properties.qdocinc {retains-properties} {QCameraDevice} {QMediaDevices}
204
205 This example prints the name of all available cameras:
206
207 \snippet multimedia-snippets/camerasnippets.cpp Camera listing
208
209 A QCameraDevice can be used to construct a QCamera. The following example
210 instantiates a QCamera whose camera device is named \c {mycamera}:
211
212 \snippet multimedia-snippets/camerasnippets.cpp Camera selection
213
214 You can also use QCameraDevice to get general information about a camera
215 device such as description and physical position on the system.
216
217 \snippet multimedia-snippets/camerasnippets.cpp Camera info
218
219 \sa QCamera
220*/
221
222/*!
223 \qmlvaluetype cameraDevice
224 \ingroup qmlvaluetypes
225 \inqmlmodule QtMultimedia
226 \since 6.2
227 \nativetype QCameraDevice
228 \brief Describes a camera device.
229 \ingroup multimedia_qml
230 \ingroup multimedia_video_qml
231
232 The cameraDevice value type describes the properties of a camera device that
233 is connected to the system.
234
235 \include qdevice-retains-properties.qdocinc {retains-properties} {cameraDevice} {MediaDevices}
236
237 Two cameraDevice instances can be compared for equality. They are
238 considered equal if they represent the same physical device,
239 regardless of whether their properties are equal or up-to-date.
240
241 The list of camera devices can be queried from the \l{MediaDevices}
242 type. To select a certain camera device set it as the device
243 on \l{Camera}.
244
245 \qml
246 CaptureSession {
247 camera: Camera {
248 cameraDevice: mediaDevices.defaultVideoInput
249 }
250 }
251 MediaDevices {
252 id: mediaDevices
253 }
254 \endqml
255*/
256
257/*!
258 Constructs a null camera device
259*/
260QCameraDevice::QCameraDevice() = default;
261
262/*!
263 Constructs a copy of \a other.
264*/
265QCameraDevice::QCameraDevice(const QCameraDevice &other) = default;
266
267/*!
268 Destroys the QCameraDevice.
269*/
270QCameraDevice::~QCameraDevice() = default;
271
272/*!
273 Returns true if this \l QCameraDevice represents the same device as \a other.
274
275 Due to the behavior of the properties in QCameraDevice, two
276 QCameraDevice instances can be considered equal even if not
277 all the properties are equal.
278*/
279bool QCameraDevice::operator==(const QCameraDevice &other) const
280{
281 return id() == other.id();
282}
283
284/*!
285 Returns true if this QCameraDevice is null or invalid.
286*/
287bool QCameraDevice::isNull() const
288{
289 return !d;
290}
291
292/*!
293 \qmlproperty string QtMultimedia::cameraDevice::id
294
295 Holds the device id of the camera
296
297 This is a unique ID to identify the camera and may not be human-readable.
298*/
299
300/*!
301 \property QCameraDevice::id
302
303 Returns the device id of the camera
304
305 This is a unique ID to identify the camera and may not be human-readable.
306*/
307QByteArray QCameraDevice::id() const
308{
309 return d ? d->id : QByteArray();
310}
311
312/*!
313 \qmlproperty bool QtMultimedia::cameraDevice::isDefault
314
315 Is true if this is the default camera device.
316*/
317
318/*!
319 \property QCameraDevice::isDefault
320
321 Returns true if this is the default camera device.
322*/
323bool QCameraDevice::isDefault() const
324{
325 return d ? d->isDefault : false;
326}
327
328/*!
329 \since 6.7
330 \qmlproperty QtVideo::Rotation QtMultimedia::cameraDevice::correctionAngle
331
332 Returns the rotation angle needed to compensate for the physical camera rotation of the camera
333 compared to its native orientation. In other words, the property represents the clockwise angle
334 through which the output image needs to be rotated to be upright on the device screen in its
335 native orientation. Since \a correctionAngle is relative to the native orientation, this value
336 does not change with altering the device orientation (portrait/landscape). The correction angle
337 may be non-zero mostly on Android, where native and camera orientations are defined by the manufacturer.
338
339 \image camera_correctionAngle_90.png Example with 90 degrees \a correctionAngle
340*/
341
342/*!
343 \since 6.7
344 \property QCameraDevice::correctionAngle
345
346 Returns the rotation angle needed to compensate for the physical camera rotation of the camera
347 compared to its native orientation. In other words, the property represents the clockwise angle
348 through which the output image needs to be rotated to be upright on the device screen in its
349 native orientation. Since \a correctionAngle is relative to the native orientation, this value
350 does not change with altering the device orientation (portrait/landscape). The correction angle
351 may be non-zero mostly on Android, where native and camera orientations are defined by the manufacturer.
352
353 \image camera_correctionAngle_90.png Example with 90 degrees \a correctionAngle
354*/
355QtVideo::Rotation QCameraDevice::correctionAngle() const
356{
357 return d ? QtVideo::Rotation(d->orientation) : QtVideo::Rotation::None;
358}
359
360/*!
361 \qmlproperty string QtMultimedia::cameraDevice::description
362
363 Holds a human readable name of the camera.
364
365 Use this string to present the device to the user.
366*/
367
368/*!
369 \property QCameraDevice::description
370
371 Returns the human-readable description of the camera.
372
373 Use this string to present the device to the user.
374*/
375QString QCameraDevice::description() const
376{
377 return d ? d->description : QString();
378}
379
380/*!
381 \enum QCameraDevice::Position
382
383 This enum specifies the physical position of the camera on the system hardware.
384
385 \value UnspecifiedPosition The camera position is unspecified or unknown.
386 \value BackFace The camera is on the back face of the system hardware. For example on a
387 mobile device, it means it is on the opposite side to that of the screen.
388 \value FrontFace The camera is on the front face of the system hardware. For example on a
389 mobile device, it means it is on the same side as that of the screen.
390 Front-facing cameras generate video frames with the property
391 \l QVideoFrame::mirrored set to \c true. This means that the presentation of these
392 frames is flipped around the vertical axis to display the video output as a mirror,
393 whereas recording only considers the transformations of the surface specified in
394 \l QVideoFrame::surfaceFormat.
395
396 \sa position()
397*/
398
399/*!
400 \qmlproperty enumeration QtMultimedia::cameraDevice::position
401
402 Returns the physical position of the camera on the hardware system.
403
404 The returned value can be one of the following:
405
406 \value cameraDevice.UnspecifiedPosition The camera position is unspecified or unknown.
407 \value cameraDevice.BackFace The camera is on the back face of the system hardware. For example on a
408 mobile device, it means it is on the opposite side to that of the screen.
409 \value cameraDevice.FrontFace The camera is on the front face of the system hardware. For example on a
410 mobile device, it means it is on the same side as that of the screen.
411 Preview of front-facing cameras is flipped around the vertical axis
412 to display the video output as a mirror, whereas this flipping is not
413 performed during recording.
414*/
415
416/*!
417 \property QCameraDevice::position
418
419 Returns the physical position of the camera on the hardware system.
420*/
421QCameraDevice::Position QCameraDevice::position() const
422{
423 return d ? d->position : QCameraDevice::UnspecifiedPosition;
424}
425
426/*!
427 Returns a list of resolutions that the camera can use to
428 capture still images.
429
430 \sa QImageCapture
431 */
432QList<QSize> QCameraDevice::photoResolutions() const
433{
434 return d ? d->photoResolutions : QList<QSize>{};
435}
436
437/*!
438 \qmlproperty CameraFormat QtMultimedia::cameraDevice::videoFormats
439
440 Holds the video formats supported by the camera.
441*/
442
443/*!
444 \property QCameraDevice::videoFormats
445
446 Returns the video formats supported by the camera.
447*/
448QList<QCameraFormat> QCameraDevice::videoFormats() const
449{
450 return d ? d->videoFormats : QList<QCameraFormat>{};
451}
452
453QCameraDevice::QCameraDevice(QCameraDevicePrivate *p)
454 : d(p)
455{}
456
457/*!
458 Sets the QCameraDevice object to be equal to \a other.
459*/
460QCameraDevice& QCameraDevice::operator=(const QCameraDevice& other) = default;
461
462/*!
463 \fn QCameraDevice::operator!=(const QCameraDevice &other) const
464
465 Returns true if this QCameraDevice does not represent the same
466 device as \a other.
467*/
468
469#ifndef QT_NO_DEBUG_STREAM
470QDebug operator<<(QDebug d, const QCameraFormat &format)
471{
472 if (format.isNull())
473 d.maybeSpace() << u"QCameraFormat(null)"_s;
474 else {
475 const char *pixelFormatCharPtr = QMetaEnum::fromType<QVideoFrameFormat::PixelFormat>()
476 .valueToKey(format.pixelFormat());
477 QString pixelFormatString = pixelFormatCharPtr
478 ? QString::fromUtf8(pixelFormatCharPtr)
479 : u"Invalid"_s;
480
481 d.maybeSpace()
482 << u"QCameraFormat(resolution=%1x%2, pixelFormat=%3, minFrameRate=%4, maxFrameRate=%5"_s
483 .arg(format.resolution().width())
484 .arg(format.resolution().height())
485 .arg(pixelFormatString)
486 .arg(format.minFrameRate())
487 .arg(format.maxFrameRate());
488 }
489
490 return d.space();
491}
492
493QDebug operator<<(QDebug d, const QCameraDevice &camera)
494{
495 d.maybeSpace() << u"QCameraDevice(name=%1, id=%2, position=%3)"_s
496 .arg(camera.description())
497 .arg(QLatin1StringView(camera.id()))
498 .arg(QLatin1StringView(
499 QMetaEnum::fromType<QCameraDevice::Position>().valueToKey(
500 camera.position())));
501 return d.space();
502}
503#endif
504
505QT_END_NAMESPACE
506
507#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)