-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathmyvideosurface.cpp
213 lines (185 loc) · 9.07 KB
/
myvideosurface.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
#include "myvideosurface.h"
#include "./ui_mainwindow.h"
MyVideoSurface::MyVideoSurface(QObject *parent, Ui::MainWindow *ui, void *reader, QCamera *camera) : QAbstractVideoSurface(parent)
{
this->ui = ui;
this->reader = reader;
this->camera = camera;
this->is_detecting = true;
}
MyVideoSurface::~MyVideoSurface()
{
}
void MyVideoSurface::reset()
{
this->is_detecting = true;
this->queue.clear();
}
QList<QVideoFrame::PixelFormat> MyVideoSurface::supportedPixelFormats(QAbstractVideoBuffer::HandleType type) const
{
if (type == QAbstractVideoBuffer::NoHandle)
{
return QList<QVideoFrame::PixelFormat>()
<< QVideoFrame::Format_RGB32;
}
else
{
return QList<QVideoFrame::PixelFormat>();
}
}
bool MyVideoSurface::present(const QVideoFrame &frame)
{
if (frame.isValid() && is_detecting)
{
QVideoFrame cloneFrame(frame);
cloneFrame.map(QAbstractVideoBuffer::ReadOnly);
const QImage img(cloneFrame.bits(),
cloneFrame.width(),
cloneFrame.height(),
QVideoFrame::imageFormatFromPixelFormat(cloneFrame.pixelFormat()));
QImage cp = img.copy();
cloneFrame.unmap();
cp = cp.mirrored(false, true);
if (worker)
{
worker->appendFrame(cp);
std::vector<BarcodeInfo> info;
mutex.lock();
if (queue.size() > 0)
{
info = queue.back();
queue.pop_back();
}
mutex.unlock();
QPixmap pm = QPixmap::fromImage(cp);
QString out = "";
if (info.size() > 0)
{
if (ui->checkBox_autostop->isChecked()) pause();
QPainter painter(&pm);
painter.setPen(Qt::red);
for (int index = 0; index < info.size(); index++)
{
BarcodeInfo barcodeInfo = info.at(index);
out += "Index: " + QString::number(index) + ", Elapsed time: " + barcodeInfo.decodingTime + "ms\n";
out += "Barcode format: " + barcodeInfo.format + "\n";
out += "Barcode value: " + barcodeInfo.text + "\n";
out += "Bounding box: (" + QString::number(barcodeInfo.x1) + ", " + QString::number(barcodeInfo.y1) + ") "
+ "(" + QString::number(barcodeInfo.x2) + ", " + QString::number(barcodeInfo.y2) + ") "
+ "(" + QString::number(barcodeInfo.x3) + ", " + QString::number(barcodeInfo.y3) + ") "
+ "(" + QString::number(barcodeInfo.x4) + ", " + QString::number(barcodeInfo.y4) + ")\n";
out += "----------------------------------------------------------------------------------------\n";
painter.drawLine(barcodeInfo.x1, barcodeInfo.y1, barcodeInfo.x2, barcodeInfo.y2);
painter.drawLine(barcodeInfo.x2, barcodeInfo.y2, barcodeInfo.x3, barcodeInfo.y3);
painter.drawLine(barcodeInfo.x3, barcodeInfo.y3, barcodeInfo.x4, barcodeInfo.y4);
painter.drawLine(barcodeInfo.x4, barcodeInfo.y4, barcodeInfo.x1, barcodeInfo.y1);
}
painter.end();
}
else
{
out = "No barcode detected";
}
ui->textEdit_results->setText(out);
ui->label->setPixmap(pm.scaled(ui->label->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
}
else
{
QPixmap pm = QPixmap::fromImage(cp);
QPainter painter(&pm);
painter.setPen(Qt::red);
// Get the template content and initialize the runtime settings.
QString content = ui->textEdit_results->toPlainText();
char errorMessage[256];
if (!content.isEmpty()) {
DBR_InitRuntimeSettingsWithString(reader, content.toStdString().c_str(), CM_OVERWRITE, errorMessage, 256);
}
// Set barcode types.
int types = 0, types2 = 0;
if (ui->checkBox_code39->isChecked()) {types |= BF_CODE_39;}
if (ui->checkBox_code93->isChecked()) {types |= BF_CODE_93;}
if (ui->checkBox_code128->isChecked()){ types |= BF_CODE_128;}
if (ui->checkBox_codabar->isChecked()){ types |= BF_CODABAR;}
if (ui->checkBox_itf->isChecked()){ types |= BF_ITF;}
if (ui->checkBox_ean13->isChecked()){ types |= BF_EAN_13;}
if (ui->checkBox_ean8->isChecked()){ types |= BF_EAN_8;}
if (ui->checkBox_upca->isChecked()){ types |= BF_UPC_A;}
if (ui->checkBox_upce->isChecked()){ types |= BF_UPC_E;}
if (ui->checkBox_industrial25->isChecked()){ types |= BF_INDUSTRIAL_25;}
if (ui->checkBox_qrcode->isChecked()){ types |= BF_QR_CODE;}
if (ui->checkBox_pdf417->isChecked()){ types |= BF_PDF417;}
if (ui->checkBox_aztec->isChecked()){ types |= BF_AZTEC;}
if (ui->checkBox_maxicode->isChecked()){ types |= BF_MAXICODE;}
if (ui->checkBox_datamatrix->isChecked()){ types |= BF_DATAMATRIX;}
if (ui->checkBox_gs1->isChecked()){ types |= BF_GS1_COMPOSITE;}
if (ui->checkBox_patchcode->isChecked()){ types |= BF_PATCHCODE;}
if (ui->checkBox_dotcode->isChecked()){ types2 |= BF2_DOTCODE;}
if (ui->checkBox_postalcode->isChecked()){ types2 |= BF2_POSTALCODE;}
PublicRuntimeSettings settings;
DBR_GetRuntimeSettings(reader, &settings);
settings.deblurLevel = 0;
settings.timeout = 50;
settings.expectedBarcodesCount = 0;
settings.barcodeFormatIds = types;
settings.barcodeFormatIds_2 = types2;
DBR_UpdateRuntimeSettings(reader, &settings, errorMessage, 256);
// Decode barcode and draw results
QDateTime start = QDateTime::currentDateTime();
int ret = DBR_DecodeBuffer(reader, (unsigned char *)cp.bits(), cp.width(), cp.height(), cp.bytesPerLine(), IPF_ARGB_8888, "");
QDateTime end = QDateTime::currentDateTime();
TextResultArray *handler = NULL;
DBR_GetAllTextResults(reader, &handler);
if (handler->resultsCount == 0)
{
QString message = "No barcode found. Elapsed time: " + QString::number(start.msecsTo(end)) + "ms\n";
ui->textEdit_results->setText(message);
DBR_FreeTextResults(&handler);
ui->label->setPixmap(pm.scaled(ui->label->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
return true;
}
if (ui->checkBox_autostop->isChecked()) pause();
QString out = "";
TextResult **results = handler->results;
for (int index = 0; index < handler->resultsCount; index++)
{
LocalizationResult* localizationResult = results[index]->localizationResult;
out += "Index: " + QString::number(index) + ", Elapsed time: " + QString::number(start.msecsTo(end)) + "ms\n";
out += "Barcode format: " + QString(results[index]->barcodeFormatString) + "\n";
out += "Barcode value: " + QString(results[index]->barcodeText) + "\n";
out += "Bounding box: (" + QString::number(localizationResult->x1) + ", " + QString::number(localizationResult->y1) + ") "
+ "(" + QString::number(localizationResult->x2) + ", " + QString::number(localizationResult->y2) + ") "
+ "(" + QString::number(localizationResult->x3) + ", " + QString::number(localizationResult->y3) + ") "
+ "(" + QString::number(localizationResult->x4) + ", " + QString::number(localizationResult->y4) + ")\n";
out += "----------------------------------------------------------------------------------------\n";
painter.drawLine(localizationResult->x1, localizationResult->y1, localizationResult->x2, localizationResult->y2);
painter.drawLine(localizationResult->x2, localizationResult->y2, localizationResult->x3, localizationResult->y3);
painter.drawLine(localizationResult->x3, localizationResult->y3, localizationResult->x4, localizationResult->y4);
painter.drawLine(localizationResult->x4, localizationResult->y4, localizationResult->x1, localizationResult->y1);
}
DBR_FreeTextResults(&handler);
painter.end();
ui->label->setPixmap(pm.scaled(ui->label->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
ui->textEdit_results->setText(out);
}
return true;
}
return false;
}
void MyVideoSurface::setWorker(Work* worker)
{
this->worker = worker;
}
void MyVideoSurface::pause()
{
is_detecting = false;
worker->stop();
camera->stop();
queue.clear();
}
void MyVideoSurface::appendResult(std::vector<BarcodeInfo> &result)
{
mutex.lock();
if (queue.size() == 4) queue.clear();
queue.push_back(result);
mutex.unlock();
}