-
Notifications
You must be signed in to change notification settings - Fork 8
/
cameraqml.cpp
213 lines (171 loc) · 6.95 KB
/
cameraqml.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
/*
Copyright (C) 2012, University of Cambridge, Department of Psychiatry.
Created by Rudolf Cardinal (rnc1001@cam.ac.uk).
This file is part of CamCOPS.
CamCOPS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CamCOPS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CamCOPS. If not, see <https://www.gnu.org/licenses/>.
*/
// #define DEBUG_CAMERA
// #define DEBUG_TEST_QML_ONLY
#define USE_FILE
#include "cameraqml.h"
#include <QFile>
#include <QFileInfo>
#include <QMimeDatabase>
#include <QMimeType>
#include <QtQml/QQmlEngine>
#include <QtQuick/QQuickItem>
#include <QVBoxLayout>
#include "lib/uifunc.h"
#ifdef DEBUG_TEST_QML_ONLY
const QString TEST_ANIMATION_QML("camcops/camera_qml/test_animation.qml");
#else
const QString CAMERA_QML("camcops/camera_qml/camera.qml");
#endif
/*
For examples see
- http://doc.qt.io/qt-5/qtmultimedia-multimediawidgets-camera-example.html
- qt5/qtmultimedia/examples/multimediawidgets/camera/camera.cpp
- qt5/qtmultimedia/examples/multimedia/declarative-camera/...
The "declarative-camera" example is the QML one.
- It's very responsive. It runs on Android properly.
The "multimediawidgets/camera" one is plain CPP.
- Its viewfinder is laggy in the default configuration.
- Its viewfinder doesn't work on Android.
Yet presumably all the QML stuff uses the same underlying CPP code?
Or maybe not?
- https://forum.qt.io/topic/59394/declarative-camera-vs-widget-based-camera-qml-to-c-breakout
- http://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html
- http://lists.qt-project.org/pipermail/android-development/2015-September/000734.html
- https://stackoverflow.com/questions/40153156/qt-qcamera-not-working-on-android
- https://bugreports.qt.io/browse/QTBUG-38233
- https://bugreports.qt.io/browse/QTBUG-41467
- http://omg-it.works/how-to-grab-video-frames-directly-from-qcamera/
- https://forum.qt.io/topic/47330/android-qcamera-5-4-beta
- https://www.ics.com/blog/combining-qt-widgets-and-qml-qwidgetcreatewindowcontainer
The actual error on Android is:
... warning: The video surface is not compatible with any format supported by the camera
*/
// ============================================================================
// Constructor/destructor
// ============================================================================
CameraQml::CameraQml(QWidget* parent) :
OpenableWidget(parent)
{
#ifdef DEBUG_CAMERA
qDebug() << Q_FUNC_INFO;
#endif
/*
QWidget::createWindowContainer()
- https://www.ics.com/blog/combining-qt-widgets-and-qml-qwidgetcreatewindowcontainer
- http://doc.qt.io/qt-5/qquickview.html#details
- http://blog.qt.io/blog/2013/02/19/introducing-qwidgetcreatewindowcontainer/
BUT:
- doesn't work on Android, even for the test animation.
- When it doesn't work, but the declarative-camera example does:
These errors come from both, so are not a problem:
(a) camcops
- D libGLESv2: DTS_GLAPI : DTS is not allowed for Package : org.camcops.camcops
- E libGLESv1: HWUI Protection: wrong call from hwui context F:ES1-glDeleteTexturesSEC
(b) declarative_camera
- D libGLESv2: DTS_GLAPI : DTS is not allowed for Package : org.qtproject.example.declarative_camera
...
- E libGLESv1: HWUI Protection: wrong call from hwui context F:ES1-glDeleteTexturesSEC
- http://lists.qt-project.org/pipermail/interest/2015-November/019657.html
... use QQuickWidget instead.
*/
m_qml_view = new QQuickWidget();
m_qml_view->setResizeMode(QQuickWidget::SizeRootObjectToView);
connect(m_qml_view->engine(), &QQmlEngine::quit,
this, &CameraQml::cancelled);
// Just after calling setSource(), calling view->rootObject() can give a
// nullptr, because it may be loading in the background. So:
connect(m_qml_view, &QQuickWidget::statusChanged,
this, &CameraQml::qmlStatusChanged);
// ... and must set that signal before calling setSource().
#ifdef DEBUG_TEST_QML_ONLY
m_qml_view->setSource(uifunc::resourceUrl(TEST_ANIMATION_QML));
m_qml_view->resize(800, 480);
#else
m_qml_view->setSource(uifunc::resourceUrl(CAMERA_QML));
#endif
auto top_layout = new QVBoxLayout();
top_layout->addWidget(m_qml_view);
setLayout(top_layout);
}
// ============================================================================
// Public interface
// ============================================================================
void CameraQml::finish()
{
emit finished();
}
// ============================================================================
// Internals
// ============================================================================
void CameraQml::qmlStatusChanged(const QQuickWidget::Status status)
{
#ifdef DEBUG_CAMERA
qDebug() << Q_FUNC_INFO;
#endif
if (status == QQuickWidget::Ready) {
qmlFinishedLoading();
} else {
qWarning() << "QML status is unhappy:" << status;
}
}
void CameraQml::qmlFinishedLoading()
{
#ifdef DEBUG_CAMERA
qDebug() << Q_FUNC_INFO;
#endif
Q_ASSERT(m_qml_view);
QQuickItem* root = m_qml_view->rootObject();
Q_ASSERT(root);
// It's possible to connect to non-root objects, but it's much cleaner to
// route from QML child objects up to the QML root object, and then to C++.
connect(root, SIGNAL(imageCaptured(const QVariant&)),
this, SLOT(copyPreviewImage(const QVariant&)));
connect(root, SIGNAL(previewSaved()), this, SLOT(savePreviewImage()));
connect(root, SIGNAL(fileNoLongerNeeded(const QString&)),
this, SLOT(deleteSuperfluousFile(const QString&)));
// ... we have to use SIGNAL() and SLOT() since C++ has no idea of the
// provenance of the signal (and whether or not it exists) -- the macros
// map signals via strings, so this works, but you'll get an error like
// "QObject::connect: No such signal PhotoPreview_QMLTYPE_2::imageCaptured(const QString&)"
// if you get the type wrong.
}
void CameraQml::copyPreviewImage(const QVariant& preview)
{
m_preview = preview.value<QImage>();
}
void CameraQml::savePreviewImage()
{
emit imageCaptured(m_preview);
}
void CameraQml::deleteFile(const QString& filename) const
{
#ifdef DEBUG_CAMERA
qDebug() << Q_FUNC_INFO;
#endif
if (!filename.isEmpty()) {
bool success = QFile::remove(filename);
qDebug() << "Deleting temporary camera file " << filename
<< (success ? "... success" : "... FAILED!");
}
}
void CameraQml::deleteSuperfluousFile(const QString& filename) const
{
#ifdef DEBUG_CAMERA
qDebug() << Q_FUNC_INFO;
#endif
deleteFile(filename);
}