Skip to content

Commit 05bcb3d

Browse files
committed
Add support of multiple barcodes per file
1 parent 8b60ada commit 05bcb3d

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

test/barcodes/multi.png

1.47 KB
Loading

test/test_all.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,29 @@ def test_possible_formats():
5555
@with_setup(setup_reader)
5656
def test_decoding_multiple():
5757
reader = zxing.BarCodeReader()
58-
filenames = [os.path.join(test_barcode_dir, filename) for filename, expected_format, expected_raw in test_barcodes]
59-
for dec, (filename, expected_format, expected_raw) in zip(reader.decode(filenames, pure_barcode=True), test_barcodes):
58+
for filename, expected_format, expected_raw in test_barcodes:
59+
full_filename = os.path.join(test_barcode_dir, filename)
60+
dec = reader.decode(full_filename, pure_barcode=True)
6061
if dec.raw != expected_raw:
6162
raise AssertionError('{}: Expected {!r} but got {!r}'.format(filename, expected_raw, dec.parsed))
6263
if dec.format != expected_format:
6364
raise AssertionError('{}: Expected {!r} but got {!r}'.format(filename, expected_format, dec.format))
6465

6566

67+
@with_setup(setup_reader)
68+
def test_multi():
69+
reader = zxing.BarCodeReader()
70+
filename = "multi.png"
71+
full_filename = os.path.join(test_barcode_dir, filename)
72+
dec = reader.decode_as_list(full_filename, multi=True)
73+
74+
expected = ["This should be QR_CODE", "This should be PDF_417"]
75+
actual = [barcode.parsed for barcode in dec]
76+
77+
if expected != actual:
78+
raise AssertionError('{}: Expected {!r} but got {!r}'.format(filename, expected, actual))
79+
80+
6681
def test_parsing():
6782
dec = zxing.BarCode.parse("""
6883
file:///tmp/default.png (format: FAKE_DATA, type: TEXT):

zxing/__init__.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def __init__(self, classpath=None, java=None):
3131
else:
3232
self.classpath = os.path.join(os.path.dirname(__file__), 'java', '*')
3333

34-
def decode(self, filenames, try_harder=False, possible_formats=None, pure_barcode=False, products_only=False):
34+
def decode(self, filenames, try_harder=False, possible_formats=None, pure_barcode=False, products_only=False, multi=False):
3535
possible_formats = (possible_formats,) if isinstance(possible_formats, str) else possible_formats
3636

3737
if isinstance(filenames, str):
@@ -44,6 +44,9 @@ def decode(self, filenames, try_harder=False, possible_formats=None, pure_barcod
4444
cmd = [self.java, '-cp', self.classpath, self.cls] + file_uris
4545
if try_harder:
4646
cmd.append('--try_harder')
47+
if multi:
48+
cmd.append('--multi')
49+
one_file = False
4750
if pure_barcode:
4851
cmd.append('--pure_barcode')
4952
if products_only:
@@ -89,9 +92,13 @@ def decode(self, filenames, try_harder=False, possible_formats=None, pure_barcod
8992
else:
9093
# zxing (insanely) randomly reorders the output blocks, so we have to put them back in the
9194
# expected order, based on their URIs
92-
d = {c.uri: c for c in codes}
93-
return [d[f] for f in file_uris]
95+
return sorted(codes, key=lambda barcode: barcode and barcode.uri)
9496

97+
def decode_as_list(self, filenames, try_harder=False, possible_formats=None, pure_barcode=False, products_only=False, multi=False):
98+
result = self.decode(filenames, try_harder, possible_formats, pure_barcode, multi)
99+
if isinstance(result, list):
100+
return result
101+
return [result]
95102

96103
class CLROutputBlock(Enum):
97104
UNKNOWN = 0

0 commit comments

Comments
 (0)