-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
352 lines (300 loc) · 11.6 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
/*
* Copyright 2015 bvtest <email>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "mainwindow.h"
#include <iostream>
#include "./ui_mainwindow.h"
#include <stdio.h>
#include <fmt/format.h>
#include <QFileDialog>
#include <QMessageBox>
#include <pwd.h>
#include <QImage>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
_ui(new Ui::MainWindow)
{
_ui->setupUi(this);
_ui->statusBar->addPermanentWidget(_ui->lblCamera);
// Instantiate the barcode scanner.
_callback_user_data.pIC_BarcodeScanner = ICBarcode_CreateScanner();
_callback_user_data.BarCodes.resize(50);
// Configure the barcode scanner to try harder. If barcodes are hardly detected,
// pass an 1 instead of a 0.
ICBarcode_SetParam(_callback_user_data.pIC_BarcodeScanner, IC_BARCODEPARAMS_TRY_HARDER, 1);
// Enable barcode checksum support.
ICBarcode_SetParam(_callback_user_data.pIC_BarcodeScanner, IC_BARCODEPARAMS_CHECK_CHAR, 1);
// Define. which formats the barcode library shall detect.
int formats = 0;
formats |= ICBarcode_Format::IC_BARCODEFORMAT_CODE_128;
formats |= ICBarcode_Format::IC_BARCODEFORMAT_CODE_93;
formats |= ICBarcode_Format::IC_BARCODEFORMAT_EAN_13;
formats |= ICBarcode_Format::IC_BARCODEFORMAT_EAN_8;
formats |= ICBarcode_Format::IC_BARCODEFORMAT_UPC_A;
formats |= ICBarcode_Format::IC_BARCODEFORMAT_QR_CODE;
formats |= ICBarcode_Format::IC_BARCODEFORMAT_DATA_MATRIX;
//formats |= ICBarcode_Format::IC_BARCODEFORMAT_INTERLEAVED_2_OF_5;
//formats |= ICBarcode_Format::IC_BARCODEFORMAT_CODE_39;
ICBarcode_SetBarcodeFormats(_callback_user_data.pIC_BarcodeScanner, formats);
LoadLastUsedDevice();
}
MainWindow::~MainWindow()
{
delete _ui;
}
// File Menu
void MainWindow::on_actionLoad_Configuration_triggered()
{
_Grabber.CloseCamera();
QFileDialog fileDialog(this, tr("Save Configuration"),tr("./"), tr("Json Files (*.json)"));
fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
fileDialog.setDefaultSuffix("json");
if( fileDialog.exec())
{
_Grabber.loadDevice(fileDialog.selectedFiles().first().toStdString());
startVideo();
}
}
void MainWindow::on_actionSave_Configuration_triggered()
{
if( _Grabber.isDevValid() )
{
QFileDialog fileDialog(this, tr("Save Configuration"),tr("./"), tr("Json Files (*.json)"));
fileDialog.setAcceptMode(QFileDialog::AcceptSave);
fileDialog.setDefaultSuffix("json");
if( fileDialog.exec())
{
_Grabber.saveDevice(fileDialog.selectedFiles().first().toStdString());
}
}
}
void MainWindow::on_actionExit_triggered()
{
close();
}
// Device Menu
void MainWindow::on_actionSelect_triggered()
{
if( _Grabber.isLive() )
{
_Grabber.CloseCamera();
}
if( _Grabber.showDeviceSelectionDlg() )
{
_Grabber.saveDevice(getDeviceFile());
startVideo();
}
}
void MainWindow::on_actionProperties_triggered()
{
if( _Grabber.isDevValid() )
{
_Grabber.showPropertyDlg();
_Grabber.saveDevice(getDeviceFile());
}
}
void MainWindow::on_actionSave_Image_triggered()
{
QImage image = {};
_ui->statusBar->showMessage("Snapping image");
if( _Grabber.snapImage(image, 1000) )
{
QFileDialog fileDialog(this, tr("Save Iamge"),tr("./"), tr("PNG Files (*.png)"));
fileDialog.setDefaultSuffix("png");
fileDialog.setAcceptMode(QFileDialog::AcceptSave);
if( fileDialog.exec())
{
image.save(fileDialog.selectedFiles().first());
}
}
_ui->statusBar->showMessage("");
}
//////////////////////////////////////////////////////////////////////////
void MainWindow::startVideo()
{
if( _Grabber.isDevValid() )
{
_ui->lblCamera->setText(_Grabber.getDeviceName().c_str());
WId xwinid = _ui->VideoWidget->winId();
_Grabber.SetWindowHandle(xwinid);
_callback_user_data.framecount = 0;
_callback_user_data.pMainWindow = this;
//callback
_Grabber.set_new_frame_callback(this->NewImageCallback, &_callback_user_data);
_callback_user_data.busy = false;
if( !_Grabber.startLive() )
{
std::string msg = _Grabber.getDeviceName() +" "+ _Grabber.getSerialNumber() + "\n"+ _Grabber.GetLastErrorText();
QMessageBox::critical(this,tr("Error start video"),tr(msg.c_str()),QMessageBox::Ok);
}
}
}
std::string MainWindow::getDeviceFile()
{
std::string devicefile = getpwuid(getuid())->pw_dir;
devicefile += "/IC Barcode.json";
return devicefile;
}
void MainWindow::LoadLastUsedDevice()
{
_Grabber.loadDevice(getDeviceFile());
startVideo();
}
/////////////////////////////////////////////////////////////////////
// Callback function, called on each new image. Here the barcode
// finding is performed
GstFlowReturn MainWindow::NewImageCallback(GstAppSink *appsink, gpointer user_data)
{
if( user_data != nullptr)
{
auto data = (callback_user_data*)user_data;
data->framecount++;
if( data->pMainWindow != nullptr)
data->pMainWindow->postNewImageEvent(0,0);
if(data->busy)
return GST_FLOW_OK;
data->busy = true;
GstSample *sample = gst_app_sink_pull_sample(appsink);
data->sample = sample;
findBarcodes(data);
gst_sample_unref(sample);
data->busy = false;
}
return GST_FLOW_OK;
}
/////////////////////////////////////////////////////////////////////////
// This function finds the barcodes and stires the result in the
// callback user data.
void MainWindow::findBarcodes(callback_user_data *data)
{
GstBuffer *buffer = gst_sample_get_buffer(data->sample);
GstMapInfo info;
gst_buffer_map(buffer, &info, GST_MAP_READ);
if (info.data != NULL)
{
int width, height ;
const GstStructure *str;
// info.data contains the image data as blob of unsigned char
GstCaps *caps = gst_sample_get_caps(data->sample);
// Get a string containg the pixel format, width and height of the image
str = gst_caps_get_structure (caps, 0);
// Now query the width and height of the image
gst_structure_get_int (str, "width", &width);
gst_structure_get_int (str, "height", &height);
if( strcmp( gst_structure_get_string (str, "format"),"BGRx") == 0)
{
// If the buffer is a 32 bit RGB32 image, it must be converted to
// 8 bit grayscale. before barcodes can be searched.
std::vector<unsigned char> buffer;
buffer.resize(width * height);
ICBarcode_Transform_BGRA32_to_Y800(
buffer.data(), info.data,
width, height,
width * 4,
width
);
data->FoundBarcodes = ICBarcode_FindBarcodes(data->pIC_BarcodeScanner,
&buffer[0],
width,height,width,
data->BarCodes.data(), data->BarCodes.size());
}
else
{
data->FoundBarcodes = ICBarcode_FindBarcodes(data->pIC_BarcodeScanner,
info.data,
width,height,width,
data->BarCodes.data(), data->BarCodes.size());
}
//if(pCustomData->FoundBarcodes> 0)
data->pMainWindow->postBarcodeFoundEvent(1,2);
}
// Calling Unref is important!
gst_buffer_unmap (buffer, &info);
}
void MainWindow::customEvent(QEvent * event)
{
// When we get here, we've crossed the thread boundary and are now
// executing in the MainWindow's thread. We could ask more events here.
if(event->type() == NEW_IMAGE_EVENT)
{
onNewImageEvent(static_cast<NewImageEvent *>(event));
}
if(event->type() == BARCODE_FOUND_EVENT)
{
handleBarcodeFoundEvent(static_cast<BarcodeFoundEvent *>(event));
}
}
//////////////////////////////////////////////////////////////////////////////////////////
// Event handler, so we can call MainWindow functions from the camera thread.
// It is called from the callback function, which runs in Grabber's Gstreamer thread
void MainWindow::postNewImageEvent(const int customData1, const int customData2)
{
// This method (postMyCustomEvent) can be called from any thread
QApplication::postEvent(this, new NewImageEvent(customData1, customData2));
}
//////////////////////////////////////////////////////////////////////////////////////////
// Event handler, so we can call MainWindow functions from the camera thread.
void MainWindow::postBarcodeFoundEvent(const int customData1, const int customData2)
{
// This method (postMyCustomEvent) can be called from any thread
QApplication::postEvent(this, new BarcodeFoundEvent(customData1, customData2));
}
//////////////////////////////////////////////////////////////////////////////
// Here we are in the MainWindow thread and can access QWidgets.
void MainWindow::onNewImageEvent(const NewImageEvent *event)
{
_ui->lblCamera->setText(
fmt::format("{} : {} frames", _Grabber.getDeviceName(), _callback_user_data.framecount).c_str());
}
///////////////////////////////////////////////////////////////////////////////
// This event is called from the GStreamer callback of TCamcamera class.
// Barcodes are already deceted and they are in the _CustomData.FoundBarcodes
// member.
// The "svg" string will contain the xml string for the rectangles around the
// found barcodes.
//
void MainWindow::handleBarcodeFoundEvent(const BarcodeFoundEvent *event)
{
std::string BarcodeList = "";
std::string svg;
char primitive[1024];
svg = "<svg>\n";
if( _callback_user_data.FoundBarcodes == 0 )
return;
for( int i = 0 ; i < _callback_user_data.FoundBarcodes; i++)
{
BarcodeList += _callback_user_data.BarCodes[i].Text; // Get the contents of a decoded barcode
BarcodeList += "\n";
printf("%s\n",_callback_user_data.BarCodes[i].Text);
// Create a polygon using the x/y coordinates of the corner of a barcode in the image
svg += "\t<polygon points=\"";
for(int p = 0; p < _callback_user_data.BarCodes[i].ResultPointsLength; p++)
{
if( p > 0)
{
svg += ",";
}
sprintf(primitive,"%d,%d",
(int)_callback_user_data.BarCodes[i].ResultPoints[p].x,
(int)_callback_user_data.BarCodes[i].ResultPoints[p].y);
svg += primitive;
}
svg += "\" fill='none' stroke='red' stroke-width='3px'/>\n";
}
svg += "</svg>\n";
_Grabber.SetOverlayGraphicString(svg.c_str()); // Pass the graphics string to the overly
_ui->txtFoundBarcodes->setText(BarcodeList.c_str()); // Show the decoded barcodes in the barcode window.
}