diff tests/test_barcode.py @ 1:1d09e1dec1d9 upstream

ADD: PyMuPDF v1.26.4: the original sdist. It does not yet contain MuPDF. This normally will be downloaded when building PyMuPDF.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:37:51 +0200
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test_barcode.py	Mon Sep 15 11:37:51 2025 +0200
@@ -0,0 +1,63 @@
+import os
+
+import pymupdf
+
+
+def test_barcode():
+    if pymupdf.mupdf_version_tuple < (1, 26):
+        print(f'Not testing barcode because {pymupdf.mupdf_version=} < 1.26')
+        return
+    path = os.path.normpath(f'{__file__}/../../tests/test_barcode_out.pdf')
+    
+    url = 'http://artifex.com'
+    text_in = '012345678901'
+    text_out = '123456789012'
+    # Create empty document and add a qrcode image.
+    with pymupdf.Document() as document:
+        page = document.new_page()
+        
+        pixmap = pymupdf.mupdf.fz_new_barcode_pixmap(
+                pymupdf.mupdf.FZ_BARCODE_QRCODE,
+                url,
+                512,
+                4,  # ec_level
+                0,  # quiet
+                1,  # hrt
+                )
+        pixmap = pymupdf.Pixmap('raw', pixmap)
+        page.insert_image(
+                (0, 0, 100, 100),
+                pixmap=pixmap,
+                )
+        pixmap = pymupdf.mupdf.fz_new_barcode_pixmap(
+                pymupdf.mupdf.FZ_BARCODE_EAN13,
+                text_in,
+                512,
+                4,  # ec_level
+                0,  # quiet
+                1,  # hrt
+                )
+        pixmap = pymupdf.Pixmap('raw', pixmap)
+        page.insert_image(
+                (0, 200, 100, 300),
+                pixmap=pixmap,
+                )
+        
+        document.save(path)
+    
+    with pymupdf.open(path) as document:
+        page = document[0]
+        for i, ii in enumerate(page.get_images()):
+            xref = ii[0]
+            pixmap = pymupdf.Pixmap(document, xref)
+            hrt, barcode_type = pymupdf.mupdf.fz_decode_barcode_from_pixmap2(
+                    pixmap.this,
+                    0,  # rotate.
+                    )
+            print(f'{hrt=}')
+            if i == 0:
+                assert hrt == url
+            elif i == 1:
+                assert hrt == text_out
+            else:
+                assert 0