-
Notifications
You must be signed in to change notification settings - Fork 1
/
Qrimagedecoder.cpp
245 lines (211 loc) · 10.7 KB
/
Qrimagedecoder.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
#include "Qrimagedecoder.hpp"
#include <QDebug>
#include<QImage>
#include <QQuickImageProvider>
#include <QGuiApplication>
QRImageDecoder* QRImageDecoder::m_instance=nullptr;
#ifdef USE_EMSCRIPTEN
#include <emscripten.h>
#include <emscripten/bind.h>
EMSCRIPTEN_BINDINGS(qrdecoder) {
emscripten::class_<QRImageDecoder>("QRImageDecoder")
.function("reload", &QRImageDecoder::reload,emscripten::allow_raw_pointers())
.class_function("instance", &QRImageDecoder::instance, emscripten::allow_raw_pointers());
}
EM_JS(void, js_start, (), {
if ('mediaDevices' in navigator && 'getUserMedia' in navigator.mediaDevices) {
stream = navigator.mediaDevices.getUserMedia({ video: { facingMode: 'environment' }, audio: false }).then((stream) => {
const settings = stream.getVideoTracks()[0].getSettings();
const width = settings.width;
const height = settings.height;
if(document.querySelector("#qrvideo")=== null)
{
let elemDiv = document.createElement('div');
elemDiv.style.cssText = 'display:none; position:absolute;width:100%;height:100%;';
elemDiv.innerHTML += '<video controls autoplay id="qrvideo" width="'+width+'px" height="'+height+'px"></video><canvas id="qrcanvas" width="'+width+'px" height="'+height+'px" ></canvas></div>';
document.body.appendChild(elemDiv);
}
const video = document.querySelector("#qrvideo");
video.srcObject = stream;
window.localStream = stream;
let canvas = document.querySelector("#qrcanvas");
let ctx=canvas.getContext("2d");
const processFrame = function () {
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
const sourceBuffer = imageData.data;
const buffer = _malloc(sourceBuffer.byteLength);
HEAPU8.set(sourceBuffer, buffer);
Module.QRImageDecoder.instance().reload(buffer,video.width,video.height);
_free(buffer);
if(window.localStream.active)
{
requestAnimationFrame(processFrame);
}
else
{
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
};
processFrame();
}).catch(alert);
}
});
EM_JS(void, js_stop, (), {
if(window.localStream)window.localStream.getVideoTracks()[0].stop();
});
#else
#if QT_CONFIG(permissions)
#include <QPermission>
#endif
void QRImageDecoder::getCamera(void)
{
const QList<QCameraDevice> cameras = QMediaDevices::videoInputs();
if(cameras.size())
{
QCameraDevice best=cameras.front();
for (const QCameraDevice &cameraDevice : cameras) {
if (cameraDevice.position() == QCameraDevice::BackFace)
{
best=cameraDevice;
}
}
m_camera=new QCamera(best,this);
auto bvF=best.videoFormats().at(0);
for (const QCameraFormat &format : best.videoFormats())
{
if(abs(format.resolution().width()*1.0-format.resolution().height())<abs(bvF.resolution().width()*1.0-bvF.resolution().height()))
{
bvF=format;
}
}
m_camera->setCameraFormat(bvF);
}
}
#endif
QRImageDecoder* QRImageDecoder::instance()
{
if (!m_instance) m_instance=new QRImageDecoder();
return m_instance;
}
QRImageDecoder::QRImageDecoder(QObject *parent):QObject(parent),m_useTorch(false),m_hasTorch(false),
#ifndef USE_EMSCRIPTEN
m_camera(nullptr),captureSession(new QMediaCaptureSession(this)),videoSink(new QVideoSink(this)),
#endif
m_state(Ready)
{
#ifndef USE_EMSCRIPTEN
captureSession->setVideoOutput(videoSink);
QObject::connect(videoSink,&QVideoSink::videoFrameChanged,this,[=](const QVideoFrame & Vframe)
{
if(m_camera&&m_camera->isActive()&&Vframe.isValid()){
auto picture=Vframe.toImage();
WasmImageProvider::img=picture;
setid();
if(m_state)
{
auto var = std::thread(&QRImageDecoder::decodePicture, this,picture);
var.detach();
}
}
});
connect(this,&QRImageDecoder::useTorchChanged,this,[=](){
if(m_camera->isActive()&&m_useTorch)
m_camera->setTorchMode(QCamera::TorchOn);
else
m_camera->setTorchMode(QCamera::TorchOff);
});
#endif
};
void QRImageDecoder::stop(){
#ifdef USE_EMSCRIPTEN
js_stop();
#else
if(m_camera)m_camera->stop();
#endif
};
void QRImageDecoder::start()
{
#ifdef USE_EMSCRIPTEN
clear();
js_start();
#elif QT_CONFIG(permissions)
QCameraPermission cPermission;
switch (qApp->checkPermission(cPermission)) {
case Qt::PermissionStatus::Undetermined:
qApp->requestPermission(cPermission, this,
&QRImageDecoder::start);
return;
case Qt::PermissionStatus::Denied:
return;
case Qt::PermissionStatus::Granted:
if(!m_camera)
{
getCamera();
if(m_camera)
{
captureSession->setCamera(m_camera);
QObject::connect(m_camera,&QCamera::activeChanged,[=](bool var)
{
if(var&&m_camera->isTorchModeSupported(QCamera::TorchOn))
{
m_hasTorch=true;
emit hasTorchChanged();
}
});
QObject::connect(m_camera,&QCamera::errorOccurred,[](QCamera::Error error, const QString &errorString)
{
qDebug()<<"Camera Error:"<<errorString;
});
}
}
if(m_camera)
{
clear();
m_camera->start();
}
return;
}
#endif
}
void QRImageDecoder::decodePicture(QImage picture)
{
m_state=QRImageDecoder::Decoding;
picture.convertTo(QImage::Format_Grayscale8);
const auto str = detector.decode_grey(picture.bits(), picture.height(),picture.bytesPerLine());
const auto qstr=QString::fromStdString(str);
if(qstr!="")
{
emit decodedQR(qstr);
}
m_state=QRImageDecoder::Ready;
}
QImage WasmImageProvider::img=QImage();
QImage WasmImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
{
return img;
}
void QRImageDecoder::clear(void)
{
WasmImageProvider::restart();
setid();
}
void WasmImageProvider::restart(void)
{
WasmImageProvider::img=QImage(QSize(200,150),QImage::Format_RGBA8888);
WasmImageProvider::img.fill("black");
}
void QRImageDecoder::reload(int offset, int width, int height)
{
auto imgarr = reinterpret_cast<uchar*>(offset);
WasmImageProvider::img=QImage(imgarr,width,height,QImage::Format_RGBA8888);
setid();
if(m_state)decodePicture(WasmImageProvider::img);
}
void QRImageDecoder::setid()
{
static quint8 index=0;
m_source="qrimage"+QString::number(index);
emit sourceChanged();
index++;
}