From ec9a51c8f33b305abe764a459aaba6899a825a24 Mon Sep 17 00:00:00 2001 From: Konstantin Kalentev Date: Wed, 5 May 2021 15:22:57 +0200 Subject: [PATCH] Add support of multiple barcodes per file --- test/barcodes/multi.png | Bin 0 -> 1509 bytes test/test_all.py | 19 +++++++++++++++++-- zxing/__init__.py | 13 ++++++++++--- 3 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 test/barcodes/multi.png diff --git a/test/barcodes/multi.png b/test/barcodes/multi.png new file mode 100644 index 0000000000000000000000000000000000000000..8af649098b1b21a90f16b4c67473c7e0d0bad789 GIT binary patch literal 1509 zcmb7EeNd8j9RHako8{`Rc@-|{wmR>|OxL2iE2O4^T$eLf_=2dtKq{az@g;nXHeJ~x zOSk@@ur6+&h%69?C>lmlbXqfmjHx+Lp{T$p5K+9 z&Q9A!AfAXhznAI*0D?%2ICiSQU^A(!Z-#C{JgKW$UZy&KRPCqWZv+Qa+?vt8dbFk5 zzOH)Ee5lI24aB^Vh6BL$2k)W51`;^hvaT!@0S0_Mi!DuHSG*S-de=UDWcyT7>vg5M zb@s$gF%MESR`O?BE|k?lLqc(suTAcGFnaZEw$b673PHX2 zI!%dczSPU>EA-m|;{Hu)s{P69wv$j?5r3{Ba(_^NgC`e zn`|&of>%CgKxT^%E5fBQ{%qtdlKW6X`tDNnoBsQ2yW-LFCM%zwS1N*3HG`^3OqPGY z|BAlm^JLuT<#_q!6dJDWbGfO$uv2YFLzr@Xu7Dd0BPsr#Fpqq!ZQ~y z*PhrT#Ej_aZjuDEssG3uVmu0!N+V}tC&RfZw1cO9&v=rCNxZD;MS-v*@s}_`)C)lg zll0-L+G|vKtk8z1^fc3#yLu({^hTvY9M&f&GY9Ju;*^ zCzwkUYu#5$P^*d@HIDn12OOSw&9L2oWZ#Ko;bi$qA?91MBZNcAYkpxxKGd^R$tb48 zS-92xls^!Fm>55HTXAD*+%D>yZC85VxlJBe9jJa=a6@aAHdI8vqlbA4HZukf%9n*k zpV0@G{k!p9H}f^bGxRuyhIp19#~gMS+Gpw?A2RU9b28kEcZVAC3q>L2VS+b!sb3w> zL`cL7r?G7fxr=IJf@UVeEBm3ZXKLql=J@Q9X$yHkk4 z6)s>-C_1wRUK>yqm>1Wr<-XZ-J!Chzcvz> zUTX##m(k?ch8Y^4(@zt02S_=H)jHq-jiFT{CoE$Exb1c`FKm3#vKF+gZ*N1ZxHc|9 zp|BwT&Qxw(V+8jCD~La=v5o&_QtvP^IOMS=SVAyE@7_OpA0;MLUb*jItn;`nF)}(r I{ADWlAB07xlK=n! literal 0 HcmV?d00001 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..9bb9df5 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, 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