0% found this document useful (0 votes)
2 views2 pages

Import Cv2 Import Numpy

The document contains a Python script that uses OpenCV and PyZbar to read QR codes from a webcam feed. It captures video frames, overlays a rectangle for scanning, and displays the decoded QR code data on the screen. The program continues running until the user presses the 'q' key to exit.

Uploaded by

szcordeiro3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Import Cv2 Import Numpy

The document contains a Python script that uses OpenCV and PyZbar to read QR codes from a webcam feed. It captures video frames, overlays a rectangle for scanning, and displays the decoded QR code data on the screen. The program continues running until the user presses the 'q' key to exit.

Uploaded by

szcordeiro3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import cv2

import numpy as np
from pyzbar.pyzbar import decode

def read_qr_code():
cap = cv2.VideoCapture(0)
qr_data = ""

while True:
ret, frame = cap.read()
if not ret:
break

white_bg = np.ones_like(frame, dtype=np.uint8) * 255

square_size = 300
center_x, center_y = frame.shape[1] // 2, frame.shape[0] // 2
top_left = (center_x - square_size // 2, center_y - square_size // 2)
bottom_right = (center_x + square_size // 2, center_y + square_size // 2)

cv2.rectangle(white_bg, top_left, bottom_right, (0, 0, 0), 3)

cv2.putText(white_bg, "Escaneie o seu QR Code", (center_x - 180,


top_left[1] - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2)

qr_region = frame[top_left[1]:bottom_right[1], top_left[0]:bottom_right[0]]


white_bg[top_left[1]:bottom_right[1], top_left[0]:bottom_right[0]] =
qr_region

decoded_objects = decode(qr_region)
for obj in decoded_objects:
qr_data = obj.data.decode("utf-8")
print(f"QR Code detectado: {qr_data}")

points = obj.polygon
if len(points) == 4:
pts = [(p.x + top_left[0], p.y + top_left[1]) for p in points]
cv2.polylines(white_bg, [np.array(pts, dtype=np.int32)],
isClosed=True, color=(0, 255, 0), thickness=3)

if qr_data:
cv2.putText(white_bg, f"Mensagem do QR Code: {qr_data}", (50,
frame.shape[0] - 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)

cv2.imshow("Scan de QRCode", white_bg)

if cv2.waitKey(1) & 0xFF == ord('q'):


break

cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
read_qr_code()

You might also like