-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathqrCodePrinter.py
80 lines (70 loc) · 2.2 KB
/
qrCodePrinter.py
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
#/usr/bin/local/python
#_*_ coding:utf-8 _*_
import platform
import qrcode
class QRCodePrinter():
def __init__(self,codeStr,fileName):
self.codeStr = codeStr
self.codeArray = []
self.fileName = fileName
# 保存生成二维码的字符串
self.platform = self.__getSystemType()
if "Windows" in self.platform:
self.white = "▇"
self.black = " "
elif "Linux" in self.platform:
self.black = "\033[40m \033[0m"
self.white = "\033[47m \033[0m"
else:
self.black = "\033[40m \033[0m"
self.white = "\033[47m \033[0m"
self.createQR()
def __getSystemType(self):
return platform.platform()+":"+platform.architecture()[0]
def createQR(self):
# save QR code
qrSave =qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=1,
)
qrSave.add_data(self.codeStr)
qrSave.make(fit=True)
imgSave = qrSave.make_image()
imgSave.save(self.fileName)
## print QR code
qrPrite = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=1,
border=1,
)
qrPrite.add_data(self.codeStr)
qrPrite.make(fit=True)
img = qrPrite.make_image()
# img.save('buy.png')
imgL = img.convert("L")
width = imgL.size[0]
height = imgL.size[1]
codeArray = []
for h in range(0, height):
row = []
for w in range(0, width):
pixel = imgL.getpixel((w, h))
row.append(pixel)
codeArray.append(row)
self.codeArray = codeArray
def printQR(self):
for i in self.codeArray:
str = ""
for j in i:
if j == 255:
str+=self.white # 是1表示白色,0表示黑色
else:
str+=self.black
# str += "\n"
print(str)
if __name__=="__main__":
pr = QRCodePrinter("testtesttesttesttesttesttesttesttesttest")
pr.printQR()