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