summaryrefslogtreecommitdiffstats
path: root/python/examples
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-03-09 00:06:44 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-03-09 00:06:44 +0000
commit44cf8ec67278bd1ab6c7f83a9993f7a5686a9541 (patch)
tree5eec4b0d1a3f163d279c3c27c03324ba49fa235a /python/examples
parentInitial commit. (diff)
downloadzbar-44cf8ec67278bd1ab6c7f83a9993f7a5686a9541.tar.xz
zbar-44cf8ec67278bd1ab6c7f83a9993f7a5686a9541.zip
Adding upstream version 0.23.93.upstream/0.23.93upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'python/examples')
-rw-r--r--python/examples/processor.py35
-rw-r--r--python/examples/read_one.py29
-rw-r--r--python/examples/scan_image.py31
3 files changed, 95 insertions, 0 deletions
diff --git a/python/examples/processor.py b/python/examples/processor.py
new file mode 100644
index 0000000..b8f20f0
--- /dev/null
+++ b/python/examples/processor.py
@@ -0,0 +1,35 @@
+#!/usr/bin/env python
+from sys import argv
+import zbar
+
+# create a Processor
+proc = zbar.Processor()
+
+# configure the Processor
+proc.parse_config('enable')
+
+# initialize the Processor
+device = '/dev/video0'
+if len(argv) > 1:
+ device = argv[1]
+proc.init(device)
+
+# setup a callback
+def my_handler(proc, image, closure):
+ # extract results
+ for symbol in image.symbols:
+ # do something useful with results
+ print('decoded', symbol.type, 'symbol', '"%s"' % symbol.data)
+
+proc.set_data_handler(my_handler)
+
+# enable the preview window
+proc.visible = True
+
+# initiate scanning
+proc.active = True
+try:
+ # keep scanning until user provides key/mouse input
+ proc.user_wait()
+except zbar.WindowClosed as e:
+ pass
diff --git a/python/examples/read_one.py b/python/examples/read_one.py
new file mode 100644
index 0000000..7a18c99
--- /dev/null
+++ b/python/examples/read_one.py
@@ -0,0 +1,29 @@
+#!/usr/bin/env python
+from sys import argv
+import zbar
+
+# create a Processor
+proc = zbar.Processor()
+
+# configure the Processor
+proc.parse_config('enable')
+
+# initialize the Processor
+device = '/dev/video0'
+if len(argv) > 1:
+ device = argv[1]
+proc.init(device)
+
+# enable the preview window
+proc.visible = True
+
+# read at least one barcode (or until window closed)
+proc.process_one()
+
+# hide the preview window
+proc.visible = False
+
+# extract results
+for symbol in proc.results:
+ # do something useful with results
+ print('decoded', symbol.type, 'symbol', '"%s"' % symbol.data)
diff --git a/python/examples/scan_image.py b/python/examples/scan_image.py
new file mode 100644
index 0000000..e26cb42
--- /dev/null
+++ b/python/examples/scan_image.py
@@ -0,0 +1,31 @@
+#!/usr/bin/env python
+from sys import argv
+import zbar
+from PIL import Image
+
+if len(argv) < 2: exit(1)
+
+# create a reader
+scanner = zbar.ImageScanner()
+
+# configure the reader
+scanner.parse_config('enable')
+
+# obtain image data
+pil = Image.open(argv[1]).convert('L')
+width, height = pil.size
+raw = pil.tobytes()
+
+# wrap image data
+image = zbar.Image(width, height, 'Y800', raw)
+
+# scan the image for barcodes
+scanner.scan(image)
+
+# extract results
+for symbol in image:
+ # do something useful with results
+ print('decoded', symbol.type, 'symbol', '"%s"' % symbol.data)
+
+# clean up
+del(image)