You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
958 B

6 years ago
  1. import cv2
  2. from pyzbar import pyzbar
  3. class Read():
  4. instance = 0
  5. sid = 0
  6. def __init__(self,inst):
  7. self.instance = inst
  8. def detect(self):
  9. cam = cv2.VideoCapture(0)
  10. barcodeData_prev = 0
  11. while 1:
  12. ret , img = cam.read()
  13. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  14. barcode = pyzbar.decode(gray)
  15. if len(barcode) > 0:
  16. barcode = barcode[0]
  17. (x, y, w, h) = barcode.rect
  18. barcodeData = barcode.data.decode("utf-8")
  19. barcodeType = barcode.type
  20. if barcodeData_prev == 0 or barcodeData_prev != barcodeData:
  21. barcodeData_prev = barcodeData
  22. self.instance.received(barcodeData)
  23. continue
  24. text = "{} ({})".format(barcodeData, barcodeType)
  25. cv2.putText(img, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX,
  26. 0.5, (255, 0, 0), 2)
  27. cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
  28. cv2.imshow("a", img)
  29. if cv2.waitKey(1) & 0xFF == ord('q'):
  30. break
  31. cam.release()
  32. cv2.destroyAllWindows()