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 | |
4 | #include "qcameradevice_p.h" |
5 | |
6 | #include "qcamera_p.h" |
7 | |
8 | QT_BEGIN_NAMESPACE |
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{pixel format}{QVideoFrameFormat::PixelFormat}, resolution and a range of frame |
45 | rates. |
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 | */ |
58 | QCameraFormat::QCameraFormat() noexcept = default; |
59 | |
60 | /*! |
61 | Copy constructs a camera format from the \a other format. |
62 | */ |
63 | QCameraFormat::QCameraFormat(const QCameraFormat &other) noexcept = default; |
64 | |
65 | /*! |
66 | Assign \a other to this. |
67 | */ |
68 | QCameraFormat &QCameraFormat::operator=(const QCameraFormat &other) noexcept = default; |
69 | |
70 | /*! |
71 | Destructs the camera format object. |
72 | */ |
73 | QCameraFormat::~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 | */ |
101 | QVideoFrameFormat::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 | */ |
117 | QSize 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 | */ |
133 | float 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 | */ |
155 | float QCameraFormat::maxFrameRate() const noexcept |
156 | { |
157 | return d ? d->maxFrameRate : 0; |
158 | } |
159 | |
160 | /*! |
161 | \internal |
162 | */ |
163 | QCameraFormat::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 | */ |
171 | bool 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 | */ |
261 | QCameraDevice::QCameraDevice() = default; |
262 | |
263 | /*! |
264 | Constructs a copy of \a other. |
265 | */ |
266 | QCameraDevice::QCameraDevice(const QCameraDevice &other) = default; |
267 | |
268 | /*! |
269 | Destroys the QCameraDevice. |
270 | */ |
271 | QCameraDevice::~QCameraDevice() = default; |
272 | |
273 | /*! |
274 | Returns true if this QCameraDevice is equal to \a other. |
275 | */ |
276 | bool QCameraDevice::operator==(const QCameraDevice &other) const |
277 | { |
278 | if (d == other.d) |
279 | return true; |
280 | |
281 | if (!d || ! other.d) |
282 | return false; |
283 | |
284 | return (d->id == other.d->id |
285 | && d->description == other.d->description |
286 | && d->position == other.d->position); |
287 | } |
288 | |
289 | /*! |
290 | Returns true if this QCameraDevice is null or invalid. |
291 | */ |
292 | bool QCameraDevice::isNull() const |
293 | { |
294 | return !d; |
295 | } |
296 | |
297 | /*! |
298 | \qmlproperty string QtMultimedia::cameraDevice::id |
299 | |
300 | Holds he device id of the camera |
301 | |
302 | This is a unique ID to identify the camera and may not be human-readable. |
303 | */ |
304 | |
305 | /*! |
306 | \property QCameraDevice::id |
307 | |
308 | Returns the device id of the camera |
309 | |
310 | This is a unique ID to identify the camera and may not be human-readable. |
311 | */ |
312 | QByteArray QCameraDevice::id() const |
313 | { |
314 | return d ? d->id : QByteArray(); |
315 | } |
316 | |
317 | /*! |
318 | \qmlproperty bool QtMultimedia::cameraDevice::isDefault |
319 | |
320 | Is true if this is the default camera device. |
321 | */ |
322 | |
323 | /*! |
324 | \property QCameraDevice::isDefault |
325 | |
326 | Returns true if this is the default camera device. |
327 | */ |
328 | bool QCameraDevice::isDefault() const |
329 | { |
330 | return d ? d->isDefault : false; |
331 | } |
332 | |
333 | /*! |
334 | \since 6.7 |
335 | \qmlproperty QtVideo::Rotation QtMultimedia::cameraDevice::correctionAngle |
336 | |
337 | Returns the rotation angle needed to compensate for the physical camera rotation of the camera |
338 | compared to its native orientation. In other words, the property represents the clockwise angle |
339 | through which the output image needs to be rotated to be upright on the device screen in its |
340 | native orientation. Since \a correctionAngle is relative to the native orientation, this value |
341 | does not change with altering the device orientation (portrait/landscape). The correction angle |
342 | may be non-zero mostly on Android, where native and camera orientations are defined by the manufacturer. |
343 | |
344 | \image camera_correctionAngle_90.png Example with 90 degrees \a correctionAngle |
345 | */ |
346 | |
347 | /*! |
348 | \since 6.7 |
349 | \property QCameraDevice::correctionAngle |
350 | |
351 | Returns the rotation angle needed to compensate for the physical camera rotation of the camera |
352 | compared to its native orientation. In other words, the property represents the clockwise angle |
353 | through which the output image needs to be rotated to be upright on the device screen in its |
354 | native orientation. Since \a correctionAngle is relative to the native orientation, this value |
355 | does not change with altering the device orientation (portrait/landscape). The correction angle |
356 | may be non-zero mostly on Android, where native and camera orientations are defined by the manufacturer. |
357 | |
358 | \image camera_correctionAngle_90.png Example with 90 degrees \a correctionAngle |
359 | */ |
360 | QtVideo::Rotation QCameraDevice::correctionAngle() const |
361 | { |
362 | return d ? QtVideo::Rotation(d->orientation) : QtVideo::Rotation::None; |
363 | } |
364 | |
365 | /*! |
366 | \qmlproperty string QtMultimedia::cameraDevice::description |
367 | |
368 | Holds a human readable name of the camera. |
369 | |
370 | Use this string to present the device to the user. |
371 | */ |
372 | |
373 | /*! |
374 | \property QCameraDevice::description |
375 | |
376 | Returns the human-readable description of the camera. |
377 | |
378 | Use this string to present the device to the user. |
379 | */ |
380 | QString QCameraDevice::description() const |
381 | { |
382 | return d ? d->description : QString(); |
383 | } |
384 | |
385 | /*! |
386 | \enum QCameraDevice::Position |
387 | |
388 | This enum specifies the physical position of the camera on the system hardware. |
389 | |
390 | \value UnspecifiedPosition The camera position is unspecified or unknown. |
391 | \value BackFace The camera is on the back face of the system hardware. For example on a |
392 | mobile device, it means it is on the opposite side to that of the screen. |
393 | \value FrontFace The camera is on the front face of the system hardware. For example on a |
394 | mobile device, it means it is on the same side as that of the screen. |
395 | Front-facing cameras generate video frames with the property |
396 | \l QVideoFrame::mirrored set to \c true. This means that the presentation of these |
397 | frames is flipped around the vertical axis to display the video output as a mirror, |
398 | whereas recording only considers the transformations of the surface specified in |
399 | \l QVideoFrame::surfaceFormat. |
400 | |
401 | \sa position() |
402 | */ |
403 | |
404 | /*! |
405 | \qmlproperty enumeration QtMultimedia::cameraDevice::position |
406 | |
407 | Returns the physical position of the camera on the hardware system. |
408 | |
409 | The returned value can be one of the following: |
410 | |
411 | \value cameraDevice.UnspecifiedPosition The camera position is unspecified or unknown. |
412 | \value cameraDevice.BackFace The camera is on the back face of the system hardware. For example on a |
413 | mobile device, it means it is on the opposite side to that of the screen. |
414 | \value cameraDevice.FrontFace The camera is on the front face of the system hardware. For example on a |
415 | mobile device, it means it is on the same side as that of the screen. |
416 | Preview of front-facing cameras is flipped around the vertical axis |
417 | to display the video output as a mirror, whereas this flipping is not |
418 | performed during recording. |
419 | */ |
420 | |
421 | /*! |
422 | \property QCameraDevice::position |
423 | |
424 | Returns the physical position of the camera on the hardware system. |
425 | */ |
426 | QCameraDevice::Position QCameraDevice::position() const |
427 | { |
428 | return d ? d->position : QCameraDevice::UnspecifiedPosition; |
429 | } |
430 | |
431 | /*! |
432 | Returns a list of resolutions that the camera can use to |
433 | capture still images. |
434 | |
435 | \sa QImageCapture |
436 | */ |
437 | QList<QSize> QCameraDevice::photoResolutions() const |
438 | { |
439 | return d ? d->photoResolutions : QList<QSize>{}; |
440 | } |
441 | |
442 | /*! |
443 | \qmlproperty CameraFormat QtMultimedia::cameraDevice::videoFormats |
444 | |
445 | Holds the video formats supported by the camera. |
446 | */ |
447 | |
448 | /*! |
449 | \property QCameraDevice::videoFormats |
450 | |
451 | Returns the video formats supported by the camera. |
452 | */ |
453 | QList<QCameraFormat> QCameraDevice::videoFormats() const |
454 | { |
455 | return d ? d->videoFormats : QList<QCameraFormat>{}; |
456 | } |
457 | |
458 | QCameraDevice::QCameraDevice(QCameraDevicePrivate *p) |
459 | : d(p) |
460 | {} |
461 | |
462 | /*! |
463 | Sets the QCameraDevice object to be equal to \a other. |
464 | */ |
465 | QCameraDevice& QCameraDevice::operator=(const QCameraDevice& other) = default; |
466 | |
467 | /*! |
468 | \fn QCameraDevice::operator!=(const QCameraDevice &other) const |
469 | |
470 | Returns true if this QCameraDevice is different from \a other. |
471 | */ |
472 | |
473 | #ifndef QT_NO_DEBUG_STREAM |
474 | QDebug operator<<(QDebug d, const QCameraDevice &camera) |
475 | { |
476 | d.maybeSpace() << QStringLiteral("QCameraDevice(name=%1, id=%2, position=%3)" ) |
477 | .arg(a: camera.description()) |
478 | .arg(a: QLatin1StringView(camera.id())) |
479 | .arg(a: QLatin1StringView( |
480 | QMetaEnum::fromType<QCameraDevice::Position>().valueToKey( |
481 | value: camera.position()))); |
482 | return d.space(); |
483 | } |
484 | #endif |
485 | |
486 | QT_END_NAMESPACE |
487 | |
488 | #include "moc_qcameradevice.cpp" |
489 | |