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
camera.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4/* Camera snippets */
5
6#include "qcamera.h"
11#include "qimagecapture.h"
12#include "qvideosink.h"
13#include <QtMultimediaWidgets/qvideowidget.h>
14#include <QtGui/qscreen.h>
15#include <QtGui/qguiapplication.h>
16#include <QtGui/qimage.h>
17
18/* Globals so that everything is consistent. */
19QCamera *camera = 0;
20QMediaRecorder *recorder = 0;
21QImageCapture *imageCapture = 0;
23
24//! [Camera overview check]
26{
27 if (QMediaDevices::videoInputs().count() > 0)
28 return true;
29 else
30 return false;
31}
32//! [Camera overview check]
33
35{
36 //! [Camera overview viewfinder]
37 QMediaCaptureSession captureSession;
38 camera = new QCamera;
39 captureSession.setCamera(camera);
40 viewfinder = new QVideoWidget;
41 captureSession.setVideoOutput(viewfinder);
42 viewfinder->show();
43
44 camera->start(); // to start the camera
45 //! [Camera overview viewfinder]
46}
47
49{
50 //! [Camera overview position]
51 camera = new QCamera(QCameraDevice::FrontFace);
52 //! [Camera overview position]
53}
54
55// -.-
57{
58 QVideoSink *mySink;
59 //! [Camera overview surface]
60 QMediaCaptureSession captureSession;
61 camera = new QCamera;
62 captureSession.setCamera(camera);
63 mySink = new QVideoSink;
64 captureSession.setVideoOutput(mySink);
65
66 camera->start();
67 // MyVideoSink::setVideoFrame(..) will be called with video frames
68 //! [Camera overview surface]
69}
70
72{
73 QCamera camera;
74
75 //! [Camera overview viewfinder orientation]
76 // Assuming a QImage has been created from the QVideoFrame that needs to be presented
77 QImage videoFrame;
78 QCameraDevice cameraDevice(camera); // needed to get the camera sensor position and orientation
79
80 // Get the current display orientation
81 const QScreen *screen = QGuiApplication::primaryScreen();
82 const int screenAngle = screen->angleBetween(screen->nativeOrientation(), screen->orientation());
83
84 int rotation;
85 if (cameraDevice.position() == QCameraDevice::BackFace) {
86 rotation = (cameraDevice.orientation() - screenAngle) % 360;
87 } else {
88 // Front position, compensate the mirror
89 rotation = (360 - cameraDevice.orientation() + screenAngle) % 360;
90 }
91
92 // Rotate the frame so it always shows in the correct orientation
93 videoFrame = videoFrame.transformed(QTransform().rotate(rotation));
94 //! [Camera overview viewfinder orientation]
95}
96
98{
99 //! [Camera overview capture]
100 QMediaCaptureSession captureSession;
101 camera = new QCamera;
102 captureSession.setCamera(camera);
103 imageCapture = new QImageCapture;
104 captureSession.setImageCapture(imageCapture);
105
106 camera->start(); // Viewfinder frames start flowing
107
108 //on shutter button pressed
109 imageCapture->capture();
110 //! [Camera overview capture]
111}
112
114{
115 //! [Camera overview movie]
116 QMediaCaptureSession captureSession;
117 camera = new QCamera;
118 captureSession.setCamera(camera);
119 recorder = new QMediaRecorder(camera);
120 captureSession.setRecorder(recorder);
121
122 camera->start();
123
124 // setup output format for the recorder
125 QMediaFormat format(QMediaFormat::MPEG4);
126 format.setVideoCodec(QMediaRecorder::VideoCodec::H264);
127 format.setAudioCodec(QMediaRecorder::AudioCodec::MP3);
128 recorder->setMediaFormat(settings);
129
130 //on shutter button pressed
131 recorder->record();
132
133 // sometime later, or on another press
134 recorder->stop();
135 //! [Camera overview movie]
136}
137
139{
140 //! [Camera listing]
141 const QList<QCameraDevice> cameras = QMediaDevices::videoInputs();
142 for (const QCameraDevice &cameraDevice : cameras)
143 qDebug() << cameraDevice.description();
144 //! [Camera listing]
145}
146
148{
149 //! [Camera selection]
150 const QList<QCameraDevice> cameras = QMediaDevices::videoInputs();
151 for (const QCameraDevice &cameraDevice : cameras) {
152 if (cameraDevice.description() == "mycamera")
153 camera = new QCamera(cameraDevice);
154 }
155 //! [Camera selection]
156}
157
159{
160 //! [Camera info]
161 QCamera myCamera;
162 QCameraDevice cameraDevice = camera->cameraDevice();
163
164 if (cameraDevice.position() == QCameraDevice::FrontFace)
165 qDebug() << "The camera is on the front face of the hardware system.";
166 else if (cameraDevice.position() == QCameraDevice::BackFace)
167 qDebug() << "The camera is on the back face of the hardware system.";
168 //! [Camera info]
169}
170
172{
173 //! [Camera]
174 QMediaCaptureSession captureSession;
175 camera = new QCamera;
176 captureSession.setCamera(camera);
177
178 viewfinder = new QVideoWidget();
179 viewfinder->show();
180 captureSession.setVideoOutput(viewfinder);
181
182 imageCapture = new QImageCapture(camera);
183 captureSession.setImageCapture(imageCapture);
184
185 camera->start();
186 //! [Camera]
187
188 //! [Camera keys]
189 //on shutter button pressed
190 imageCapture->capture();
191 //! [Camera keys]
192}
193
195{
196 camera = new QCamera;
197 //! [Camera image whitebalance]
198 camera->setWhiteBalanceMode(QCamera::WhiteBalanceFluorescent);
199 //! [Camera image whitebalance]
200}
201
203{
204 //! [Camera custom focus]
205 camera->setFocusPointMode(QCamera::FocusModeManual);
206 camera->setCustomFocusPoint(QPointF(0.25f, 0.75f)); // A point near the bottom left, 25% away from the corner, near that shiny vase
207 //! [Camera custom focus]
208
209 //! [Camera zoom]
210 camera->setZoomFactor(3.0);
211 //! [Camera zoom]
212}
The QCamera class provides interface for system camera devices.
Definition qcamera.h:25
void camerafocus()
Definition camera.cpp:202
QVideoWidget * viewfinder
Definition camera.cpp:22
void camera_info()
Definition camera.cpp:158
void camera_blah()
Definition camera.cpp:171
void overview_camera_by_position()
Definition camera.cpp:48
void overview_surface()
Definition camera.cpp:56
void camera_selection()
Definition camera.cpp:147
void overview_still()
Definition camera.cpp:97
void overview_viewfinder_orientation()
Definition camera.cpp:71
void camera_listing()
Definition camera.cpp:138
QImageCapture * imageCapture
Definition camera.cpp:21
void cameraimageprocessing()
Definition camera.cpp:194
bool checkCameraAvailability()
[Camera overview check]
Definition camera.cpp:25
void overview_movie()
Definition camera.cpp:113
QMediaRecorder * recorder
Definition camera.cpp:20
void overview_viewfinder()
[Camera overview check]
Definition camera.cpp:34