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