Avatar
0
hurmcuuxqeu47e6u Beginner
Đọc mã QR trên CCCD
import cv2
import numpy as np
from pyzbar.pyzbar import decode
import tkinter.messagebox as msgbox
import tkinter as tk
cap = cv2.VideoCapture(0)
root = tk.Tk()
root.title("QR_CODE")
decoded_label_widget = tk.Label(root, text="")
decoded_label_widget.grid(row=0, column=0)
decoded_entry_widget = tk.Entry(root, state="readonly", font=("Arial", 12), width=60, highlightthickness=0, bd=0)
decoded_entry_widget.grid(row=0, column=1, pady=10)
flag_camera = True
def toggle_display():
global flag_camera
flag_camera = not flag_camera
toggle_button = tk.Button(root, text="Bật-Tắt-Cam", command=toggle_display)
toggle_button.grid(row=0, column=2, padx=10)
def copy_all():
root.clipboard_clear()
root.clipboard_append(decoded_entry_widget.get())
copy_button = tk.Button(root, text="Sao Chép", command=copy_all)
copy_button.grid(row=0, column=3, padx=10)
if not cap.isOpened():
msgbox.showwarning("Cảnh báo", "Không thể mở camera. Vui lòng kiểm tra lại!")
flag_camera = False
while True:
if flag_camera:
_, frame = cap.read()
frame = cv2.resize(frame, None, fx=1.3, fy=1.3, interpolation=cv2.INTER_LINEAR)
frame = cv2.convertScaleAbs(frame, alpha=3, beta=20)
decodedObjects = decode(frame)
decoded_data = [obj.data.decode("utf-8") for obj in decodedObjects]
for obj in decodedObjects:
x, y, w, h = obj.rect.left, obj.rect.top, obj.rect.width, obj.rect.height
cx, cy = x + w/2, y + h/2
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.line(frame, (int(cx), y), (int(cx), y + h), (0, 255, 0), 2)
cv2.line(frame, (x, int(cy)), (x + w, int(cy)), (0, 255, 0), 2)
if len(decoded_data) > 0:
decoded_text = "n".join(decoded_data)
decoded_entry_widget.delete(0, tk.END)
decoded_entry_widget.insert(tk.END, decoded_text)
decoded_entry_widget.configure(state="normal")
decoded_label_widget.configure(text="")
else:
decoded_label_widget.configure(text="")
cv2.imshow("QR Code Scanner", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if cv2.getWindowProperty("QR Code Scanner", cv2.WND_PROP_VISIBLE) < 1:
break
else:
decoded_entry_widget.configure(state="normal")
decoded_label_widget.configure(text="")
cv2.destroyAllWindows()
root.update()
root.update()
cap.release()
cv2.destroyAllWindows()
Mình có đoạn code scan mã qr trên CCCD nhưng hiện tại đọc nó rất lâu tầm, mình cũng đã kết nối vơi cam iphone bằng ivcam cũng vậy, Nhờ mọi người xem giúp chỉnh ở đâu ạ
Mình cảm ơn
  • Answer
Remain: 5
2 Answers
Avatar
tvd12 Enlightened
tvd12 Enlightened
The Best Answer
Em chưa hiểu lắm đây là code cho mobile hay là code cho desktop hay cho thiết bị gì anh nhỉ?
  • 0
  • Reply
Đây là code cho desktop ạ. tại không import nên phải copy như vậy ạ
 –  hurmcuuxqeu47e6u 1684508007000
Avatar
tvd12 Enlightened
tvd12 Enlightened
Anh thử sử dụng code này xem sao, em thử trên máy em thì thấy rất nhanh:
import cv2
from pyzbar.pyzbar import decode

# Initialize the video capture object
cap = cv2.VideoCapture(0)

if not cap.isOpened():
    print("Failed to open the camera.")
    exit()

while True:
    # Read a frame from the camera
    ret, frame = cap.read()

    if not ret:
        print("Failed to capture a frame.")
        break

    # Decode QR codes in the frame
    qr_codes = decode(frame)

    # Process each detected QR code
    for qr_code in qr_codes:
        # Extract the decoded data
        data = qr_code.data.decode('utf-8')

        # Draw a bounding box around the QR code
        x, y, w, h = qr_code.rect
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

        # Display the decoded data
        cv2.putText(frame, data, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
        
        # Print the decoded data
        print("QR Code:", data)

        # Release the video capture object and close the windows
        cap.release()
        cv2.destroyAllWindows()
        exit()

    # Display the frame
    cv2.imshow('QR Code Reader', frame)

    # Check for key press to exit
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release the video capture object and close the windows
cap.release()
cv2.destroyAllWindows()
  • 0
  • Reply