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
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.
19QCamera *camera = nullptr;
23
24//! [Camera overview check]
26{
27 return !QMediaDevices::videoInputs().isEmpty();
28}
29//! [Camera overview check]
30
32{
33//! [Camera listing]
34const QList<QCameraDevice> cameras = QMediaDevices::videoInputs();
35for (const QCameraDevice &cameraDevice : cameras)
36 qDebug() << cameraDevice.description();
37//! [Camera listing]
38}
39
41{
42//! [Camera selection]
43const QList<QCameraDevice> cameras = QMediaDevices::videoInputs();
44for (const QCameraDevice &cameraDevice : cameras) {
45 if (cameraDevice.description() == "mycamera")
46 camera = new QCamera(cameraDevice);
47}
48//! [Camera selection]
49}
50
52{
53//! [Camera info]
54QCamera myCamera;
55QCameraDevice cameraDevice = myCamera.cameraDevice();
56
57if (cameraDevice.position() == QCameraDevice::FrontFace)
58 qDebug() << "The camera is on the front face of the hardware system.";
59else if (cameraDevice.position() == QCameraDevice::BackFace)
60 qDebug() << "The camera is on the back face of the hardware system.";
61//! [Camera info]
62}
63
65{
66//! [Camera overview position]
67camera = new QCamera(QCameraDevice::FrontFace);
68//! [Camera overview position]
69}
70
72{
73//! [Camera overview viewfinder]
74QMediaCaptureSession captureSession;
75camera = new QCamera;
76captureSession.setCamera(camera);
77viewfinder = new QVideoWidget;
78captureSession.setVideoOutput(viewfinder);
79viewfinder->show();
80
81camera->start(); // to start the camera
82//! [Camera overview viewfinder]
83}
84
86{
87QVideoSink *mySink = nullptr;
88//! [Camera overview surface]
89QMediaCaptureSession captureSession;
90camera = new QCamera;
91captureSession.setCamera(camera);
92mySink = new QVideoSink;
93captureSession.setVideoOutput(mySink);
94
95camera->start();
96// MyVideoSink::setVideoFrame(..) will be called with video frames
97//! [Camera overview surface]
98}
99
101{
102//! [Camera overview capture]
103QMediaCaptureSession captureSession;
104camera = new QCamera;
105captureSession.setCamera(camera);
106imageCapture = new QImageCapture;
107captureSession.setImageCapture(imageCapture);
108
109camera->start(); // Viewfinder frames start flowing
110
111// on shutter button pressed
112imageCapture->capture();
113//! [Camera overview capture]
114}
115
117{
118//! [Camera overview movie]
119QMediaCaptureSession captureSession;
120camera = new QCamera;
121captureSession.setCamera(camera);
122recorder = new QMediaRecorder;
123captureSession.setRecorder(recorder);
124
125camera->start();
126
127// setup output format for the recorder
128QMediaFormat format(QMediaFormat::MPEG4);
129format.setVideoCodec(QMediaFormat::VideoCodec::H264);
130format.setAudioCodec(QMediaFormat::AudioCodec::MP3);
131recorder->setMediaFormat(format);
132
133// on shutter button pressed
134recorder->record();
135
136// sometime later, or on another press
137recorder->stop();
138//! [Camera overview movie]
139}
140
142{
143//! [Camera]
144QMediaCaptureSession captureSession;
145camera = new QCamera;
146captureSession.setCamera(camera);
147
148viewfinder = new QVideoWidget;
149viewfinder->show();
150captureSession.setVideoOutput(viewfinder);
151
152imageCapture = new QImageCapture;
153captureSession.setImageCapture(imageCapture);
154
155camera->start();
156//! [Camera]
157
158//! [Camera keys]
159// on shutter button pressed
160imageCapture->capture();
161//! [Camera keys]
162}
163
165{
166//! [Camera zoom]
167camera->setZoomFactor(camera->maximumZoomFactor()); // zoom in as much as possible
168//! [Camera zoom]
169}
170
172{
173//! [Camera image whitebalance]
174camera->setWhiteBalanceMode(QCamera::WhiteBalanceManual);
175camera->setColorTemperature(5600);
176//! [Camera image whitebalance]
177}
178
180{
181//! [Camera permission]
182qApp->requestPermission(QCameraPermission{}, [](const QPermission &permission) {
183 if (permission.status() == Qt::PermissionStatus::Granted)
184 camera->setActive(true);
185});
186//! [Camera permission]
187}
void camerafocus()
QVideoWidget * viewfinder
void camera_info()
void overview_camera_by_position()
void overview_surface()
void camera_selection()
void overview_still()
QCamera * camera
void camera_listing()
[Camera overview check]
QImageCapture * imageCapture
void camerapermission()
void cameraimageprocessing()
bool checkCameraAvailability()
[Camera overview check]
void image_capture()
void overview_movie()
QMediaRecorder * recorder
void overview_viewfinder()