代码仅供学习
import cv2
import pyzbar.pyzbar as pyzbar
import numpy as np
from PIL import ImageGrab
import time
import tkinter as tk
import re
import http.client
import json
import threading
root = tk.Tk()
win_width = 540
win_height = 540
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x_pos = (screen_width // 2) - (win_width // 2)
y_pos = (screen_height // 2) - (win_height // 2)
left, top, width, height = x_pos, y_pos, win_width, win_height
right = left + width
bottom = top + height
cv2.namedWindow("QR Code Scanner", cv2.WINDOW_NORMAL)
cv2.resizeWindow("QR Code Scanner", win_width, win_height)
def Request(ticket):
conn = http.client.HTTPSConnection("api-sdk.mihoyo.com")
payload = json.dumps({
"app_id": 4,
"device": "",
"ticket": ticket
})
headers = {}
conn.request("POST", "/hk4e_cn/combo/panda/qrcode/scan", payload, headers)
res = conn.getresponse()
data = res.read()
data = json.loads(data.decode("utf-8"))
retcode = data["retcode"]
return retcode
def process_code(code_data):
match = re.search(r"ticket=([a-f0-9]+)", code_data)
if match:
start_time = time.time()
retcode = Request(match.group(1))
end_time = time.time()
if retcode == 0:
elapsed_time = end_time - start_time
print("抢码成功耗时 %.3f 秒" % elapsed_time)
def scan_codes(screenshot):
codes = pyzbar.decode(screenshot)
for code in codes:
code_data = code.data.decode('utf-8')
print(code_data)
thread = threading.Thread(target=process_code, args=(code_data,))
thread.start()
while True:
screenshot = cv2.cvtColor(
np.array(ImageGrab.grab(bbox=(left, top, right, bottom))),
cv2.COLOR_BGR2RGB
)
gray = cv2.cvtColor(screenshot, cv2.COLOR_RGB2GRAY)
scan_codes(screenshot)
cv2.imshow("QR Code Scanner", screenshot)
if cv2.waitKey(1) != -1:
break
cv2.destroyAllWindows()