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
qplatformcamera.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 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
5
6#include <QtMultimedia/private/qcameradevice_p.h>
7#include <QtMultimedia/private/qmultimedia_ranges_p.h>
8
10
11namespace ranges = QtMultimediaPrivate::ranges;
12
13QPlatformCamera::QPlatformCamera(QCamera *parent) : QPlatformVideoSource(parent), m_camera(parent)
14{
15 Q_ASSERT(parent);
16 qRegisterMetaType<QVideoFrame>();
17}
18
19QCameraFormat QPlatformCamera::findBestCameraFormat(const QCameraDevice &camera) const
20{
21 // check if fmt is better. We try to find the highest resolution that offers
22 // at least 30 FPS
23 // we use 29 FPS to compare against as some cameras report 29.97 FPS...
24
25 auto makeCriteria = [this](const QCameraFormat &fmt) {
26 constexpr float MinSufficientFrameRate = 29.f;
27
28 const auto isValid = fmt.pixelFormat() != QVideoFrameFormat::Format_Invalid;
29 const auto resolution = fmt.resolution();
30 const auto sufficientFrameRate = std::min(fmt.maxFrameRate(), MinSufficientFrameRate);
31 const auto pixelFormatScore =
32 cameraPixelFormatScore(fmt.pixelFormat(), QCameraFormatPrivate::getColorRange(fmt));
33
34 return std::make_tuple(
35 isValid, // 1st: ensure valid formats
36 sufficientFrameRate, // 2nd: ensure the highest frame rate in the range [0; 29]*/
37 resolution.width() * resolution.height(), // 3rd: ensure the highest resolution
38 pixelFormatScore, // 4th: eshure the best pixel format
39 fmt.maxFrameRate()); // 5th: ensure the highest framerate in the whole range
40 };
41
42 const auto formats = camera.videoFormats();
43 if (formats.empty())
44 return QCameraFormat{};
45
46 const auto found =
47 ranges::max(formats, [&](const QCameraFormat &fmtA, const QCameraFormat &fmtB) {
48 return makeCriteria(fmtA) < makeCriteria(fmtB);
49 });
50
51 return found;
52}
53
54QVideoFrameFormat QPlatformCamera::frameFormat() const
55{
56 QVideoFrameFormat result(m_cameraFormat.resolution(),
57 m_framePixelFormat == QVideoFrameFormat::Format_Invalid
58 ? m_cameraFormat.pixelFormat()
59 : m_framePixelFormat);
60 result.setStreamFrameRate(m_cameraFormat.maxFrameRate());
61 return result;
62}
63
64void QPlatformCamera::supportedFeaturesChanged(QCamera::Features f)
65{
66 if (m_supportedFeatures == f)
67 return;
68 m_supportedFeatures = f;
69 emit m_camera->supportedFeaturesChanged();
70}
71
72void QPlatformCamera::minimumZoomFactorChanged(float factor)
73{
74 if (m_minZoom == factor)
75 return;
76 m_minZoom = factor;
77 emit m_camera->minimumZoomFactorChanged(factor);
78}
79
80void QPlatformCamera::maximumZoomFactorChanged(float factor)
81{
82 if (m_maxZoom == factor)
83 return;
84 m_maxZoom = factor;
85 emit m_camera->maximumZoomFactorChanged(factor);
86}
87
88void QPlatformCamera::focusModeChanged(QCamera::FocusMode mode)
89{
90 if (m_focusMode == mode)
91 return;
92 m_focusMode = mode;
93 emit m_camera->focusModeChanged();
94}
95
96void QPlatformCamera::customFocusPointChanged(const QPointF &point)
97{
98 if (m_customFocusPoint == point)
99 return;
100 m_customFocusPoint = point;
101 emit m_camera->customFocusPointChanged();
102}
103
104
105void QPlatformCamera::zoomFactorChanged(float zoom)
106{
107 if (m_zoomFactor == zoom)
108 return;
109 m_zoomFactor = zoom;
110 emit m_camera->zoomFactorChanged(zoom);
111}
112
113
114void QPlatformCamera::focusDistanceChanged(float d)
115{
116 if (m_focusDistance == d)
117 return;
118 m_focusDistance = d;
119 emit m_camera->focusDistanceChanged(m_focusDistance);
120}
121
122
123void QPlatformCamera::flashReadyChanged(bool ready)
124{
125 if (m_flashReady == ready)
126 return;
127 m_flashReady = ready;
128 emit m_camera->flashReady(m_flashReady);
129}
130
131void QPlatformCamera::flashModeChanged(QCamera::FlashMode mode)
132{
133 if (m_flashMode == mode)
134 return;
135 m_flashMode = mode;
136 emit m_camera->flashModeChanged();
137}
138
139void QPlatformCamera::torchModeChanged(QCamera::TorchMode mode)
140{
141 if (m_torchMode == mode)
142 return;
143 m_torchMode = mode;
144 emit m_camera->torchModeChanged();
145}
146
147void QPlatformCamera::exposureModeChanged(QCamera::ExposureMode mode)
148{
149 if (m_exposureMode == mode)
150 return;
151 m_exposureMode = mode;
152 emit m_camera->exposureModeChanged();
153}
154
155void QPlatformCamera::exposureCompensationChanged(float compensation)
156{
157 if (m_exposureCompensation == compensation)
158 return;
159 m_exposureCompensation = compensation;
160 emit m_camera->exposureCompensationChanged(compensation);
161}
162
163void QPlatformCamera::exposureCompensationRangeChanged(float min, float max)
164{
165 if (m_minExposureCompensation == min && m_maxExposureCompensation == max)
166 return;
167 m_minExposureCompensation = min;
168 m_maxExposureCompensation = max;
169 // tell frontend
170}
171
172void QPlatformCamera::isoSensitivityChanged(int iso)
173{
174 if (m_iso == iso)
175 return;
176 m_iso = iso;
177 emit m_camera->isoSensitivityChanged(iso);
178}
180void QPlatformCamera::exposureTimeChanged(float speed)
181{
182 if (m_exposureTime == speed)
183 return;
184 m_exposureTime = speed;
185 emit m_camera->exposureTimeChanged(speed);
186}
187
188void QPlatformCamera::whiteBalanceModeChanged(QCamera::WhiteBalanceMode mode)
189{
190 if (m_whiteBalance == mode)
191 return;
192 m_whiteBalance = mode;
193 emit m_camera->whiteBalanceModeChanged();
194}
195
196void QPlatformCamera::colorTemperatureChanged(int temperature)
197{
198 Q_ASSERT(temperature >= 0);
199 Q_ASSERT((temperature > 0 && whiteBalanceMode() == QCamera::WhiteBalanceManual) ||
200 (temperature == 0 && whiteBalanceMode() == QCamera::WhiteBalanceAuto));
201 if (m_colorTemperature == temperature)
202 return;
203 m_colorTemperature = temperature;
204 emit m_camera->colorTemperatureChanged();
205}
206
207int QPlatformCamera::colorTemperatureForWhiteBalance(QCamera::WhiteBalanceMode mode)
208{
209 switch (mode) {
210 case QCamera::WhiteBalanceAuto:
211 break;
212 case QCamera::WhiteBalanceManual:
213 case QCamera::WhiteBalanceSunlight:
214 return 5600;
215 case QCamera::WhiteBalanceCloudy:
216 return 6000;
217 case QCamera::WhiteBalanceShade:
218 return 7000;
219 case QCamera::WhiteBalanceTungsten:
220 return 3200;
221 case QCamera::WhiteBalanceFluorescent:
222 return 4000;
223 case QCamera::WhiteBalanceFlash:
224 return 5500;
225 case QCamera::WhiteBalanceSunset:
226 return 3000;
227 }
228 return 0;
229}
230
231void QPlatformCamera::updateError(QCamera::Error error, const QString &errorString)
232{
233 QMetaObject::invokeMethod(this, [this, error, errorString]() {
234 m_error.setAndNotify(error, errorString, *this);
235 });
236}
237
238QT_END_NAMESPACE
239
240#include "moc_qplatformcamera_p.cpp"
Combined button and popup list for selecting options.