Qt 6.x
The Qt SDK
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
cameraoverview.qdoc
Go to the documentation of this file.
1// Copyright (C) 2021 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/*!
5\page cameraoverview.html
6\title Camera Overview
7\brief Camera viewfinder, still image capture, and video recording.
8
9The Qt Multimedia API provides a number of camera related classes, so you
10can access images and videos from mobile device cameras or web cameras.
11There are both C++ and QML APIs for common tasks.
12
13\section1 Camera Features
14
15In order to use the camera classes, a quick overview of the way a camera
16works is needed. If you're already familiar with this, you can skip ahead to
17\l {camera-tldr}{Camera implementation details}.
18For a more detailed explanations of how a camera works, see the following YouTube
19clip.
20
21\youtube qS1FmgPVLqw
22
23\section2 The Lens Assembly
24
25At one end of the camera assembly is the lens assembly (one or
26more lenses, arranged to focus light onto the sensor). The lenses
27themselves can sometimes be moved to adjust things like focus and zoom. They
28might also be fixed in an arrangement for a good balance between maintaining
29focus and cost.
30
31\image how-focus-works.gif "An animation of how focus works"
32
33\image Zoom.gif "An animation of how zoom works."
34
35Some lens assemblies can automatically be adjusted so that
36an object at different distances from the camera can be kept in focus.
37This is usually done by measuring how sharp a particular area of the
38frame is, and then adjusting the lens assembly to find the peak sharpness. In
39some cases, the camera will always use the center of the frame for this.
40In other cases, a camera may also allow this target focus region to be specified.
41Some examples of this feature include:
42\list
43\li Face zoom: Using computer vision to detect and use one or more faces as the
44target.
45\li Touch to zoom: Enabling the user to manually select an area via the preview
46screen.
47\endlist
48
49\section2 The Sensor
50Once light arrives at the sensor, it gets converted into digital pixels.
51This process can depend on a number of things but ultimately comes down
52to two things:
53\list
54\li The length of time conversion is allowed to take. Also known as exposure
55time.
56\li How bright the light is.
57\endlist
58
59The longer a conversion is allowed to take, the better the resulting image
60quality. Using a flash can assist with letting more light hit the sensor,
61allowing it to convert pixels faster, giving better quality for the same
62amount of time. Conversely, allowing a longer conversion time can let you
63take photos in darker environments, \b{as long as the camera is steady}. If the
64camera moves while the sensor is recording, the resulting image is blurred.
65
66\section2 Image Processing
67After the image has been captured by the sensor, the camera firmware performs
68various image processing tasks on it to compensate for various sensor
69characteristics, current lighting, and desired image properties. Faster sensor
70pixel conversion times may introduce digital noise, so some amount of image
71processing can be done to remove this, based on the camera sensor settings.
72
73The color of the image can also be adjusted at this stage to compensate for
74different light sources - fluorescent lights and sunlight give very different
75appearances to the same object, so the image can be adjusted based on the
76white balance of the picture (due to the different color temperatures of the
77light sources).
78\image image_processing.png "5 examples of various image processing techniques."
79
80Some forms of "special effects" can also be performed at this stage. Black
81and white, sepia, or "negative" style images can be produced.
82
83\section2 Recording for Posterity
84Finally, once a perfectly focused, exposed and processed image has been
85created, it can be put to good use. Camera images can be further processed
86by application code (for example, to detect bar-codes, or to stitch together a
87panoramic image), or saved to a common format like JPEG, or used to create a movie.
88Many of these tasks have classes to assist them.
89
90\target camera-tldr
91\section1 Camera Implementation Details
92\section2 Detecting and Selecting a Camera
93
94Before using the camera APIs, you should check that a camera is available at
95runtime. If there is none available, you could disable camera related features
96in your application. To perform this check in C++, use the
97\l QMediaDevices::videoInputs() function, as shown in the example below:
98
99
100 \snippet multimedia-snippets/camera.cpp Camera overview check
101
102Access a camera using the \l QCamera class in C++ or the \l Camera
103type in QML.
104
105When multiple cameras are available, you can specify which one to use.
106
107In C++:
108
109 \snippet multimedia-snippets/camera.cpp Camera selection
110
111In QML, you can select the camera by setting the \l{Camera::cameraDevice} property.
112You can also select a camera by its physical position on the system rather than
113by camera info. This is useful on mobile devices, which often have a
114front-facing and a back-facing camera.
115
116In C++:
117
118 \snippet multimedia-snippets/camera.cpp Camera overview position
119
120
121In QML, you can set the \c Camera \l{Camera::cameraDevice}{cameraDevice} property.
122Available cameras can be retrieved with MediaDevices.videoInputs
123
124In QML:
125
126\qml
127Camera {
128 position: Camera.FrontFace
129}
130\endqml
131
132
133If both QCameraDevice and position aren't specified, the default camera
134will be used. On desktop platforms, the default camera is set by the
135user in the system settings. On a mobile device, the back-facing camera
136is usually the default camera. You can get the default camera with
137QMediaDevices::defaultVideoInput() or MediaDevices.defaultVideoInput
138in QML.
139
140\section2 Preview
141
142While not strictly necessary, it's often useful to be able to see
143what the camera is pointing at. This is known as a preview.
144
145Depending on whether you're using QML or C++, you can do this in multiple ways.
146In QML, you can use \l Camera and videoOutput together to monitor a
147captureSession.
148
149\qml
150Item {
151 VideoOutput {
152 id: output
153 anchors.fill: parent
154 }
155 CaptureSession {
156 videoOutput: output
157
158 Camera {
159 // You can adjust various settings in here
160 }
161 }
162}
163\endqml
164
165In C++, your choice depends on whether you are using widgets, or QGraphicsView.
166The \l QVideoWidget class is used in the widgets case, and \l QGraphicsVideoItem
167is useful for QGraphicsView.
168
169 \snippet multimedia-snippets/camera.cpp Camera overview viewfinder
170
171For advanced usage (like processing preview frames as they come, which enables
172detection of objects or patterns), you can also use your own QVideoSink and set
173that as the videoOutput for the QMediaCaptureSession. In this case, you will need to
174render the preview image yourself by processing the data received from the
175videoFrameChanged() signal.
176
177 \snippet multimedia-snippets/camera.cpp Camera overview surface
178
179On mobile devices, the preview image is by default oriented in the same way as the device.
180Thus, as the user rotates the device, the preview image will switch between portrait and
181landscape mode. Once you start recording, the orientation will be locked. To avoid a poor
182user experience, you should also lock the orientation of the applications user interface
183while recording. This can be achieved using the
184\l{QWindow::contentOrientation}{contentOrientation} property of QWindow.
185
186\section2 Still Images
187
188After setting up a viewfinder and finding something photogenic, to capture an
189image we need to initialize a new QImageCapture object. All that is then
190needed is to start the camera and capture the image.
191
192 \snippet multimedia-snippets/camera.cpp Camera overview capture
193
194\section2 Movies
195
196Previously we saw code that allowed the capture of a still image. Recording
197video requires the use of a \l QMediaRecorder object.
198
199To record video we need to create a camera object as before but this time as
200well as creating a viewfinder, we will also initialize a media recorder object.
201
202 \snippet multimedia-snippets/camera.cpp Camera overview movie
203
204Signals from the \e QMediaRecorder can be connected to slots to react to
205changes in the state of the encoding process or error events. Recording
206starts when \l QMediaRecorder::record() is called. This causes the signal
207\l{QMediaRecorder::}{recorderStateChanged()} to be emitted. Recording is
208controlled by the record(), stop(), and pause() slots of QMediaRecorder.
209
210\section2 Controlling the Imaging Pipeline
211
212Now that the basics of capturing images and movies are covered, there are a number
213of ways to control the imaging pipeline to implement some interesting techniques.
214As explained earlier, several physical and electronic elements combine to determine
215the final images, and you can control them with different classes.
216
217\section3 Focus and Zoom
218
219QCamera allows you to set the general focus policy by means of the
220enums for the \l {QCamera::FocusMode}{FocusMode}. \l {QCamera::FocusMode}{FocusMode}
221deals with settings such as \l {QCamera::FocusModeAuto},
222and \l {QCamera::FocusModeInfinity}.
223
224For camera hardware that supports it, \l QCamera::FocusModeAutoNear allows
225imaging of things that are close to the sensor. This is useful in applications
226like bar-code recognition, or business card scanning.
227
228In addition to focus, QCamera allows you to control any available zoom
229functionality using \l{QCamera::setZoomFactor}{setZoomFactor()} or
230\l{QCamera::zoomTo}{zoomTo()}. The
231available zoom range might be limited or entirely fixed to unity (1:1). The
232allowed range can be checked with \l{QCamera::minimumZoomFactor}{minimumZoomFactor()}
233and \l{QCamera::maximumZoomFactor}{maximumZoomFactor()}.
234
235\section3 Exposure, Shutter Speed and Flash
236
237There are a number of settings that affect the amount of light that hits the
238camera sensor, and hence the quality of the resulting image.
239
240The main settings for automatic image taking are the
241\l {QCamera::ExposureMode}{exposure mode} and \l {QCamera::FlashMode}{flash mode}.
242Several other settings (such as: ISO setting and exposure time) are usually
243managed automatically, but can also be overridden if desired.
244
245Finally, you can control the flash hardware (if present) using this class. In
246some cases the hardware may also double as a torch.
247
248\target camera_image_processing
249\section3 Image Processing
250
251The QCamera class lets you adjust the image processing part of the pipeline.
252These settings include:
253\list
254 \li \l {QCamera::WhiteBalanceMode}{white balance}
255 (also known as color temperature)
256\endlist
257
258Most cameras support automatic settings for all of these, so you shouldn't need
259to adjust them unless the user wants a specific setting.
260
261\section3 Canceling Asynchronous Operations
262
263Various operations, such as image capture and auto focusing, occur asynchronously.
264These operations can often be canceled by the start of a new operation, as long
265as this is supported by the camera.
266
267\section1 Examples
268
269There are both C++ and QML examples available.
270
271\section2 C++ Examples
272
273\annotatedlist camera_examples
274
275\section2 QML Examples
276
277\annotatedlist camera_examples_qml
278
279\section1 Reference Documentation
280
281\section2 C++ Classes
282
283\annotatedlist multimedia_camera
284
285\section2 QML Types
286
287\annotatedlist camera_qml
288
289*/