-
Notifications
You must be signed in to change notification settings - Fork 11
/
gphotocamerasession.cpp
303 lines (251 loc) · 9.57 KB
/
gphotocamerasession.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include <QAbstractVideoSurface>
#include <QDebug>
#include <QFile>
#include <QFileInfo>
#include <QStandardPaths>
#include <QVideoSurfaceFormat>
#include "gphotocamera.h"
#include "gphotocamerafocuscontrol.h"
#include "gphotocamerasession.h"
#include "gphotocontroller.h"
namespace {
constexpr auto maxDownscaleSteps = 8;
constexpr auto maxFileIndex = 9999;
constexpr auto maxPreviewWidth = 800;
}
GPhotoCameraSession::GPhotoCameraSession(std::weak_ptr<GPhotoController> controller, QObject *parent)
: QObject(parent)
, m_controller(std::move(controller))
, m_cameraFocusControl(new GPhotoCameraFocusControl())
{
if (const auto &controller = m_controller.lock()) {
using Controller = GPhotoController;
using Session = GPhotoCameraSession;
connect(controller.get(), &Controller::captureModeChanged, this, &Session::onCaptureModeChanged);
connect(controller.get(), &Controller::error, this, &Session::onError);
connect(controller.get(), &Controller::imageCaptureError, this, &Session::onImageCaptureError);
connect(controller.get(), &Controller::imageCaptured, this, &Session::onImageCaptured);
connect(controller.get(), &Controller::previewCaptured, this, &Session::onPreviewCaptured);
connect(controller.get(), &Controller::readyForCaptureChanged, this, &Session::onReadyForCaptureChanged);
connect(controller.get(), &Controller::stateChanged, this, &Session::onStateChanged);
connect(controller.get(), &Controller::statusChanged, this, &Session::onStatusChanged);
}
}
GPhotoCameraSession::~GPhotoCameraSession() = default;
QList<QByteArray> GPhotoCameraSession::cameraNames() const
{
if (const auto &controller = m_controller.lock())
return controller->cameraNames();
return {};
}
QByteArray GPhotoCameraSession::defaultCameraName() const
{
if (const auto &controller = m_controller.lock())
return controller->defaultCameraName();
return {};
}
QCamera::State GPhotoCameraSession::state() const
{
return m_state;
}
void GPhotoCameraSession::setState(QCamera::State state)
{
if (const auto &controller = m_controller.lock())
controller->setState(m_cameraIndex, state);
}
QCamera::Status GPhotoCameraSession::status() const
{
return m_status;
}
bool GPhotoCameraSession::isCaptureModeSupported(QCamera::CaptureModes mode) const
{
return (QCamera::CaptureViewfinder == mode || QCamera::CaptureStillImage == mode);
}
QCamera::CaptureModes GPhotoCameraSession::captureMode() const
{
return m_captureMode;
}
void GPhotoCameraSession::setCaptureMode(QCamera::CaptureModes captureMode)
{
if (const auto &controller = m_controller.lock())
controller->setCaptureMode(m_cameraIndex, captureMode);
}
bool GPhotoCameraSession::isCaptureDestinationSupported(QCameraImageCapture::CaptureDestinations destination) const
{
Q_UNUSED(destination)
return true;
}
QCameraImageCapture::CaptureDestinations GPhotoCameraSession::captureDestination() const
{
return m_captureDestination;
}
void GPhotoCameraSession::setCaptureDestination(QCameraImageCapture::CaptureDestinations destination)
{
if (m_captureDestination != destination) {
m_captureDestination = destination;
emit captureDestinationChanged(destination);
}
}
bool GPhotoCameraSession::isReadyForCapture() const
{
return m_readyForCapture;
}
int GPhotoCameraSession::capture(const QString &fileName)
{
++m_captureId;
if (const auto &controller = m_controller.lock())
controller->capturePhoto(m_cameraIndex, m_captureId, fileName);
return m_captureId;
}
QAbstractVideoSurface* GPhotoCameraSession::surface() const
{
return m_surface;
}
void GPhotoCameraSession::setSurface(QAbstractVideoSurface *surface)
{
if (m_surface != surface)
m_surface = surface;
}
QVariant GPhotoCameraSession::parameter(const QString &name) const
{
if (const auto &controller = m_controller.lock())
return controller->parameter(m_cameraIndex, name);
return {};
}
bool GPhotoCameraSession::setParameter(const QString &name, const QVariant &value)
{
if (const auto &controller = m_controller.lock())
return controller->setParameter(m_cameraIndex, name, value);
return false;
}
QVariantList GPhotoCameraSession::parameterValues(const QString &name, QMetaType::Type valueType) const
{
if (const auto &controller = m_controller.lock())
return controller->parameterValues(m_cameraIndex, name, valueType);
return {};
}
QCameraFocusControl *GPhotoCameraSession::cameraFocusControl() const
{
return m_cameraFocusControl.get();
}
void GPhotoCameraSession::setCamera(int cameraIndex)
{
if (m_cameraIndex != cameraIndex) {
m_cameraIndex = cameraIndex;
if (const auto &controller = m_controller.lock()) {
onCaptureModeChanged(cameraIndex, controller->captureMode(m_cameraIndex));
onStateChanged(cameraIndex, controller->state(m_cameraIndex));
onStatusChanged(cameraIndex, controller->status(m_cameraIndex));
}
}
}
void GPhotoCameraSession::onCaptureModeChanged(int cameraIndex, QCamera::CaptureModes captureMode)
{
if (m_cameraIndex == cameraIndex && m_captureMode != captureMode) {
m_captureMode = captureMode;
emit captureModeChanged(captureMode);
}
}
void GPhotoCameraSession::onError(int cameraIndex, int errorCode, const QString &errorString)
{
if (m_cameraIndex == cameraIndex)
emit error(errorCode, errorString);
}
void GPhotoCameraSession::onImageCaptureError(int cameraIndex, int id, int errorCode, const QString &errorString)
{
if (m_cameraIndex == cameraIndex)
emit imageCaptureError(id, errorCode, errorString);
}
void GPhotoCameraSession::onImageCaptured(int cameraIndex, int id, const QByteArray &imageData,
const QString &format, const QString &fileName)
{
if (m_cameraIndex != cameraIndex)
return;
if (format.startsWith(QLatin1String("jp"), Qt::CaseInsensitive)) {
auto image = QImage::fromData(imageData);
if (!image.isNull()) {
auto previewSize = image.size();
auto downScaleSteps = 0;
while (previewSize.width() > maxPreviewWidth && downScaleSteps < maxDownscaleSteps) {
previewSize.rwidth() /= 2;
previewSize.rheight() /= 2;
++downScaleSteps;
}
const auto &snapPreview = image.scaled(previewSize);
emit imageCaptured(id, snapPreview);
if (m_captureDestination & QCameraImageCapture::CaptureToBuffer) {
QVideoFrame frame(image);
emit imageAvailable(id, frame);
}
}
}
if (m_captureDestination & QCameraImageCapture::CaptureToFile) {
QString actualFileName(fileName);
if (actualFileName.isEmpty()) {
auto dir = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
if (dir.isEmpty()) {
emit imageCaptureError(id, QCameraImageCapture::ResourceError,
tr("Could not determine writable location for saving captured image"));
return;
}
dir += QLatin1String("/DCIM%1.") + format;
// Trying to find free filename
for (auto i = 0; i < maxFileIndex; ++i) {
auto tmpFileName = dir.arg(i, 4, 10, QChar('0'));
if (!QFile(tmpFileName).exists()) {
actualFileName = std::move(tmpFileName);
break;
}
}
if (actualFileName.isEmpty()) {
emit imageCaptureError(id, QCameraImageCapture::ResourceError,
tr("Could not determine writable location for saving captured image"));
return;
}
}
QFile file(actualFileName);
if (file.open(QFile::WriteOnly)) {
if (file.write(imageData)) {
emit imageSaved(id, actualFileName);
} else {
emit imageCaptureError(id, QCameraImageCapture::OutOfSpaceError, file.errorString());
}
} else {
auto errorMessage = tr("Could not open destination file:\n%1").arg(actualFileName);
emit imageCaptureError(id, QCameraImageCapture::ResourceError, errorMessage);
}
}
}
void GPhotoCameraSession::onPreviewCaptured(int cameraIndex, const QImage &image)
{
if (m_cameraIndex == cameraIndex && QCamera::ActiveState == m_state && m_surface && !image.isNull()) {
if (m_surface->isActive() && image.size() != m_surface->surfaceFormat().frameSize())
m_surface->stop();
if (!m_surface->isActive())
m_surface->start(QVideoSurfaceFormat(image.size(), QVideoFrame::Format_RGB32));
QVideoFrame frame(image);
m_surface->present(frame);
emit videoFrameProbed(frame);
}
}
void GPhotoCameraSession::onReadyForCaptureChanged(int cameraIndex, bool readyForCapture)
{
if (m_cameraIndex == cameraIndex && m_readyForCapture != readyForCapture) {
m_readyForCapture = readyForCapture;
emit readyForCaptureChanged(readyForCapture);
}
}
void GPhotoCameraSession::onStateChanged(int cameraIndex, QCamera::State state)
{
if (m_cameraIndex == cameraIndex && m_state != state) {
m_state = state;
emit stateChanged(state);
}
}
void GPhotoCameraSession::onStatusChanged(int cameraIndex, QCamera::Status status)
{
if (m_cameraIndex == cameraIndex && m_status != status) {
m_status = status;
emit statusChanged(status);
}
}