Add qr.py
This commit is contained in:
@@ -0,0 +1,83 @@
|
|||||||
|
import pygame
|
||||||
|
import qrcode
|
||||||
|
import random
|
||||||
|
import string
|
||||||
|
import time
|
||||||
|
import cv2
|
||||||
|
from io import BytesIO
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
def gen_id():
|
||||||
|
return ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(8))
|
||||||
|
|
||||||
|
def make_qr(data, size):
|
||||||
|
qr = qrcode.QRCode(box_size=10, border=2)
|
||||||
|
qr.add_data(data)
|
||||||
|
qr.make(fit=True)
|
||||||
|
img = qr.make_image(fill="black", back_color="white").convert("RGB")
|
||||||
|
img = img.resize((size, size))
|
||||||
|
buf = BytesIO()
|
||||||
|
img.save(buf, format="PNG")
|
||||||
|
buf.seek(0)
|
||||||
|
return pygame.image.load(buf)
|
||||||
|
|
||||||
|
pygame.init()
|
||||||
|
|
||||||
|
screen = pygame.display.set_mode((0,0), pygame.FULLSCREEN)
|
||||||
|
w, h = screen.get_size()
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
|
||||||
|
cap = cv2.VideoCapture(0)
|
||||||
|
detector = cv2.QRCodeDetector()
|
||||||
|
|
||||||
|
A_id = gen_id()
|
||||||
|
B_id = gen_id()
|
||||||
|
|
||||||
|
qrA = qrB = None
|
||||||
|
decoded_text = ""
|
||||||
|
last = 0
|
||||||
|
|
||||||
|
qr_size = h // 2
|
||||||
|
|
||||||
|
running = True
|
||||||
|
while running:
|
||||||
|
for e in pygame.event.get():
|
||||||
|
if e.type == pygame.KEYDOWN and e.key == pygame.K_ESCAPE:
|
||||||
|
running = False
|
||||||
|
|
||||||
|
now = time.time()
|
||||||
|
|
||||||
|
if now - last >= 1:
|
||||||
|
last = now
|
||||||
|
qrA = make_qr(f"A:{A_id}|T:{int(now)}", qr_size)
|
||||||
|
qrB = make_qr(f"B:{B_id}|T:{int(now)}", qr_size)
|
||||||
|
|
||||||
|
ret, frame = cap.read()
|
||||||
|
|
||||||
|
if ret:
|
||||||
|
data, points, _ = detector.detectAndDecode(frame)
|
||||||
|
if data:
|
||||||
|
decoded_text = data
|
||||||
|
|
||||||
|
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
||||||
|
frame = cv2.resize(frame, (w//2, h))
|
||||||
|
frame_surface = pygame.surfarray.make_surface(frame.swapaxes(0,1))
|
||||||
|
|
||||||
|
screen.fill((0,0,0))
|
||||||
|
|
||||||
|
if qrA:
|
||||||
|
screen.blit(qrA, (w//4 - qr_size//2, 0))
|
||||||
|
if qrB:
|
||||||
|
screen.blit(qrB, (w//4 - qr_size//2, h//2))
|
||||||
|
|
||||||
|
if ret:
|
||||||
|
screen.blit(frame_surface, (w//2, 0))
|
||||||
|
|
||||||
|
font = pygame.font.SysFont(None, 36)
|
||||||
|
screen.blit(font.render(decoded_text, True, (0,255,0)), (w//2 + 20, 20))
|
||||||
|
|
||||||
|
pygame.display.flip()
|
||||||
|
clock.tick(30)
|
||||||
|
|
||||||
|
cap.release()
|
||||||
|
pygame.quit()
|
||||||
Reference in New Issue
Block a user