-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreaderQR.py
More file actions
83 lines (62 loc) · 1.89 KB
/
readerQR.py
File metadata and controls
83 lines (62 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import io
import os
import urllib.request
from typing import Union
import cv2
import numpy as np
from PIL import Image
from pyzxing import BarCodeReader
from complexQrReader import read_qr_complex_pyzxing
from pyzbar.pyzbar import decode
from main import cache_dir
def read_image(path):
with open(path, "rb") as f:
return bytearray(f.read())
def read_qr_by_pyzbar(path) -> (str, bool):
img = cv2.imread(path)
decoded = decode(img)
decoded = [code for code in decoded if code.type == 'QRCODE']
if len(decoded) != 1:
return '', False
text = decoded[0].data.decode("utf-8")
return text, True
def read_qr_by_pyzxing(img: Union[bytearray, str]):
reader = BarCodeReader()
if isinstance(img, bytearray):
np_array = np.array(Image.open(io.BytesIO(img)))
results = reader.decode_array(np_array)
elif isinstance(img, str):
results = reader.decode(img)
else:
return "", False
try:
results = [code for code in results if code['format'].decode("utf-8") == 'QR_CODE']
except:
return "", False
if len(results) == 0:
return '', False
text = results[0]['raw'].decode("utf-8")
return text, True
def download_photo(url, uniq_id) -> str:
img = urllib.request.urlopen(url).read()
path = f"{cache_dir}/{uniq_id}.jpg"
with open(path, "wb") as file:
file.write(img)
return path
def delete_photo(uniq_id):
os.remove(f"{cache_dir}/{uniq_id}.jpg")
def main_qr_reader(url, uniq_id):
path = download_photo(url, uniq_id)
text, got = read_qr_by_pyzbar(path)
if got:
delete_photo(uniq_id)
return text, True
text, got = read_qr_by_pyzxing(path)
if got:
delete_photo(uniq_id)
return text, True
text, got = read_qr_complex_pyzxing(path)
if got:
delete_photo(uniq_id)
return text, True
return '', False