Skip to content

Commit

Permalink
[#117] Support zbar library that does not provide orientation
Browse files Browse the repository at this point in the history
  • Loading branch information
quicklizard99 committed Oct 23, 2021
1 parent 20bab4d commit 6ded598
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
11 changes: 11 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,17 @@ symbol types
>>> decode(Image.open('pyzbar/tests/qrcode.png'), symbols=[ZBarSymbol.CODE128])
[]

ZBar versions
-------------

Development of the `original zbar <http:https://zbar.sourceforge.net/>`__ stopped in 2012.
Development was started again in 2019 under a `new project <https://github.com/mchehab/zbar/>`__
that has added some new features, including support for decoding
barcode orientation. At the time of writing the project does not produce Windows DLLs.
If you see `orientation=None` then your system has an older release of zbar.
The ``zbar`` ``DLL``\ s that are included with the Windows Python wheels are
an old build that does not support orientation.

Quality field
-------------
From
Expand Down
11 changes: 9 additions & 2 deletions pyzbar/pyzbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
)

__all__ = [
'decode', 'Point', 'Rect', 'Decoded', 'ZBarSymbol', 'EXTERNAL_DEPENDENCIES'
'decode', 'Point', 'Rect', 'Decoded', 'ZBarSymbol', 'EXTERNAL_DEPENDENCIES', 'ORIENTATION_AVAILABLE'
]


ORIENTATION_AVAILABLE = zbar_symbol_get_orientation is not None

Decoded = namedtuple('Decoded', 'data type rect polygon quality orientation')

# ZBar's magic 'fourcc' numbers that represent image formats
Expand Down Expand Up @@ -120,7 +122,12 @@ def _decode_symbols(symbols):
)
for index in _RANGEFN(zbar_symbol_get_loc_size(symbol))
)
orientation = ZBarOrientation(zbar_symbol_get_orientation(symbol)).name

if zbar_symbol_get_orientation:
orientation = ZBarOrientation(zbar_symbol_get_orientation(symbol)).name
else:
orientation = None

yield Decoded(
data=data,
type=symbol_type,
Expand Down
14 changes: 7 additions & 7 deletions pyzbar/tests/test_pyzbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

from pyzbar import wrapper
from pyzbar.pyzbar import (
decode, Decoded, Rect, ZBarSymbol, EXTERNAL_DEPENDENCIES
decode, Decoded, Rect, ZBarSymbol, EXTERNAL_DEPENDENCIES, ORIENTATION_AVAILABLE
)
from pyzbar.pyzbar_error import PyZbarError

Expand All @@ -40,15 +40,15 @@ class TestDecode(unittest.TestCase):
type='CODE128',
rect=Rect(left=37, top=550, width=324, height=76),
polygon=[(37, 551), (37, 625), (361, 626), (361, 550)],
orientation='UP',
orientation='UP' if ORIENTATION_AVAILABLE else None,
quality=77,
),
Decoded(
data=b'Rana temporaria',
type='CODE128',
rect=Rect(left=4, top=0, width=390, height=76),
polygon=[(4, 1), (4, 75), (394, 76), (394, 0)],
orientation='UP',
orientation='UP' if ORIENTATION_AVAILABLE else None,
quality=77,
),
]
Expand All @@ -59,7 +59,7 @@ class TestDecode(unittest.TestCase):
type='CODE128',
rect=Rect(left=4, top=0, width=390, height=75),
polygon=[(4, 1), (4, 75), (394, 74), (394, 0)],
orientation="UP",
orientation="UP" if ORIENTATION_AVAILABLE else None,
quality=76,
),
]
Expand All @@ -70,7 +70,7 @@ class TestDecode(unittest.TestCase):
type='QRCODE',
rect=Rect(left=27, top=27, width=145, height=145),
polygon=[(27, 27), (27, 172), (172, 172), (172, 27)],
orientation='UP',
orientation='UP' if ORIENTATION_AVAILABLE else None,
quality=1,
),
]
Expand All @@ -82,15 +82,15 @@ class TestDecode(unittest.TestCase):
type='QRCODE',
rect=Rect(left=173, top=10, width=205, height=205),
polygon=[(173, 113), (276, 215), (378, 113), (276, 10)],
orientation='UP',
orientation='UP' if ORIENTATION_AVAILABLE else None,
quality=1,
),
Decoded(
data=b'Thalassiodracon',
type='QRCODE',
rect=Rect(left=32, top=208, width=158, height=158),
polygon=[(32, 352), (177, 366), (190, 222), (46, 208)],
orientation='RIGHT',
orientation='RIGHT' if ORIENTATION_AVAILABLE else None,
quality=1,
),
]
Expand Down
16 changes: 11 additions & 5 deletions pyzbar/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,17 @@ def zbar_function(fname, restype, *args):
c_uint
)

zbar_symbol_get_orientation = zbar_function(
'zbar_symbol_get_orientation',
c_uint,
POINTER(zbar_symbol)
)

try:
zbar_symbol_get_orientation = zbar_function(
'zbar_symbol_get_orientation',
c_uint,
POINTER(zbar_symbol)
)
except AttributeError:
# This function not present in the original pre-20
zbar_symbol_get_orientation = None


zbar_symbol_next = zbar_function(
'zbar_symbol_next',
Expand Down

0 comments on commit 6ded598

Please sign in to comment.