Nikita Dvoretskii
Nikita Dvoretskii

Reputation: 1

Decode data matrix using pylibdmtx or altenative library

I'm trying to use pylibdmtx to decode data matrix code from image and faced with problem of getting raw data string containing full set of symbols.

I'm using this code:

import cv2
from pylibdmtx import pylibdmtx

if __name__ == '__main__':
    image = cv2.imread("image path", cv2.IMREAD_UNCHANGED);
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    ret,thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)

    msg = pylibdmtx.decode(thresh)
    print(msg)

And getting this result: b'0104607009780924215XstxH93zPte'

But data matrix should contain this: (FNC1)0104607009780924215XstxH(FNC1)93zPte

(FNC1) is not printable symbol, and it equal to ASCII<232>.

According libdmtx change log, library should support FNC1. Maybe problem in Python wrapper or some missing initialization steps.

I know about this and this posts, but still have problem.

Test image: dm code sample

Could you please give any advise or recommend other libraries? Thanks in advance.

Upvotes: 0

Views: 1047

Answers (1)

bjallamo
bjallamo

Reputation: 21

import zxing

def decode_datamatrix(image_path):
    # Create a reader
    reader = zxing.BarCodeReader()

    # Read the barcode and store to data variable
    barcode = reader.decode(image_path, "utf-8")
    data=barcode.raw

    # Print out content of the code
    print(data)

    i = 0
    for s in data:
        i+=1
        if s==chr(29):
            print(f"{i}. character in data is a GS/FNC1 character.")


# Call the function with your image
decode_datamatrix("datamatrix.png")

Picture how the output looks like: DataMatrix Data

You can also use repr to print the characters out as "\x1d"

print(repr(data))

Upvotes: 0

Related Questions