comparison mupdf-source/thirdparty/zxing-cpp/wrappers/python/test.py @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 import importlib.util
2 import unittest
3 import math
4 import platform
5
6 import zxingcpp
7
8 has_numpy = importlib.util.find_spec('numpy') is not None
9 has_pil = importlib.util.find_spec('PIL') is not None
10 has_cv2 = importlib.util.find_spec('cv2') is not None
11
12 BF = zxingcpp.BarcodeFormat
13 CT = zxingcpp.ContentType
14
15 class TestFormat(unittest.TestCase):
16
17 def test_format(self):
18 self.assertEqual(zxingcpp.barcode_format_from_str('qrcode'), BF.QRCode)
19 self.assertEqual(zxingcpp.barcode_formats_from_str('ITF, qrcode'), BF.ITF | BF.QRCode)
20
21
22 class TestReadWrite(unittest.TestCase):
23
24 def check_res(self, res, format, text):
25 self.assertTrue(res.valid)
26 self.assertEqual(res.format, format)
27 self.assertEqual(res.text, text)
28 self.assertEqual(res.bytes, bytes(text, 'utf-8'))
29 self.assertEqual(res.orientation, 0)
30 self.assertEqual(res.content_type, CT.Text)
31
32 def test_write_read_cycle(self):
33 format = BF.QRCode
34 text = "I have the best words."
35 img = zxingcpp.write_barcode(format, text)
36
37 res = zxingcpp.read_barcode(img)
38 self.check_res(res, format, text)
39 self.assertEqual(res.symbology_identifier, "]Q1")
40 # self.assertEqual(res.position.top_left.x, 4)
41
42 res = zxingcpp.read_barcode(img, formats=format)
43 self.check_res(res, format, text)
44
45 @unittest.skipIf(not hasattr(zxingcpp, 'create_barcode'), "skipping test for new create_barcode API")
46 def test_create_write_read_cycle(self):
47 format = BF.DataMatrix
48 text = "I have the best words."
49 img = zxingcpp.create_barcode(text, format).to_image()
50
51 res = zxingcpp.read_barcode(img)
52 self.check_res(res, format, text)
53
54 def test_write_read_oned_cycle(self):
55 format = BF.Code128
56 text = "I have the best words."
57 height = 50
58 width = 400
59 img = zxingcpp.write_barcode(format, text, width=width, height=height)
60 # self.assertEqual(img.shape[0], height)
61 # self.assertEqual(img.shape[1], width)
62
63 res = zxingcpp.read_barcode(img)
64 self.check_res(res, format, text)
65 # self.assertEqual(res.position.top_left.x, 61)
66
67 def test_write_read_multi_cycle(self):
68 format = BF.QRCode
69 text = "I have the best words."
70 img = zxingcpp.write_barcode(format, text)
71
72 res = zxingcpp.read_barcodes(img)[0]
73 self.check_res(res, format, text)
74
75 def test_write_read_bytes_cycle(self):
76 format = BF.QRCode
77 text = b"\1\2\3\4"
78 img = zxingcpp.write_barcode(format, text)
79
80 res = zxingcpp.read_barcode(img)
81 self.assertTrue(res.valid)
82 self.assertEqual(res.bytes, text)
83 self.assertEqual(res.content_type, CT.Binary)
84
85 @unittest.skipIf(not hasattr(zxingcpp, 'create_barcode'), "skipping test for new create_barcode API")
86 def test_create_write_read_bytes_cycle(self):
87 format = BF.DataMatrix
88 text = b"\1\2\3\4"
89 img = zxingcpp.create_barcode(text, format).to_image()
90
91 res = zxingcpp.read_barcode(img)
92 self.assertTrue(res.valid)
93 self.assertEqual(res.bytes, text)
94 self.assertEqual(res.content_type, CT.Binary)
95
96 @staticmethod
97 def zeroes(shape):
98 return memoryview(b"0" * math.prod(shape)).cast("B", shape=shape)
99
100 def test_failed_read_buffer(self):
101 res = zxingcpp.read_barcode(
102 self.zeroes((100, 100)), formats=BF.EAN8 | BF.Aztec, binarizer=zxingcpp.Binarizer.BoolCast
103 )
104
105 self.assertEqual(res, None)
106
107 @unittest.skipIf(not has_numpy, "need numpy for read/write tests")
108 def test_failed_read_numpy(self):
109 import numpy as np
110 res = zxingcpp.read_barcode(
111 np.zeros((100, 100), np.uint8), formats=BF.EAN8 | BF.Aztec, binarizer=zxingcpp.Binarizer.BoolCast
112 )
113
114 self.assertEqual(res, None)
115
116 def test_write_read_cycle_buffer(self):
117 format = BF.QRCode
118 text = "I have the best words."
119 img = zxingcpp.write_barcode(format, text)
120
121 self.check_res(zxingcpp.read_barcode(img), format, text)
122
123 @unittest.skipIf(not has_numpy, "need numpy for read/write tests")
124 def test_write_read_cycle_numpy(self):
125 import numpy as np
126 format = BF.QRCode
127 text = "I have the best words."
128 img = zxingcpp.write_barcode(format, text, quiet_zone=10)
129 img = np.array(img)
130
131 self.check_res(zxingcpp.read_barcode(img), format, text)
132 self.check_res(zxingcpp.read_barcode(img[4:40,4:40]), format, text)
133
134 @unittest.skipIf(not has_pil, "need PIL for read/write tests")
135 def test_write_read_cycle_pil(self):
136 from PIL import Image
137 format = BF.QRCode
138 text = "I have the best words."
139 img = zxingcpp.write_barcode(format, text)
140 img = Image.fromarray(img, "L")
141
142 self.check_res(zxingcpp.read_barcode(img), format, text)
143 self.check_res(zxingcpp.read_barcode(img.convert("RGB")), format, text)
144 self.check_res(zxingcpp.read_barcode(img.convert("RGBA")), format, text)
145 self.check_res(zxingcpp.read_barcode(img.convert("1")), format, text)
146 self.check_res(zxingcpp.read_barcode(img.convert("CMYK")), format, text)
147
148 @unittest.skipIf(not has_cv2, "need cv2 for read/write tests")
149 def test_write_read_cycle_cv2(self):
150 import cv2, numpy
151 format = BF.QRCode
152 text = "I have the best words."
153 img = zxingcpp.write_barcode(format, text, quiet_zone=10)
154 img = cv2.cvtColor(numpy.array(img), cv2.COLOR_GRAY2BGR )
155
156 self.check_res(zxingcpp.read_barcode(img), format, text)
157 self.check_res(zxingcpp.read_barcode(img[4:40,4:40,:]), format, text)
158
159 def test_read_invalid_type(self):
160 self.assertRaisesRegex(
161 TypeError, "Invalid input: <class 'str'> does not support the buffer protocol.", zxingcpp.read_barcode, "foo"
162 )
163
164 def test_read_invalid_numpy_array_channels_buffer(self):
165 self.assertRaisesRegex(
166 ValueError, "Unsupported number of channels for buffer: 4", zxingcpp.read_barcode,
167 self.zeroes((100, 100, 4))
168 )
169
170 @unittest.skipIf(not has_numpy, "need numpy for read/write tests")
171 def test_read_invalid_numpy_array_channels_numpy(self):
172 import numpy as np
173 self.assertRaisesRegex(
174 ValueError, "Unsupported number of channels for buffer: 4", zxingcpp.read_barcode,
175 np.zeros((100, 100, 4), np.uint8)
176 )
177
178
179 if __name__ == '__main__':
180 unittest.main()