Skip to content

Commit 97d33df

Browse files
tests/test_barcode.py: new, for testing barcode support.
1 parent 152962e commit 97d33df

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

tests/test_barcode.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import os
2+
3+
import pymupdf
4+
5+
6+
def test_barcode():
7+
if pymupdf.mupdf_version_tuple < (1, 26):
8+
print(f'Not testing barcode because {pymupdf.mupdf_version=} < 1.26')
9+
return
10+
path = os.path.normpath(f'{__file__}/../../tests/test_barcode_out.pdf')
11+
12+
url = 'http://artifex.com'
13+
text_in = '012345678901'
14+
text_out = '123456789012'
15+
# Create empty document and add a qrcode image.
16+
with pymupdf.Document() as document:
17+
page = document.new_page()
18+
19+
pixmap = pymupdf.mupdf.fz_new_barcode_pixmap(
20+
pymupdf.mupdf.FZ_BARCODE_QRCODE,
21+
url,
22+
512,
23+
4, # ec_level
24+
0, # quiet
25+
1, # hrt
26+
)
27+
pixmap = pymupdf.Pixmap('raw', pixmap)
28+
page.insert_image(
29+
(0, 0, 100, 100),
30+
pixmap=pixmap,
31+
)
32+
pixmap = pymupdf.mupdf.fz_new_barcode_pixmap(
33+
pymupdf.mupdf.FZ_BARCODE_EAN13,
34+
text_in,
35+
512,
36+
4, # ec_level
37+
0, # quiet
38+
1, # hrt
39+
)
40+
pixmap = pymupdf.Pixmap('raw', pixmap)
41+
page.insert_image(
42+
(0, 200, 100, 300),
43+
pixmap=pixmap,
44+
)
45+
46+
document.save(path)
47+
48+
with pymupdf.open(path) as document:
49+
page = document[0]
50+
for i, ii in enumerate(page.get_images()):
51+
xref = ii[0]
52+
pixmap = pymupdf.Pixmap(document, xref)
53+
hrt, barcode_type = pymupdf.mupdf.fz_decode_barcode_from_pixmap2(
54+
pixmap.this,
55+
0, # rotate.
56+
)
57+
print(f'{hrt=}')
58+
if i == 0:
59+
assert hrt == url
60+
elif i == 1:
61+
assert hrt == text_out
62+
else:
63+
assert 0

0 commit comments

Comments
 (0)