Skip to content

Commit

Permalink
Add support of multiple barcodes per file
Browse files Browse the repository at this point in the history
  • Loading branch information
boring-hopper committed May 5, 2021
1 parent 8b60ada commit 55a1614
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
Binary file added test/barcodes/multi.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 17 additions & 2 deletions test/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,29 @@ def test_possible_formats():
@with_setup(setup_reader)
def test_decoding_multiple():
reader = zxing.BarCodeReader()
filenames = [os.path.join(test_barcode_dir, filename) for filename, expected_format, expected_raw in test_barcodes]
for dec, (filename, expected_format, expected_raw) in zip(reader.decode(filenames, pure_barcode=True), test_barcodes):
for filename, expected_format, expected_raw in test_barcodes:
full_filename = os.path.join(test_barcode_dir, filename)
dec = reader.decode(full_filename, pure_barcode=True)
if dec.raw != expected_raw:
raise AssertionError('{}: Expected {!r} but got {!r}'.format(filename, expected_raw, dec.parsed))
if dec.format != expected_format:
raise AssertionError('{}: Expected {!r} but got {!r}'.format(filename, expected_format, dec.format))


@with_setup(setup_reader)
def test_multi():
reader = zxing.BarCodeReader()
filename = "multi.png"
full_filename = os.path.join(test_barcode_dir, filename)
dec = reader.decode_as_list(full_filename, multi=True)

expected = ["This should be QR_CODE", "This should be PDF_417"]
actual = [barcode.parsed for barcode in dec]

if expected != actual:
raise AssertionError('{}: Expected {!r} but got {!r}'.format(filename, expected, actual))


def test_parsing():
dec = zxing.BarCode.parse("""
file:///tmp/default.png (format: FAKE_DATA, type: TEXT):
Expand Down
13 changes: 10 additions & 3 deletions zxing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(self, classpath=None, java=None):
else:
self.classpath = os.path.join(os.path.dirname(__file__), 'java', '*')

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

if isinstance(filenames, str):
Expand All @@ -44,6 +44,9 @@ def decode(self, filenames, try_harder=False, possible_formats=None, pure_barcod
cmd = [self.java, '-cp', self.classpath, self.cls] + file_uris
if try_harder:
cmd.append('--try_harder')
if multi:
cmd.append('--multi')
one_file = False
if pure_barcode:
cmd.append('--pure_barcode')
if products_only:
Expand Down Expand Up @@ -89,9 +92,13 @@ def decode(self, filenames, try_harder=False, possible_formats=None, pure_barcod
else:
# zxing (insanely) randomly reorders the output blocks, so we have to put them back in the
# expected order, based on their URIs
d = {c.uri: c for c in codes}
return [d[f] for f in file_uris]
return sorted(codes, key=lambda barcode: barcode and barcode.uri)

def decode_as_list(self, filenames, try_harder=False, possible_formats=None, pure_barcode=False, multi=False):
result = self.decode(filenames, try_harder, possible_formats, pure_barcode, multi)
if isinstance(result, list):
return result
return [result]

class CLROutputBlock(Enum):
UNKNOWN = 0
Expand Down

0 comments on commit 55a1614

Please sign in to comment.