import cv2
import numpy as np
import pyzbar.pyzbar as pyzbar
cap = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_PLAIN
while True:
_, frame = cap.read()
decodedObjects = pyzbar.decode(frame)
for obj in decodedObjects:
#print("Data", obj.data)
cv2.putText(frame, str(obj.data), (50, 50), font, 2,
(255, 0, 0), 3)
cv2.imshow("Frame", frame)
key = cv2.waitKey(1)
if key == 27:
break
你可以直接停止显示frame。然后用另一个键继续。例如,你可以用另一个键来继续。
import cv2
import pyzbar.pyzbar as zbar
cap = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_SIMPLEX
stopped = False
delay = 1
while(True):
ret = cv2.waitKey(delay) & 0xFF
if ret == ord('c'): # continue
stopped = False
delay = 1
if ret == ord('q'):
break
if stopped or (ret == ord('s')): # stop
stopped = True
delay = 30
continue
# Capture frame-by-frame
ret, frame = cap.read()
decodedObjects = zbar.decode(frame)
if len(decodedObjects) > 0:
stopped = True
y = 50
for obj in decodedObjects:
#print("Data", obj.data)
print(obj.data.decode("utf-8"))
cv2.putText(frame, obj.data.decode("utf-8"), (50, y), font, 2, (255, 0, 0), 3)
y += 50
# Display the resulting frame
cv2.imshow('frame',frame)
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()