Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of multiple barcodes per file #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, products_only=False, multi=False):
result = self.decode(filenames, try_harder, possible_formats, pure_barcode, products_only, multi)
if isinstance(result, list):
return result
return [result]

class CLROutputBlock(Enum):
UNKNOWN = 0
Expand Down