Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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"
7#include "qcameradevice.h"
8#include "qmediarecorder.h"
9#include "qmediadevices.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. */
23
26{
28 return true;
29 else
30 return false;
31}
33
35{
37 QMediaCaptureSession captureSession;
38 camera = new QCamera;
39 captureSession.setCamera(camera);
41 captureSession.setVideoOutput(viewfinder);
43
44 camera->start(); // to start the camera
46}
47
54
55// -.-
57{
58 QVideoSink *mySink;
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
69}
70
72{
74
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
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));
95}
96
98{
100 QMediaCaptureSession captureSession;
101 camera = new QCamera;
102 captureSession.setCamera(camera);
104 captureSession.setImageCapture(imageCapture);
105
106 camera->start(); // Viewfinder frames start flowing
107
108 //on shutter button pressed
111}
112
114{
116 QMediaCaptureSession captureSession;
117 camera = new QCamera;
118 captureSession.setCamera(camera);
120 captureSession.setRecorder(recorder);
121
122 camera->start();
123
124 // setup output format for the recorder
126 format.setVideoCodec(QMediaRecorder::VideoCodec::H264);
127 format.setAudioCodec(QMediaRecorder::AudioCodec::MP3);
129
130 //on shutter button pressed
131 recorder->record();
132
133 // sometime later, or on another press
134 recorder->stop();
136}
137
139{
141 const QList<QCameraDevice> cameras = QMediaDevices::videoInputs();
142 for (const QCameraDevice &cameraDevice : cameras)
143 qDebug() << cameraDevice.description();
145}
146
148{
150 const QList<QCameraDevice> cameras = QMediaDevices::videoInputs();
151 for (const QCameraDevice &cameraDevice : cameras) {
152 if (cameraDevice.description() == "mycamera")
153 camera = new QCamera(cameraDevice);
154 }
156}
157
159{
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.";
169}
170
172{
174 QMediaCaptureSession captureSession;
175 camera = new QCamera;
176 captureSession.setCamera(camera);
177
178 viewfinder = new QVideoWidget();
179 viewfinder->show();
180 captureSession.setVideoOutput(viewfinder);
181
183 captureSession.setImageCapture(imageCapture);
184
185 camera->start();
187
189 //on shutter button pressed
192}
193
201
203{
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
208
210 camera->setZoomFactor(3.0);
212}
The QCameraDevice class provides general information about camera devices.
Position position
\qmlproperty enumeration QtMultimedia::cameraDevice::position
The QCamera class provides interface for system camera devices.
Definition qcamera.h:28
@ WhiteBalanceFluorescent
Definition qcamera.h:119
@ FocusModeManual
Definition qcamera.h:73
QCameraDevice cameraDevice
\qmlproperty cameraDevice QtMultimedia::Camera::cameraDevice
Definition qcamera.h:32
void start()
\qmlmethod void Camera::start()
Definition qcamera.h:204
void setWhiteBalanceMode(WhiteBalanceMode mode)
Sets the white balance to mode.
Definition qcamera.cpp:1246
void setZoomFactor(float factor)
Zooms to a zoom factor factor at a rate of 1 factor per second.
Definition qcamera.cpp:748
void setCustomFocusPoint(const QPointF &point)
Definition qcamera.cpp:639
QScreen * primaryScreen
the primary (or default) screen of the application.
\inmodule QtMultimedia
int capture()
Capture the image and make it available as a QImage.
\inmodule QtGui
Definition qimage.h:37
QImage transformed(const QTransform &matrix, Qt::TransformationMode mode=Qt::FastTransformation) const
The QMediaCaptureSession class allows capturing of audio and video content.
void setCamera(QCamera *camera)
void setRecorder(QMediaRecorder *recorder)
void setVideoOutput(QObject *output)
Sets a QObject, (output), to a video preview for the capture session.
void setImageCapture(QImageCapture *imageCapture)
QList< QCameraDevice > videoInputs
\qmlproperty list<cameraDevice> QtMultimedia::MediaDevices::videoInputs Contains a list of cameras on...
\inmodule QtMultimedia
\inmodule QtMultimedia
void stop()
\qmlmethod QtMultimedia::MediaRecorder::stop()
void setMediaFormat(const QMediaFormat &format)
void record()
\qmlmethod QtMultimedia::MediaRecorder::record()
\inmodule QtCore\reentrant
Definition qpoint.h:217
The QScreen class is used to query screen properties. \inmodule QtGui.
Definition qscreen.h:32
int angleBetween(Qt::ScreenOrientation a, Qt::ScreenOrientation b) const
Convenience function to compute the angle of rotation to get from rotation a to rotation b.
Definition qscreen.cpp:519
Qt::ScreenOrientation nativeOrientation
the native screen orientation
Definition qscreen.h:63
Qt::ScreenOrientation orientation
the screen orientation
Definition qscreen.h:62
The QTransform class specifies 2D transformations of a coordinate system.
Definition qtransform.h:20
QTransform & rotate(qreal a, Qt::Axis axis=Qt::ZAxis, qreal distanceToPlane=1024.0f)
The QVideoSink class represents a generic sink for video data.
Definition qvideosink.h:22
The QVideoWidget class provides a widget which presents video produced by a media object.
void show()
Shows the widget and its child widgets.
Definition qwidget.cpp:7875
void camerafocus()
Definition camera.cpp:202
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
QVideoWidget * viewfinder
Definition camera.cpp:22
QMediaRecorder * recorder
Definition camera.cpp:20
QCamera * camera
Definition camera.cpp:19
void camera_selection()
Definition camera.cpp:147
QImageCapture * imageCapture
Definition camera.cpp:21
void overview_still()
Definition camera.cpp:97
void overview_viewfinder_orientation()
Definition camera.cpp:71
void camera_listing()
Definition camera.cpp:138
void cameraimageprocessing()
Definition camera.cpp:194
bool checkCameraAvailability()
[Camera overview check]
Definition camera.cpp:25
void overview_movie()
Definition camera.cpp:113
void overview_viewfinder()
[Camera overview check]
Definition camera.cpp:34
#define qDebug
[1]
Definition qlogging.h:164
GLenum GLenum GLsizei count
GLint GLsizei GLsizei GLenum format
QScreen * screen
[1]
Definition main.cpp:29
QSettings settings("MySoft", "Star Runner")
[0]