diff --git a/test/barcodes/multi.png b/test/barcodes/multi.png new file mode 100644 index 0000000..8af6490 Binary files /dev/null and b/test/barcodes/multi.png differ diff --git a/test/test_all.py b/test/test_all.py index ea404ea..4c5542c 100644 --- a/test/test_all.py +++ b/test/test_all.py @@ -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): diff --git a/zxing/__init__.py b/zxing/__init__.py index c165166..c66240d 100644 --- a/zxing/__init__.py +++ b/zxing/__init__.py @@ -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): @@ -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: @@ -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