python米游社卡码工具

代码仅供学习

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()

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值