Skip to content
This repository was archived by the owner on May 7, 2026. It is now read-only.

Commit d583ab0

Browse files
committed
add pure_barcode option, and use it in tests
1 parent 1a70647 commit d583ab0

3 files changed

Lines changed: 7 additions & 5 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ BarCode(raw='This should be QR_CODE', parsed='This should be QR_CODE', format='Q
2929
The attributes of the decoded `BarCode` object are `raw`, `parsed`, `format`, `type`, and `points`. The list of formats which ZXing can decode is
3030
[here](https://zxing.github.io/zxing/apidocs/com/google/zxing/BarcodeFormat.html).
3131

32-
The `decode()` method accepts an image path (or list of paths) and takes optional parameters `try_harder` (boolean) and `possible_formats` (list of formats to consider).
32+
The `decode()` method accepts an image path (or list of paths) and takes optional parameters `try_harder` (boolean), `possible_formats` (list of formats to consider), and `pure_barcode` (boolean).
3333
If no barcode is found, it returns `None`, and if it encounters any other recognizable error from the Java ZXing library, it raises `BarCodeReaderException`.
3434

3535
## Command-line interface

test/test_all.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def test_decoding():
3232
for filename, expected_format, expected_raw in test_barcodes:
3333
path = os.path.join(test_barcode_dir, filename)
3434
logging.debug('Trying to parse {}, expecting {!r}.'.format(path, expected_raw))
35-
dec = test_reader.decode(path)
35+
dec = test_reader.decode(path, pure_barcode=True)
3636
if dec.raw != expected_raw:
3737
raise AssertionError('Expected {!r} but got {!r}'.format(expected_raw, dec.raw))
3838
if dec.format != expected_format:
@@ -43,7 +43,7 @@ def test_decoding():
4343
def test_decoding_multiple():
4444
reader = zxing.BarCodeReader()
4545
filenames = [os.path.join(test_barcode_dir, filename) for filename, expected_format, expected_raw in test_barcodes]
46-
for dec, (filename, expected_format, expected_raw) in zip(reader.decode(filenames), test_barcodes):
46+
for dec, (filename, expected_format, expected_raw) in zip(reader.decode(filenames, pure_barcode=True), test_barcodes):
4747
if dec.raw != expected_raw:
4848
raise AssertionError('{}: Expected {!r} but got {!r}'.format(filename, expected_raw, dec.parsed))
4949
if dec.format != expected_format:
@@ -79,7 +79,7 @@ def test_possible_formats():
7979
for filename, expected_format, expected_raw in test_barcodes:
8080
path = os.path.join(test_barcode_dir, filename)
8181
incomplete_formats = all_test_formats - {expected_format}
82-
dec = test_reader.decode(path, possible_formats=incomplete_formats)
82+
dec = test_reader.decode(path, possible_formats=incomplete_formats, pure_barcode=True)
8383
if dec is not None:
8484
raise AssertionError('Tried to decode {}-format barcode with possible_formats={!r}; expected failure, but got result in {} format'.format(
8585
expected_format, incomplete_formats, dec.format))

zxing/__init__.py

Lines changed: 3 additions & 1 deletion
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):
34+
def decode(self, filenames, try_harder=False, possible_formats=None, pure_barcode=False):
3535
possible_formats = (possible_formats,) if isinstance(possible_formats, str) else possible_formats
3636

3737
if isinstance(filenames, str):
@@ -44,6 +44,8 @@ def decode(self, filenames, try_harder=False, possible_formats=None):
4444
cmd = [self.java, '-cp', self.classpath, self.cls] + file_uris
4545
if try_harder:
4646
cmd.append('--try_harder')
47+
if pure_barcode:
48+
cmd.append('--pure_barcode')
4749
if possible_formats:
4850
for pf in possible_formats:
4951
cmd += ['--possible_formats', pf ]

0 commit comments

Comments
 (0)