-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathmainwindow.cpp
392 lines (344 loc) · 12 KB
/
mainwindow.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#include "mainwindow.h"
#include "./ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
setAcceptDrops(true);
worker = NULL;
thread = NULL;
camera = NULL;
// Dynamsoft Barcode Reader
reader = DBR_CreateInstance();
// Open a file.
connect(ui->actionOpen_File, SIGNAL(triggered()), this, SLOT(openFile()));
// Open a folder.
connect(ui->actionOpen_Folder, SIGNAL(triggered()), this, SLOT(openFolder()));
// List widget event.
connect(ui->listWidget, SIGNAL(itemClicked(QListWidgetItem *)), this, SLOT(listWidgetClicked(QListWidgetItem *)));
// Export template.
connect(ui->actionExport_template, SIGNAL(triggered()), this, SLOT(exportTemplate()));
// About dialog.
connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(about()));
// Set license.
connect(ui->actionEnter_License_Key, SIGNAL(triggered()), this, SLOT(setLicense()));
// Template load button
connect(ui->pushButton_template, SIGNAL(clicked()), this, SLOT(loadTemplate()));
// Template export button
connect(ui->pushButton_export_template, SIGNAL(clicked()), this, SLOT(exportTemplate()));
// Camera button
connect(ui->pushButton_open, SIGNAL(clicked()), this, SLOT(startCamera()));
connect(ui->pushButton_stop, SIGNAL(clicked()), this, SLOT(stopCamera()));
// Cameras
QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
if (cameras.size() > 0)
{
for (int i = 0; i < cameras.count(); ++i)
{
QCameraInfo cameraInfo = cameras.at(i);
qDebug() << cameraInfo.deviceName();
qDebug() << cameraInfo.description();
camera = new QCamera(cameraInfo);
surface = new MyVideoSurface(this, ui, reader, camera);
camera->setViewfinder(surface);
break;
}
}
else
{
ui->pushButton_open->setEnabled(false);
ui->pushButton_stop->setEnabled(false);
}
}
MainWindow::~MainWindow()
{
camera->stop();
delete ui;
DBR_DestroyInstance(reader);
delete camera;
delete surface;
}
void MainWindow::loadFile(QString fileName)
{
// Add to list
ui->listWidget->addItem(fileName);
ui->statusbar->showMessage(fileName);
// Load the image file to QImage
QImage image(fileName);
showImage(image, fileName);
}
void MainWindow::openFile()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "", tr("Barcode images (*)"));
if (!fileName.isEmpty())
{
loadFile(fileName);
}
}
void MainWindow::openFolder()
{
QString folderName = QFileDialog::getExistingDirectory(this, tr("Open Folder"), "", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
if (!folderName.isEmpty())
{
// Get all files in the folder
QStringList fileNames = QDir(folderName).entryList(QDir::Files | QDir::NoDotAndDotDot);
// Add to list
for (int i = 0; i < fileNames.size(); i++)
{
ui->listWidget->addItem(QDir::cleanPath(folderName + QDir::separator() + fileNames.at(i)));
}
ui->statusbar->showMessage(folderName);
}
}
void MainWindow::listWidgetClicked(QListWidgetItem *item)
{
ui->statusbar->showMessage(QString(item->text()));
// Load the image file to QImage
QImage image(item->text());
showImage(image, item->text());
}
void MainWindow::showImage(const QImage &image, QString fileName)
{
ui->textEdit_results->setText("");
if (!image.isNull())
{
QPixmap pm = QPixmap::fromImage(image);
QPainter painter(&pm);
painter.setPen(Qt::red);
/************************
* Barcode detection.
************************/
// Get the template content and initialize the runtime settings.
QString content = ui->textEdit_template->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 = 5;
// settings.expectedBarcodesCount = 0;
settings.barcodeFormatIds = types;
settings.barcodeFormatIds_2 = types2;
DBR_UpdateRuntimeSettings(reader, &settings, errorMessage, 256);
QDateTime start = QDateTime::currentDateTime();
int errorCode = DBR_DecodeFile(reader, fileName.toStdString().c_str(), "");
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;
}
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);
}
}
void MainWindow::exportTemplate()
{
QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), "", tr("Barcode Template (*.json)"));
QFile file(fileName);
if (file.open(QIODevice::ReadWrite))
{
QTextStream stream(&file);
char *pContent = NULL;
DBR_OutputSettingsToStringPtr(reader, &pContent, "currentRuntimeSettings");
stream << QString(pContent);
DBR_FreeSettingsString(&pContent);
}
}
void MainWindow::about()
{
showMessageBox(tr("About"), tr("<a href='https://www.dynamsoft.com/barcode-reader/overview/'>Dynamsoft Barcode Reader</a>"));
}
void MainWindow::showMessageBox(QString title, QString content)
{
QMessageBox::information(this, title, content);
}
void MainWindow::closeEvent(QCloseEvent *event)
{
QMessageBox::StandardButton reply = QMessageBox::question(this, tr("Exit"), tr("Are you sure to quit?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
if (reply == QMessageBox::Yes)
{
// stopCamera();
event->accept();
}
else
{
event->ignore();
}
}
void MainWindow::setLicense()
{
QString licenseKey = QInputDialog::getText(this, tr("Enter License Key"), tr("License Key:"));
char errorMsgBuffer[512];
int ret = DBR_InitLicense(licenseKey.toStdString().c_str(), errorMsgBuffer, 512);
if (ret != 0)
{
showMessageBox(tr("License Key"), tr("License key is invalid."));
}
else
{
showMessageBox(tr("License Key"), tr("Dynamsoft Barcode Reader is activated successfully!"));
}
}
void MainWindow::loadTemplate()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "", tr("Barcode Template (*.json)"));
QFile file(fileName);
if (file.open(QIODevice::ReadOnly))
{
QTextStream stream(&file);
QString content = stream.readAll();
// DBR_LoadSettingsFromStringPtr(reader, content.toStdString().c_str());
ui->textEdit_template->setText(content);
}
}
void MainWindow::startCamera()
{
surface->reset();
thread = new QThread(this);
worker = new Work(ui, reader, surface); // Do not set a parent. The object cannot be moved if it has a parent.
worker->moveToThread(thread);
connect(thread, SIGNAL(finished()), worker, SLOT(deleteLater()));
connect(thread, SIGNAL(started()), worker, SLOT(detectBarcode()));
connect(worker, SIGNAL(update(QString)), this, SLOT(updateUI(QString)));
thread->start();
surface->setWorker(worker);
camera->start();
}
void MainWindow::stopCamera()
{
if (worker)
worker->stop();
if (camera && camera->state() == QCamera::ActiveState)
{
qDebug() << camera->state() << endl;
camera->stop();
}
if (thread)
{
thread->quit();
thread->wait();
}
worker = NULL;
thread = NULL;
}
void MainWindow::updateUI(QString results)
{
worker->updateUI(results);
}
void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
event->acceptProposedAction();
}
void MainWindow::dropEvent(QDropEvent *event)
{
const QMimeData *mimeData = event->mimeData();
foreach (QUrl url, event->mimeData()->urls())
{
QString fileName = url.toLocalFile();
loadFile(fileName);
}
event->acceptProposedAction();
}