2022-02-10 17:24:07 +03:00
|
|
|
import mss
|
|
|
|
import zxingcpp
|
2022-03-22 17:16:52 +03:00
|
|
|
import base64
|
|
|
|
import io
|
2022-02-10 17:24:07 +03:00
|
|
|
from PIL import Image
|
|
|
|
|
|
|
|
|
2022-03-29 14:57:08 +03:00
|
|
|
def scan_qr(image_data = None):
|
|
|
|
if (image_data):
|
|
|
|
msg = base64.b64decode(image_data)
|
2022-03-22 17:16:52 +03:00
|
|
|
buf = io.BytesIO(msg)
|
|
|
|
img = Image.open(buf)
|
|
|
|
else:
|
|
|
|
with mss.mss() as sct:
|
|
|
|
monitor = sct.monitors[0] # 0 is the special "all monitors" value.
|
|
|
|
sct_img = sct.grab(monitor) # mss format
|
|
|
|
img = Image.frombytes("RGB", sct_img.size, sct_img.bgra, "raw", "BGRX")
|
2022-02-10 17:24:07 +03:00
|
|
|
result = zxingcpp.read_barcode(img)
|
|
|
|
if result.valid:
|
|
|
|
return result.text
|
|
|
|
raise ValueError("Unable to read QR code")
|