Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read data matrix code in Python?

I wanted to know how I can read data matrix bar code using openCV in python

I've found How to locate and read Data Matrix code with python but this is still unclear for me. I would like to show the result of reading bar code to output.

like image 352
trieser Avatar asked Sep 11 '25 20:09

trieser


2 Answers

Ok after few days I've found resolution:

import cv2
from pylibdmtx.pylibdmtx import decode
import ctypes  

def Mbox(title, text, style):
    return ctypes.windll.user32.MessageBoxW(0, text, title, style)

# Read file using OpenCV
Mbox('Data Matrix', str(decode(cv2.imread('C:/Users/User/Desktop/Zdjecia_QrCode/again2.png'))), 1)

But I would like to know how I can improve reading because I cannot read every datamatrix. Do you have any idea?

like image 89
trieser Avatar answered Sep 13 '25 10:09

trieser


import numpy as np
import cv2
from pylibdmtx import pylibdmtx

if __name__ == '__main__':

    image = cv2.imread('datamatrix_sample1_130x116.png', 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)
like image 20
Anand Raj Avatar answered Sep 13 '25 09:09

Anand Raj