-
Notifications
You must be signed in to change notification settings - Fork 1
/
ocvdecoder.cpp
157 lines (129 loc) · 4.55 KB
/
ocvdecoder.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
#include "ocvdecoder.h"
#include <opencv2/opencv.hpp>
#include <QUrl>
#include <QtConcurrent/QtConcurrent>
using namespace std;
OCVDecoder::OCVDecoder(QObject *parent): QObject(parent)
{
QString path = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
m_detector = makePtr<wechat_qrcode::WeChatQRCode>("" , "" ,
"", "");
}
void OCVDecoder::setFrame(const QVideoFrame &frame)
{
if (m_run && !isDecoding() && m_processThread.isFinished()) {
qDebug() << "DECODING1\n";
m_decoding = true;
QImage image = frame.toImage().convertToFormat(QImage::Format_RGB32).rgbSwapped();
m_processThread = QtConcurrent::run([=]() {
qDebug() << "DECODING2\n";
if(image.isNull()) {
m_decoding = false;
return;
}
cv::Mat img(image.height(), image.width(), CV_8UC4, (void *) image.bits(), image.bytesPerLine());
vector<Mat> points;
auto res = m_detector->detectAndDecode(img, points);
if (res.size() > 0) {
for (const auto& value : res) {
qDebug() << " opencv " << QString(value.c_str());
emit decoded(QString(value.c_str()));
m_run = false;
break;
}
}
m_decoding = false;
});
}
}
void OCVDecoder::setVideoSink(QObject *videoSync)
{
m_videoSink = qobject_cast<QVideoSink*>(videoSync);
if (m_videoSink) {
QObject::connect(m_videoSink, &QVideoSink::videoFrameChanged,
this, &OCVDecoder::setFrame);
}
}
void OCVDecoder::setRun(bool run)
{
m_run = run;
}
// https://asmaloney.com/2013/11/code/converting-between-cvmat-and-qimage-or-qpixmap/
static cv::Mat QImageToCvMat(QImage inImage, bool inCloneImageData = true)
{
switch ( inImage.format() )
{
// 8-bit, 4 channel
case QImage::Format_ARGB32:
case QImage::Format_ARGB32_Premultiplied:
{
cv::Mat mat( inImage.height(), inImage.width(),
CV_8UC4,
const_cast<uchar*>(inImage.bits()),
static_cast<size_t>(inImage.bytesPerLine())
);
return (inCloneImageData ? mat.clone() : mat);
}
// 8-bit, 3 channel
case QImage::Format_RGB32:
{
if ( !inCloneImageData ) {
qWarning() << "QImageToCvMat() - Conversion requires cloning so we don't modify the original QImage data";
}
cv::Mat mat( inImage.height(), inImage.width(),
CV_8UC4,
const_cast<uchar*>(inImage.bits()),
static_cast<size_t>(inImage.bytesPerLine())
);
cv::Mat matNoAlpha;
cv::cvtColor( mat, matNoAlpha, cv::COLOR_BGRA2BGR ); // drop the all-white alpha channel
return matNoAlpha;
}
// 8-bit, 3 channel
case QImage::Format_RGB888:
{
if ( !inCloneImageData ) {
qWarning() << "QImageToCvMat() - Conversion requires cloning so we don't modify the original QImage data";
}
QImage swapped = inImage.rgbSwapped();
cv::Mat mat( swapped.height(), swapped.width(),
CV_8UC3,
const_cast<uchar*>(swapped.bits()),
static_cast<size_t>(swapped.bytesPerLine())
);
return (inCloneImageData ? mat.clone() : mat);
}
// 8-bit, 1 channel
case QImage::Format_Grayscale8:
case QImage::Format_Indexed8:
{
cv::Mat mat( inImage.height(), inImage.width(),
CV_8UC1,
const_cast<uchar*>(inImage.bits()),
static_cast<size_t>(inImage.bytesPerLine())
);
return (inCloneImageData ? mat.clone() : mat);
}
#if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
case QImage::Format_Grayscale16:
{
cv::Mat mat(cv::Size(inImage.width(),inImage.height()),CV_16U,const_cast<uchar*>(inImage.bits()),cv::Mat::AUTO_STEP);
return (inCloneImageData ? mat.clone() : mat);
}
#endif
default:
qWarning() << "QImageToCvMat() - QImage format not handled in switch:" << inImage.format();
break;
}
return cv::Mat();
}
OCVDecoder::~OCVDecoder()
{
if(!m_processThread.isFinished()) {
m_processThread.cancel();
m_processThread.waitForFinished();
}
if (m_detector) {
delete m_detector;
}
}