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
|
//------------------------------------------------------------------------
// Copyright 2008-2009 (c) Jeff Brown <spadix@users.sourceforge.net>
//
// This file is part of the ZBar Bar Code Reader.
//
// The ZBar Bar Code Reader is free software; you can redistribute it
// and/or modify it under the terms of the GNU Lesser Public License as
// published by the Free Software Foundation; either version 2.1 of
// the License, or (at your option) any later version.
//
// The ZBar Bar Code Reader is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser Public License for more details.
//
// You should have received a copy of the GNU Lesser Public License
// along with the ZBar Bar Code Reader; if not, write to the Free
// Software Foundation, Inc., 51 Franklin St, Fifth Floor,
// Boston, MA 02110-1301 USA
//
// http://sourceforge.net/projects/zbar
//------------------------------------------------------------------------
#include "QZBarThread.h"
#include <iostream>
using namespace zbar;
static const QString textFormat("%1:%2");
QZBarThread::QZBarThread(int verbosity)
: _videoOpened(false), reqWidth(DEFAULT_WIDTH), reqHeight(DEFAULT_HEIGHT),
video(NULL), image(NULL), running(true), videoRunning(false),
videoEnabled(false)
{
zbar_set_verbosity(verbosity);
scanner.set_handler(*this);
}
void QZBarThread::image_callback(Image &image)
{
for (Image::SymbolIterator sym = image.symbol_begin();
sym != image.symbol_end(); ++sym)
if (!sym->get_count()) {
QString data = QString::fromStdString(sym->get_data());
emit decoded(sym->get_type(), data);
emit decodedText(textFormat.arg(
QString::fromStdString(sym->get_type_name()), data));
}
}
void QZBarThread::processImage(Image &image)
{
{
scanner.recycle_image(image);
Image tmp = image.convert(*(long *)"Y800");
scanner.scan(tmp);
image.set_symbols(tmp.get_symbols());
}
window.draw(image);
if (this->image && this->image != &image) {
delete this->image;
this->image = NULL;
}
emit update();
}
void QZBarThread::enableVideo(bool enable)
{
if (!video) {
videoRunning = videoEnabled = false;
return;
}
try {
scanner.enable_cache(enable);
video->enable(enable);
videoRunning = enable;
} catch (std::exception &e) {
std::cerr << "ERROR: " << e.what() << std::endl;
}
if (!enable) {
// release video image and revert to logo
clear();
emit update();
}
}
void QZBarThread::openVideo(const QString &device)
{
if (videoRunning)
enableVideo(false);
{
QMutexLocker locker(&mutex);
videoEnabled = _videoOpened = false;
}
// ensure old video doesn't have image ref
// (FIXME handle video destroyed w/images outstanding)
clear();
emit update();
if (video) {
delete video;
video = NULL;
emit videoOpened(false);
}
if (device.isEmpty())
return;
try {
std::string devstr = device.toStdString();
video = new Video(devstr);
if (reqWidth != DEFAULT_WIDTH || reqHeight != DEFAULT_HEIGHT)
video->request_size(reqWidth, reqHeight);
negotiate_format(*video, window);
{
QMutexLocker locker(&mutex);
videoEnabled = _videoOpened = true;
reqWidth = video->get_width();
reqHeight = video->get_height();
}
currentDevice = device;
emit videoOpened(true);
} catch (std::exception &e) {
std::cerr << "ERROR: " << e.what() << std::endl;
emit videoOpened(false);
}
}
void QZBarThread::videoDeviceEvent(VideoDeviceEvent *e)
{
openVideo(e->device);
}
void QZBarThread::videoEnabledEvent(VideoEnabledEvent *e)
{
if (videoRunning && !e->enabled)
enableVideo(false);
videoEnabled = e->enabled;
}
void QZBarThread::scanImageEvent(ScanImageEvent *e)
{
if (videoRunning)
enableVideo(false);
try {
image = new QZBarImage(e->image);
processImage(*image);
} catch (std::exception &e) {
std::cerr << "ERROR: " << e.what() << std::endl;
clear();
}
}
bool QZBarThread::event(QEvent *e)
{
switch ((EventType)e->type()) {
case VideoDevice:
videoDeviceEvent((VideoDeviceEvent *)e);
break;
case VideoEnabled:
videoEnabledEvent((VideoEnabledEvent *)e);
break;
case ScanImage:
scanImageEvent((ScanImageEvent *)e);
break;
case Exit:
if (videoRunning)
enableVideo(false);
running = false;
break;
case ReOpen:
openVideo(currentDevice);
break;
default:
return (false);
}
return (true);
}
void QZBarThread::run()
{
QEvent *e = NULL;
while (running) {
if (!videoEnabled) {
QMutexLocker locker(&mutex);
while (queue.isEmpty())
newEvent.wait(&mutex);
e = queue.takeFirst();
} else {
// release reference to any previous QImage
clear();
enableVideo(true);
while (videoRunning && !e) {
try {
Image image = video->next_image();
processImage(image);
} catch (std::exception &e) {
std::cerr << "ERROR: " << e.what() << std::endl;
enableVideo(false);
openVideo("");
}
QMutexLocker locker(&mutex);
if (!queue.isEmpty())
e = queue.takeFirst();
}
if (videoRunning)
enableVideo(false);
}
if (e) {
event(e);
delete e;
e = NULL;
}
}
clear();
openVideo("");
}
QVector<QPair<int, QString> > QZBarThread::get_menu(int index)
{
QVector<QPair<int, QString> > vector;
struct video_controls_s *ctrl;
if (!video)
return vector;
ctrl = video->get_controls(index);
if (!ctrl)
return vector;
for (unsigned int i = 0; i < ctrl->menu_size; i++)
vector.append(qMakePair((int)ctrl->menu[i].value,
QString::fromUtf8(ctrl->menu[i].name)));
return vector;
}
int QZBarThread::get_controls(int index, char **name, char **group,
enum QZBar::ControlType *type, int *min, int *max,
int *def, int *step)
{
struct video_controls_s *ctrl;
if (!video)
return 0;
ctrl = video->get_controls(index);
if (!ctrl)
return 0;
if (name)
*name = ctrl->name;
if (group)
*group = ctrl->group;
if (min)
*min = ctrl->min;
if (max)
*max = ctrl->max;
if (def)
*def = ctrl->def;
if (step)
*step = ctrl->step;
if (type) {
switch (ctrl->type) {
case VIDEO_CNTL_INTEGER:
*type = QZBar::Integer;
break;
case VIDEO_CNTL_MENU:
*type = QZBar::Menu;
break;
case VIDEO_CNTL_BUTTON:
*type = QZBar::Button;
break;
case VIDEO_CNTL_INTEGER64:
*type = QZBar::Integer64;
break;
case VIDEO_CNTL_STRING:
*type = QZBar::String;
break;
case VIDEO_CNTL_BOOLEAN:
*type = QZBar::Boolean;
break;
default:
*type = QZBar::Unknown;
break;
}
}
return 1;
}
int QZBarThread::set_control(char *name, bool value)
{
if (!video)
return 0;
return video->set_control(name, value);
}
int QZBarThread::set_control(char *name, int value)
{
if (!video)
return 0;
return video->set_control(name, value);
}
int QZBarThread::get_control(char *name, bool *value)
{
if (!video)
return 0;
return video->get_control(name, value);
}
int QZBarThread::get_control(char *name, int *value)
{
if (!video)
return 0;
return video->get_control(name, value);
}
void QZBarThread::request_size(unsigned width, unsigned height)
{
reqWidth = width;
reqHeight = height;
}
int QZBarThread::get_resolution(int index, unsigned &width, unsigned &height,
float &max_fps)
{
struct video_resolution_s *res;
if (!video)
return 0;
res = video->get_resolution(index);
if (!res)
return 0;
width = res->width;
height = res->height;
max_fps = res->max_fps;
return 1;
}
|