Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
camerasnippets.cpp
Go to the documentation of this file.
1
// Copyright (C) 2026 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4
#
include
<
QCamera
>
5
#
include
<
QCameraDevice
>
6
#
include
<
QImageCapture
>
7
#
include
<
QMediaCaptureSession
>
8
#
include
<
QMediaDevices
>
9
#
include
<
QMediaFormat
>
10
#
include
<
QMediaRecorder
>
11
#
include
<
QPermission
>
12
#
include
<
QVideoSink
>
13
#
include
<
QVideoWidget
>
14
15
#
include
<
QtCore
/
qcoreapplication
.
h
>
16
#
include
<
QtCore
/
qdebug
.
h
>
17
18
// Globals so that the snippet bodies can stay focused on the relevant API.
19
QCamera
*
camera
=
nullptr
;
20
QImageCapture
*
imageCapture
=
nullptr
;
21
QMediaRecorder
*
recorder
=
nullptr
;
22
QVideoWidget
*
viewfinder
=
nullptr
;
23
24
//! [Camera overview check]
25
bool
checkCameraAvailability
()
26
{
27
return
!QMediaDevices::videoInputs().isEmpty();
28
}
29
//! [Camera overview check]
30
31
void
camera_listing
()
32
{
33
//! [Camera listing]
34
const
QList<QCameraDevice> cameras = QMediaDevices::videoInputs();
35
for
(
const
QCameraDevice &cameraDevice : cameras)
36
qDebug() << cameraDevice.description();
37
//! [Camera listing]
38
}
39
40
void
camera_selection
()
41
{
42
//! [Camera selection]
43
const
QList<QCameraDevice> cameras = QMediaDevices::videoInputs();
44
for
(
const
QCameraDevice &cameraDevice : cameras) {
45
if
(cameraDevice.description() ==
"mycamera"
)
46
camera =
new
QCamera(cameraDevice);
47
}
48
//! [Camera selection]
49
}
50
51
void
camera_info
()
52
{
53
//! [Camera info]
54
QCamera myCamera;
55
QCameraDevice cameraDevice = myCamera.cameraDevice();
56
57
if
(cameraDevice.position() == QCameraDevice::FrontFace)
58
qDebug() <<
"The camera is on the front face of the hardware system."
;
59
else
if
(cameraDevice.position() == QCameraDevice::BackFace)
60
qDebug() <<
"The camera is on the back face of the hardware system."
;
61
//! [Camera info]
62
}
63
64
void
overview_camera_by_position
()
65
{
66
//! [Camera overview position]
67
camera
=
new
QCamera(QCameraDevice::FrontFace);
68
//! [Camera overview position]
69
}
70
71
void
overview_viewfinder
()
72
{
73
//! [Camera overview viewfinder]
74
QMediaCaptureSession captureSession;
75
camera
=
new
QCamera;
76
captureSession.setCamera(camera);
77
viewfinder
=
new
QVideoWidget;
78
captureSession.setVideoOutput(viewfinder);
79
viewfinder
->show();
80
81
camera
->start();
// to start the camera
82
//! [Camera overview viewfinder]
83
}
84
85
void
overview_surface
()
86
{
87
QVideoSink *mySink =
nullptr
;
88
//! [Camera overview surface]
89
QMediaCaptureSession captureSession;
90
camera
=
new
QCamera;
91
captureSession.setCamera(camera);
92
mySink =
new
QVideoSink;
93
captureSession.setVideoOutput(mySink);
94
95
camera
->start();
96
// MyVideoSink::setVideoFrame(..) will be called with video frames
97
//! [Camera overview surface]
98
}
99
100
void
overview_still
()
101
{
102
//! [Camera overview capture]
103
QMediaCaptureSession captureSession;
104
camera
=
new
QCamera;
105
captureSession.setCamera(camera);
106
imageCapture
=
new
QImageCapture;
107
captureSession.setImageCapture(imageCapture);
108
109
camera
->start();
// Viewfinder frames start flowing
110
111
// on shutter button pressed
112
imageCapture
->capture();
113
//! [Camera overview capture]
114
}
115
116
void
overview_movie
()
117
{
118
//! [Camera overview movie]
119
QMediaCaptureSession captureSession;
120
camera
=
new
QCamera;
121
captureSession.setCamera(camera);
122
recorder
=
new
QMediaRecorder;
123
captureSession.setRecorder(recorder);
124
125
camera
->start();
126
127
// setup output format for the recorder
128
QMediaFormat format(QMediaFormat::MPEG4);
129
format.setVideoCodec(QMediaFormat::VideoCodec::H264);
130
format.setAudioCodec(QMediaFormat::AudioCodec::MP3);
131
recorder
->setMediaFormat(format);
132
133
// on shutter button pressed
134
recorder
->record();
135
136
// sometime later, or on another press
137
recorder
->stop();
138
//! [Camera overview movie]
139
}
140
141
void
image_capture
()
142
{
143
//! [Camera]
144
QMediaCaptureSession captureSession;
145
camera
=
new
QCamera;
146
captureSession.setCamera(camera);
147
148
viewfinder
=
new
QVideoWidget;
149
viewfinder
->show();
150
captureSession.setVideoOutput(viewfinder);
151
152
imageCapture
=
new
QImageCapture;
153
captureSession.setImageCapture(imageCapture);
154
155
camera
->start();
156
//! [Camera]
157
158
//! [Camera keys]
159
// on shutter button pressed
160
imageCapture
->capture();
161
//! [Camera keys]
162
}
163
164
void
camerafocus
()
165
{
166
//! [Camera zoom]
167
camera
->setZoomFactor(camera->maximumZoomFactor());
// zoom in as much as possible
168
//! [Camera zoom]
169
}
170
171
void
cameraimageprocessing
()
172
{
173
//! [Camera image whitebalance]
174
camera
->setWhiteBalanceMode(QCamera::WhiteBalanceManual);
175
camera
->setColorTemperature(5600);
176
//! [Camera image whitebalance]
177
}
178
179
void
camerapermission
()
180
{
181
//! [Camera permission]
182
qApp->requestPermission(QCameraPermission{}, [](
const
QPermission &permission) {
183
if
(permission.status() == Qt::PermissionStatus::Granted)
184
camera->setActive(
true
);
185
});
186
//! [Camera permission]
187
}
camerafocus
void camerafocus()
Definition
camerasnippets.cpp:164
viewfinder
QVideoWidget * viewfinder
Definition
camerasnippets.cpp:22
camera_info
void camera_info()
Definition
camerasnippets.cpp:51
overview_camera_by_position
void overview_camera_by_position()
Definition
camerasnippets.cpp:64
overview_surface
void overview_surface()
Definition
camerasnippets.cpp:85
camera_selection
void camera_selection()
Definition
camerasnippets.cpp:40
overview_still
void overview_still()
Definition
camerasnippets.cpp:100
camera
QCamera * camera
Definition
camerasnippets.cpp:19
camera_listing
void camera_listing()
[Camera overview check]
Definition
camerasnippets.cpp:31
imageCapture
QImageCapture * imageCapture
Definition
camerasnippets.cpp:20
camerapermission
void camerapermission()
Definition
camerasnippets.cpp:179
cameraimageprocessing
void cameraimageprocessing()
Definition
camerasnippets.cpp:171
checkCameraAvailability
bool checkCameraAvailability()
[Camera overview check]
Definition
camerasnippets.cpp:25
image_capture
void image_capture()
Definition
camerasnippets.cpp:141
overview_movie
void overview_movie()
Definition
camerasnippets.cpp:116
recorder
QMediaRecorder * recorder
Definition
camerasnippets.cpp:21
overview_viewfinder
void overview_viewfinder()
Definition
camerasnippets.cpp:71
qtmultimedia
src
multimedia
doc
snippets
multimedia-snippets
camerasnippets.cpp
Generated on
for Qt by
1.18.0