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
|
/* GStreamer
* Copyright (C) 2009 Stefan Kost <ensonic@users.sf.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/**
* SECTION:element-zbar
*
* Detect bar codes in the video streams and send them as element messages to
* the #GstBus if .#GstZBar:message property is %TRUE.
*
* The element generate messages named
* <classname>"barcode"</classname>. The structure containes these
* fields:
* <itemizedlist>
* <listitem>
* <para>
* #GstClockTime
* <classname>"timestamp"</classname>:
* the timestamp of the buffer that triggered the message.
* </para>
* </listitem>
* <listitem>
* <para>
* gchar*
* <classname>"type"</classname>:
* the symbol type.
* </para>
* </listitem>
* <listitem>
* <para>
* gchar*
* <classname>"symbol"</classname>:
* the deteted bar code data.
* </para>
* </listitem>
* <listitem>
* <para>
* gint
* <classname>"quality"</classname>:
* an unscaled, relative quantity: larger values are better than smaller
* values.
* </para>
* </listitem>
* </itemizedlist>
*
* <refsect2>
* <title>Example launch lines</title>
* |[
* gst-launch -m v4l2src ! videoconvert ! zbar ! videoconvert ! xvimagesink
* ]| This pipeline will detect barcodes and send them as messages.
* |[
* gst-launch -m v4l2src ! tee name=t ! queue ! videoconvert ! zbar ! fakesink t. ! queue ! xvimagesink
* ]| Same as above, but running the filter on a branch to keep the display in color
* </refsect2>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gstzbar.h"
#include <string.h>
#include <math.h>
#include <gst/video/video.h>
GST_DEBUG_CATEGORY_STATIC (zbar_debug);
#define GST_CAT_DEFAULT zbar_debug
/* GstZBar signals and args */
enum
{
/* FILL ME */
LAST_SIGNAL
};
enum
{
PROP_0,
PROP_MESSAGE,
PROP_CACHE
};
#define DEFAULT_CACHE FALSE
#define DEFAULT_MESSAGE TRUE
/* FIXME: add YVU9 and YUV9 once we have a GstVideoFormat for that */
#define ZBAR_YUV_CAPS \
"{ Y800, I420, YV12, NV12, NV21, Y41B, Y42B }"
static GstStaticPadTemplate gst_zbar_src_template =
GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (ZBAR_YUV_CAPS))
);
static GstStaticPadTemplate gst_zbar_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (ZBAR_YUV_CAPS))
);
static void gst_zbar_finalize (GObject * object);
static void gst_zbar_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec);
static void gst_zbar_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec);
static gboolean gst_zbar_start (GstBaseTransform * base);
static gboolean gst_zbar_stop (GstBaseTransform * base);
static GstFlowReturn gst_zbar_transform_frame_ip (GstVideoFilter * vfilter,
GstVideoFrame * frame);
#define gst_zbar_parent_class parent_class
G_DEFINE_TYPE (GstZBar, gst_zbar, GST_TYPE_VIDEO_FILTER);
static void
gst_zbar_class_init (GstZBarClass * g_class)
{
GObjectClass *gobject_class;
GstElementClass *gstelement_class;
GstBaseTransformClass *trans_class;
GstVideoFilterClass *vfilter_class;
gobject_class = G_OBJECT_CLASS (g_class);
gstelement_class = GST_ELEMENT_CLASS (g_class);
trans_class = GST_BASE_TRANSFORM_CLASS (g_class);
vfilter_class = GST_VIDEO_FILTER_CLASS (g_class);
gobject_class->set_property = gst_zbar_set_property;
gobject_class->get_property = gst_zbar_get_property;
gobject_class->finalize = gst_zbar_finalize;
g_object_class_install_property (gobject_class, PROP_MESSAGE,
g_param_spec_boolean ("message", "message",
"Post a barcode message for each detected code",
DEFAULT_MESSAGE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_CACHE,
g_param_spec_boolean ("cache", "cache",
"Enable or disable the inter-image result cache",
DEFAULT_CACHE,
G_PARAM_READWRITE | GST_PARAM_MUTABLE_READY |
G_PARAM_STATIC_STRINGS));
gst_element_class_set_metadata (gstelement_class, "Barcode detector",
"Filter/Analyzer/Video",
"Detect bar codes in the video streams",
"Stefan Kost <ensonic@users.sf.net>");
gst_element_class_add_pad_template (gstelement_class,
gst_static_pad_template_get (&gst_zbar_sink_template));
gst_element_class_add_pad_template (gstelement_class,
gst_static_pad_template_get (&gst_zbar_src_template));
trans_class->start = GST_DEBUG_FUNCPTR (gst_zbar_start);
trans_class->stop = GST_DEBUG_FUNCPTR (gst_zbar_stop);
trans_class->transform_ip_on_passthrough = FALSE;
vfilter_class->transform_frame_ip =
GST_DEBUG_FUNCPTR (gst_zbar_transform_frame_ip);
}
static void
gst_zbar_init (GstZBar * zbar)
{
zbar->cache = DEFAULT_CACHE;
zbar->message = DEFAULT_MESSAGE;
zbar->scanner = zbar_image_scanner_create ();
}
static void
gst_zbar_finalize (GObject * object)
{
GstZBar *zbar = GST_ZBAR (object);
zbar_image_scanner_destroy (zbar->scanner);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
gst_zbar_set_property (GObject * object, guint prop_id, const GValue * value,
GParamSpec * pspec)
{
GstZBar *zbar;
g_return_if_fail (GST_IS_ZBAR (object));
zbar = GST_ZBAR (object);
switch (prop_id) {
case PROP_CACHE:
zbar->cache = g_value_get_boolean (value);
break;
case PROP_MESSAGE:
zbar->message = g_value_get_boolean (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_zbar_get_property (GObject * object, guint prop_id, GValue * value,
GParamSpec * pspec)
{
GstZBar *zbar;
g_return_if_fail (GST_IS_ZBAR (object));
zbar = GST_ZBAR (object);
switch (prop_id) {
case PROP_CACHE:
g_value_set_boolean (value, zbar->cache);
break;
case PROP_MESSAGE:
g_value_set_boolean (value, zbar->message);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static GstFlowReturn
gst_zbar_transform_frame_ip (GstVideoFilter * vfilter, GstVideoFrame * frame)
{
GstZBar *zbar = GST_ZBAR (vfilter);
gpointer data;
gint stride, height;
zbar_image_t *image;
const zbar_symbol_t *symbol;
int n;
image = zbar_image_create ();
/* all formats we support start with an 8-bit Y plane. zbar doesn't need
* to know about the chroma plane(s) */
data = GST_VIDEO_FRAME_COMP_DATA (frame, 0);
stride = GST_VIDEO_FRAME_COMP_STRIDE (frame, 0);
height = GST_VIDEO_FRAME_HEIGHT (frame);
zbar_image_set_format (image, GST_MAKE_FOURCC ('Y', '8', '0', '0'));
zbar_image_set_size (image, stride, height);
zbar_image_set_data (image, (gpointer) data, stride * height, NULL);
/* scan the image for barcodes */
n = zbar_scan_image (zbar->scanner, image);
if (n == 0)
goto out;
/* extract results */
symbol = zbar_image_first_symbol (image);
for (; symbol; symbol = zbar_symbol_next (symbol)) {
zbar_symbol_type_t typ = zbar_symbol_get_type (symbol);
const char *data = zbar_symbol_get_data (symbol);
gint quality = zbar_symbol_get_quality (symbol);
GST_DEBUG_OBJECT (zbar, "decoded %s symbol \"%s\" at quality %d",
zbar_get_symbol_name (typ), data, quality);
if (zbar->cache && zbar_symbol_get_count (symbol) != 0)
continue;
if (zbar->message) {
GstMessage *m;
GstStructure *s;
/* post a message */
s = gst_structure_new ("barcode",
"timestamp", G_TYPE_UINT64, GST_BUFFER_TIMESTAMP (frame->buffer),
"type", G_TYPE_STRING, zbar_get_symbol_name (typ),
"symbol", G_TYPE_STRING, data, "quality", G_TYPE_INT, quality, NULL);
m = gst_message_new_element (GST_OBJECT (zbar), s);
gst_element_post_message (GST_ELEMENT (zbar), m);
}
}
out:
/* clean up */
zbar_image_scanner_recycle_image (zbar->scanner, image);
zbar_image_destroy (image);
return GST_FLOW_OK;
}
static gboolean
gst_zbar_start (GstBaseTransform * base)
{
GstZBar *zbar = GST_ZBAR (base);
/* start the cache if enabled (e.g. for filtering dupes) */
zbar_image_scanner_enable_cache (zbar->scanner, zbar->cache);
return TRUE;
}
static gboolean
gst_zbar_stop (GstBaseTransform * base)
{
GstZBar *zbar = GST_ZBAR (base);
/* stop the cache if enabled (e.g. for filtering dupes) */
zbar_image_scanner_enable_cache (zbar->scanner, zbar->cache);
return TRUE;
}
static gboolean
plugin_init (GstPlugin * plugin)
{
GST_DEBUG_CATEGORY_INIT (zbar_debug, "zbar", 0, "zbar");
return gst_element_register (plugin, "zbar", GST_RANK_NONE, GST_TYPE_ZBAR);
}
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
GST_VERSION_MINOR,
zbar,
"zbar barcode scanner",
plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
|