Calling barcode font encoder DLL from Python

Introduction

Morovia barcode font products include encoder DLL that can be called from various languages, including Python. This article explains how to. The DLL support in Python is called ctypes. It is strongly recommended that you get familiar with ctypes first.

To call the encoder function, you first call windll.LoadLibrary to load the DLL. Then declare the prototypes for each functions that you are going to call.

The code in this article is written for Python 3, 64-bit. Accordingly the DLL called is also 64-bit. If you are using 32-bit Python, you need to change the DLL path to point to the 32-bit DLL.

It is important to define the correct ctypes data type corresponding to the C types. The following table lists the type mappings that are used in our API.

ctypes typeC type
c_intint
c_char_pchar*
c_wchar_pwchar*
c_void_pvoid*

Therefore a C function

void *__stdcall    QRCodeEncode2W(
      const wchar_t * dataToEncode,
      int versionRequested,
      int ecLevel
  );

is declared as:

encoder_qr.QRCodeEncode2W.restype = c_void_p
encoder_qr.QRCodeEncode2W.argtypes = [c_wchar_p, c_int, c_int]

Calling DataMatrix Encoder DLL

The documentation for DataMatrix Encoder DLL API is available at https://www.morovia.com/manuals/datamatrix-font-encoder5/ch04.php. First call one of the Encode functions to obtain the result object. From the result object you can retrieve the barcode string, which becomes the data matrix barcode after being formatted with "MRV DataMatrix" font. Or you can save it as an image file in a several formats, including PNG or SVG format.

from ctypes import *

encoder_dm = windll.LoadLibrary("c:/windows/system32/MoroviaDataMatrixFontEncoder5.dll")

encoder_dm.DataMatrixEncode2.restype = c_void_p
encoder_dm.DataMatrixEncode2.argtypes = [c_char_p, c_int]

encoder_dm.DataMatrixResultGetBarcodeString2.restype = c_char_p
encoder_dm.DataMatrixResultGetBarcodeString2.argtypes = [c_void_p, c_char_p]

encoder_dm.PaintDataMatrixImageRaster.restype = c_int
encoder_dm.PaintDataMatrixImageRaster.argtypes = [c_void_p, c_wchar_p, c_int, c_int, c_int, c_int]

encoder_dm.DestroyDataMatrixEncodeResult.argtypes = [c_void_p]

result = encoder_dm.DataMatrixEncode2(b"data to be encoded", 0)

s_bytes = encoder_dm.DataMatrixResultGetBarcodeString2(result, b'\n')
s = s_bytes.decode("utf-8")

print("BarcodeString (datamatrix): \n" + s)

encoder_dm.PaintDataMatrixImageRaster(result, 
    "c:/windows/temp/datamatrix.png", # image file path
    10,  # pixels per module
    0xffffff, # foreground color, rrggbb
    0,  # background color
    0)  # 0 - PNG format

encoder_dm.DestroyDataMatrixEncodeResult(result) 
    
    

Program printout:

BarcodeString (datamatrix):
F2D494C293D792C495
F76C328D526B94CC15
F25D7220E4C0727C45
F46230A09116F21805
C44CC44C444C444C44

The script also produces the image rendering datamatrix.png under c:/window/temp directory.

Calling QRCode Encoder DLL

The documentation for QRCode Encoder DLL API is available at https://www.morovia.com/manuals/qrcode-font-encoder/ch08.php. First call one of the Encode functions to obtain the result object. From the result object you can retrieve the barcode string, which becomes the data matrix barcode after being formatted with "MRV QRCode" font. Or you can save it as an image file in a several formats, including PNG or SVG format.

encoder_qr = windll.LoadLibrary("c:/windows/system32/MoroviaQRCodeFontEncoder5.dll")
encoder_qr.QRCodeEncode2.restype = c_void_p
encoder_qr.QRCodeEncode2.argtypes = [c_char_p, c_int, c_int]
encoder_qr.QRCodeResultGetBarcodeString2.restype = c_char_p
encoder_qr.QRCodeResultGetBarcodeString2.argtypes = [c_void_p, c_char_p]

encoder_qr.PaintQRCodeImageRaster.restype = c_int
encoder_qr.PaintQRCodeImageRaster.argtypes = [c_void_p, c_wchar_p, c_int, c_int, c_int, c_int]

encoder_qr.DestroyQRCodeEncodeResult.argtypes = [c_void_p]

result = encoder_qr.QRCodeEncode2(b"data to be encoded", 0, 0)

s_bytes = encoder_qr.QRCodeResultGetBarcodeString2(result, b'\n')
s = s_bytes.decode("utf-8")

print("BarcodeString (QR code): \n" + s)

encoder_qr.PaintQRCodeImageRaster(result, 
    "c:/windows/temp/qrcode.png", # image file path
    10,  # pixels per module
    0xffffff, # foreground color, rrggbb
    0,  # background color
    0)  # 0 - PNG format

encoder_qr.DestroyQRCodeEncodeResult(result)
    

Program printout:

BarcodeString (QR code):
F8BBB8F065C011CD00F8BBB8F
E2AAA2E0F8F1E1ADA0E2AAA2E
A86825AE351FF7A0F7EB908B6
25D06FAEA48F06E887E50765E
BAA2AAB8E1F2FC76F8A8F4BBF
F0EEE0F05C640057F98EE808A
8888888088808008888800088

The script also produces the image rendering qrcode.png under c:/window/temp directory.

Calling Linear Encoder DLL

The linear barcode font encoder DLL is included in a shared installer packaged called Font Tools. The function prototypes are documented in KB10011. Additional information on Code128Ex and EAN128Ex is available at KB10048. The linear encoder functions take a single string parameter and returns the barcode string (the bytes object in Python). The barcode string returned should be interpreted as ISO8859-1 encoding.

encoder = windll.LoadLibrary("C:/Program Files/Common Files/Morovia/MoroviaFontTools/mrvFontTools_x64.dll")
encoder.Code128Ex.restype = c_char_p
encoder.Code128Ex.argtypes = [c_char_p]
encoder.EAN128Ex.restype = c_char_p
encoder.EAN128Ex.argtypes = [c_char_p]
s_data_encode = b"1234ABCDEQ"

print("Code128 barcode string for data %s is %s\n" % (s_data_encode,
    encoder.Code128Ex(s_data_encode).decode("iso8859-1")))

s_data_encode = b"(00)123456789012345678(01)12345678901234"
print("GS1-128 barcode string for data %s is %s\n" % (s_data_encode, encoder.EAN128Ex(s_data_encode).decode("iso8859-1")))
    

Program printout:

Code128 barcode string for data b'1234ABCDEQ' is Ê,BÅABCDEQ=ËÍ
GS1-128 barcode string for data b'(00)123456789012345678(01)12345678901234' is ÊÇÌ,BXnz,BXnÇ!,BXnz,BMËÍ