datamatrix.py 742 B

123456789101112131415161718192021222324252627
  1. import cv2
  2. from pylibdmtx.pylibdmtx import decode
  3. # TODO[SG]: add real datamatrix code. This is just a placeholder.
  4. class DataMatrixReader:
  5. def __init__(self, camera_id: int = 0):
  6. self.camera_id = camera_id
  7. self.camera = None
  8. def initialize(self):
  9. self.camera = cv2.VideoCapture(self.camera_id)
  10. def read_datamatrix(self) -> str:
  11. ret, frame = self.camera.read()
  12. if not ret:
  13. raise Exception("Failed to capture image")
  14. decoded = decode(frame)
  15. if not decoded:
  16. raise Exception("No data matrix found")
  17. return decoded[0].data.decode('utf-8')
  18. def cleanup(self):
  19. if self.camera:
  20. self.camera.release()