-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBarcodeScanner.cpp
375 lines (322 loc) · 12.9 KB
/
BarcodeScanner.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
//////////////////////////////////////////////////////////////////
//
// BarcodeScanner
// Scan and lookup barcodes
//
// Copyright (c) 2022, Relaxed Communications GmbH <[email protected]>
//
// This work is published under the GNU Public License
//
//////////////////////////////////////////////////////////////////
#include "BarcodeScanner.h"
#include <QLabel>
#include <QComboBox>
#include <QCheckBox>
#include <QGroupBox>
#include <QPushButton>
#include <QtWidgets>
#include <QMessageBox>
#include <QTextStream>
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkReply>
#include <QSettings>
#include <gst/video/videooverlay.h>
#ifdef Q_OS_WINDOWS
#define strcasecmp stricmp
#endif
BarcodeScanner::BarcodeScanner(QWidget *parent)
: QDialog(parent)
{
pipeline = NULL;
bus = NULL;
gst_event_timer = NULL;
settings = new QSettings("ean-search.org", "BarcodeScanner");
// Qt dialog init
camera = new QComboBox();
QFont cameraFont = camera->font();
cameraFont.setPointSize(FONT_SIZE);
camera->setFont(cameraFont);
// camera device detection
GstDeviceMonitor* monitor = gst_device_monitor_new();
gst_device_monitor_add_filter(monitor, "Video/Source", NULL);
if (gst_device_monitor_start(monitor)) {
GList* devices = gst_device_monitor_get_devices(monitor);
for (GList* devIter = g_list_first(devices); devIter != NULL; devIter = g_list_next(devIter)) {
GstDevice* device = (GstDevice*)devIter->data;
if (device == NULL)
continue;
gchar* name = gst_device_get_display_name(device);
const gchar* device_handle = name; // name on Windows, device path on Linux
GstStructure* props = gst_device_get_properties(device);
if (props) {
const gchar* api = gst_structure_get_string(props, "device.api");
if (api && strcmp("v4l2", api) == 0) {
device_handle = gst_structure_get_string(props, "device.path");
}
}
QString data;
QTextStream(&data) << device_handle << "|" << "";
camera->addItem(name, data);
g_free(name);
if (props)
gst_structure_free(props);
}
g_list_free(devices);
gst_device_monitor_stop(monitor);
}
gst_object_unref(monitor);
eanSearchToken = new QLineEdit();
QFont tokenFont = eanSearchToken->font();
tokenFont.setPointSize(FONT_SIZE);
eanSearchToken->setFont(tokenFont);
eanSearchToken->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
eanSearchToken->setMinimumWidth(500);
eanSearchToken->setText(settings->value("token", "").toString());
errorMsg = new QLabel("");
errorMsg->setStyleSheet("QLabel { color : red; }");
startButton = new QPushButton(tr(" Start scanning "));
QFont largetFont = startButton->font();
largetFont.setPointSize(FONT_SIZE);
startButton->setFont(largetFont);
stopButton = new QPushButton(tr(" Stop scanning "));
stopButton->setFont(largetFont);
stopButton->setEnabled(false);
aboutButton = new QPushButton(tr(" About "));
aboutButton->setFont(largetFont);
quitButton = new QPushButton(tr("Quit"));
quitButton->setFont(largetFont);
productInfo = new QLabel(" \n ");
productInfo->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
productInfo->setMinimumWidth(500);
productInfo->setMinimumHeight(5 * FONT_SIZE);
productInfo->setStyleSheet("QLabel { color : blue; }");
productInfo->setTextFormat(Qt::RichText);
productInfo->setTextInteractionFlags(Qt::TextBrowserInteraction);
productInfo->setOpenExternalLinks(true);
videoArea = new QLabel("");
videoArea->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
videoArea->setMinimumWidth(640);
videoArea->setMinimumHeight(480);
videoArea->setAttribute(Qt::WA_NativeWindow); // deeded for GStreamer targets
connect(startButton, SIGNAL(clicked()), this, SLOT(startClicked()));
connect(stopButton, SIGNAL(clicked()), this, SLOT(stopClicked()));
connect(aboutButton, SIGNAL(clicked()), this, SLOT(aboutClicked()));
connect(quitButton, SIGNAL(clicked()), this, SLOT(quitClicked()));
QVBoxLayout* mainLayout = new QVBoxLayout();
QGroupBox* camSettingsBox = new QGroupBox();
QHBoxLayout* camSettingsBoxLayout = new QHBoxLayout();
camSettingsBoxLayout->addWidget(camera);
camSettingsBox->setLayout(camSettingsBoxLayout);
camSettingsBox->setTitle(tr("Camera Settings"));
mainLayout->addWidget(camSettingsBox);
QGroupBox* tokenBox = new QGroupBox();
QHBoxLayout* tokenBoxLayout = new QHBoxLayout();
tokenBoxLayout->addWidget(eanSearchToken);
tokenBox->setLayout(tokenBoxLayout);
tokenBox->setTitle(tr("ean-search.org API Token (optional)"));
mainLayout->addWidget(tokenBox);
mainLayout->addWidget(errorMsg);
mainLayout->addSpacing(FONT_SIZE / 2);
QGridLayout * buttonLayout = new QGridLayout();
buttonLayout->addWidget(startButton,0, 0);
buttonLayout->addWidget(stopButton, 0, 1);
buttonLayout->addWidget(aboutButton, 1, 0);
buttonLayout->addWidget(quitButton, 1, 1);
mainLayout->addLayout(buttonLayout);
mainLayout->addSpacing(FONT_SIZE);
QGroupBox* productBox = new QGroupBox();
QHBoxLayout* productBoxLayout = new QHBoxLayout();
productBoxLayout->addWidget(productInfo);
productBox->setLayout(productBoxLayout);
productBox->setTitle(tr("Product Information"));
mainLayout->addWidget(productBox);
mainLayout->addSpacing(FONT_SIZE);
mainLayout->addWidget(videoArea);
mainLayout->addStretch();
setLayout(mainLayout);
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint); // remove "?" button
setWindowFlags(windowFlags() | Qt::WindowMinimizeButtonHint); // add minimize button
setWindowTitle(tr("BarcodeScanner") + " " + VERSION);
setFixedWidth(sizeHint().width());
setFixedHeight(sizeHint().height());
// can't start without any camera
if (!camera->count()) {
camera->setEnabled(false);
startButton->setEnabled(false);
}
}
BarcodeScanner::~BarcodeScanner()
{
settings->setValue("token", eanSearchToken->text());
delete settings;
}
void BarcodeScanner::dispayErrorAndExit(const QString & msg)
{
QMessageBox::critical(this, "Terminating", msg);
exit(1);
}
void BarcodeScanner::startClicked()
{
startButton->setEnabled(false);
stopButton->setEnabled(true);
stopButton->setFocus(Qt::OtherFocusReason);
camera->setEnabled(false);
QStringList camera_data = camera->currentData().toString().split("|");
QString camera_handle = camera_data[0];
productInfo->setText("");
// Build the pipeline
#ifdef Q_OS_UNIX
QString videoSrc = "v4l2src do-timestamp=true name=videosrc";
QString videoSink = "xvimagesink name=imagesink";
#else
QString videoSrc = "ksvideosrc do-stats=true name=videosrc";
QString videoSink = "autovideosink"; // TODO: can't be embedded into dialog, detected by missing name=
#endif
//QString pipeline_str = videoSrc + QString(" ! videoconvert ! video/x-raw,width=640,height=480,framerate=10/1 ! zbar name=zbar ! videoconvert ! autovideosink");
QString pipeline_str = videoSrc
+ QString(" ! videoconvert ! video/x-raw,width=640,height=480,framerate=10/1 ! zbar name=zbar ! videoconvert ! ")
+ videoSink;
//QMessageBox::information(this, "Debug: Pipeline", pipeline_str); // debug
GError *error = NULL;
pipeline = gst_parse_launch((const char *)pipeline_str.toLatin1(), &error);
if (!pipeline || error) {
dispayErrorAndExit(QString("Pipeline error: ") + error->message + "\nIs GStreamer installed corectly ?");
}
bus = gst_element_get_bus(pipeline);
gst_event_timer = new QTimer(this);
connect(gst_event_timer, SIGNAL(timeout()), this, SLOT(checkGstBus()));
// select camera
GstElement* videosrc = gst_bin_get_by_name(GST_BIN(pipeline), "videosrc");
if (!videosrc) {
dispayErrorAndExit("Error: Can't get videosrc!");
}
#ifdef Q_OS_UNIX
g_object_set(videosrc, "device", camera_handle.toStdString().c_str(), NULL); // v4l2
#endif
#ifdef Q_OS_WINDOWS
g_object_set(videosrc, "device-name", camera_handle.toStdString().c_str(), NULL); // ksvideo
#endif
gst_object_unref(videosrc);
GstElement* imagesink = gst_bin_get_by_name(GST_BIN(pipeline), "imagesink");
if (!imagesink) {
videoArea->hide();
setFixedHeight(sizeHint().height());
} else {
gst_element_set_state(imagesink, GST_STATE_READY);
gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(imagesink) , videoArea->winId());
gst_object_unref(imagesink);
}
// Start the pipeline
gst_element_set_state(pipeline, GST_STATE_PLAYING);
gst_event_timer->start(POLL_TIMER);
}
void BarcodeScanner::stopClicked()
{
startButton->setEnabled(true);
startButton->setFocus(Qt::OtherFocusReason);
stopButton->setEnabled(false);
camera->setEnabled(true);
errorMsg->setText("");
last_symbol = "";
// Stop the pipeline
gst_event_timer->stop();
delete gst_event_timer;
gst_event_timer = NULL;
gst_element_set_state(pipeline, GST_STATE_NULL);
gst_object_unref(bus);
bus = NULL;
gst_object_unref(pipeline);
pipeline = NULL;
}
void BarcodeScanner::aboutClicked()
{
//QMessageBox::about(this, "ean-search.org Barcode Scanner", "© 2022 Relaxed Comunications GmbH, https://www.ean-search.org/" );
QMessageBox *msgBox = new QMessageBox(this);
msgBox->setAttribute(Qt::WA_DeleteOnClose);
msgBox->setWindowTitle("ean-search.org Barcode Scanner " + VERSION);
msgBox->setText("Barcode Scanner " + VERSION + "<p>Get your API token from <a href='https://www.ean-search.org/ean-database-api.html'>ean-search.org</a> to automatically lookup the scanned barcodes.");
msgBox->setInformativeText("© 2022 Relaxed Comunications GmbH<br><a href='https://www.ean-search.org/'>www.ean-search.org</a>");
msgBox->show();
}
void BarcodeScanner::quitClicked()
{
BarcodeScanner::close();
}
void BarcodeScanner::checkGstBus()
{
if (!gst_event_timer || !bus)
return;
gst_event_timer->stop();
GstMessage* msg = gst_bus_pop_filtered(bus, GstMessageType(GST_MESSAGE_ELEMENT | GST_MESSAGE_ERROR | GST_MESSAGE_EOS | GST_MESSAGE_CLOCK_LOST));
if (msg) {
if (msg && msg->type & GST_MESSAGE_ELEMENT) {
const GstStructure* s = gst_message_get_structure(msg);
const gchar* name = gst_structure_get_name(s);
if (strcmp(name, "barcode") == 0) {
const GValue* type_val = gst_structure_get_value(s, "type");
const gchar * type = g_value_get_string(type_val);
if (strcmp(type, "EAN-13") == 0 || strcmp(type, "UPC-A") == 0) {
const GValue* symbol_val = gst_structure_get_value(s, "symbol");
const gchar * symbol = g_value_get_string(symbol_val);
if (last_symbol != symbol) {
last_symbol = symbol;
EANLookup(symbol);
}
} else if (strcmp(type, "EAN-8") == 0 || strcmp(type, "UPC-E") == 0) {
productInfo->setText(QString("Barcode ") + type + QString(" is not globally unique"));
} else {
productInfo->setText(QString("") + type + QString(" is not an EAN/UPC product barcode"));
}
}
}
if (msg->type & GST_MESSAGE_EOS) {
errorMsg->setText("Error: End of stream");
}
if (msg && msg->type & GST_MESSAGE_CLOCK_LOST) {
/* Get a new clock */
errorMsg->setText("Error: Lost clock, resetting");
gst_element_set_state(pipeline, GST_STATE_PAUSED);
gst_element_set_state(pipeline, GST_STATE_PLAYING);
}
if (msg->type & GST_MESSAGE_ERROR) {
GError* err;
gchar* debug;
gst_message_parse_error(msg, &err, &debug);
if (msg->src && msg->src->name) {
errorMsg->setText(QString("Error in ") + msg->src->name + ": " + err->message);
}
else {
errorMsg->setText(QString("Error: ") + err->message);
}
g_error_free(err);
g_free(debug);
}
gst_message_unref(msg);
}
gst_event_timer->start(POLL_TIMER);
}
void BarcodeScanner::EANLookup(const QString & ean) {
QString token = eanSearchToken->text(); // get token from UI
if (token.isEmpty()) {
productInfo->setText("EAN " + ean + "<br>No API token for name lookup");
} else {
QNetworkRequest request = QNetworkRequest(QUrl("https://api.ean-search.org/api?token=" + token + "&format=json&op=barcode-lookup&ean=" + ean));
QNetworkReply * reply = network_manager.get(request);
request.setRawHeader("User-Agent", "BarcodeScanner (Qt)");
// connect to signal when its done using lambda
QObject::connect(reply, &QNetworkReply::finished, [=]() {
QString ReplyText = reply->readAll();
reply->deleteLater(); // clean up
if (ReplyText.contains("error") && ReplyText.contains("Invalid token")) {
productInfo->setText("EAN " + ean + "<br>Invalid <a href='https://www.ean-search.org/ean-database-api.html'>API</a> token");
eanSearchToken->setText("");
return;
}
QJsonDocument doc = QJsonDocument::fromJson(ReplyText.toUtf8());
QJsonValue obj0 = doc.array()[0];
QString name = obj0[QString("name")].toString();
productInfo->setText("EAN " + ean + "<br><a href='https://www.ean-search.org/ean/" + ean + "'>" + name + "</a>");
});
}
}