-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQRcreator.py
More file actions
274 lines (211 loc) · 9.27 KB
/
Copy pathQRcreator.py
File metadata and controls
274 lines (211 loc) · 9.27 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import re
from dictsToUse import someDicts
# Version: 1.1
# Licensing: GPLv3
# For any functions beside createQR and printQR, use with care.
class LengthError(Exception):
def __init__(self, message: str) -> None:
self.message = message
super().__init__(self.message)
class MaskError(Exception):
def __init__(self, message: str) -> None:
self.message = message
super().__init__(self.message)
def buildQR(
qr: list, start: tuple, end: tuple, message: str, startbit: int, mask: int
) -> list:
bitcount = startbit
for i in range(
start[0],
end[0] + ((1 * (start[0] < end[0])) + (-1) * (start[0] > end[0])),
(1 * (start[0] < end[0])) + (-1) * (start[0] > end[0]),
):
for j in range(start[1], end[1] - 1, -1):
# print(f"({i}, {j}) =", end="")
if mask == 0:
if ((i + j) % 2) == 0:
qr[i][j] = ApplyMask(int(message[bitcount]))
else:
qr[i][j] = int(message[bitcount])
# print(f"{qr[i][j]}")
# print(f"bitcount: {bitcount}")
bitcount += 1
else:
raise MaskError("Invalid mask.")
return qr
def printQR(qr: list) -> None:
"""Prints the QR code from a binary string."""
print(
"⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜\n⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜\n⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜\n⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜\n⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜"
)
for i in range(21):
print("⬜⬜⬜⬜⬜", end="")
for j in range(21):
if qr[i][j] == 1:
print("⬛", end="")
else:
print("⬜", end="")
print("⬜⬜⬜⬜⬜\n", end="")
print(
"⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜\n⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜\n⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜\n⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜\n⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜"
)
def ApplyMask(bit: int) -> int:
"""Apply Mask."""
if bit == 0:
return 1
else:
return 0
def createErrorCorrectionH(message: str) -> list:
"""Create the Error Correction level H for a message binary string."""
msgPoly: dict = {}
genPoly: dict = {}
count: int = 0
genExpo: list = [
0,
43,
139,
206,
78,
43,
239,
123,
206,
214,
147,
24,
99,
150,
39,
243,
163,
136,
]
pat = r"(........)"
founds = re.finditer(pat, message)
for idx, found in enumerate(founds):
msgPoly[25 - idx] = tuple(["dec", int(found.group(0), 2)])
neededCount: int = len(msgPoly)
for idx in range(16, -1, -1):
msgPoly[idx] = tuple(["dec", 0])
for idx, val in enumerate(genExpo):
genPoly[17 - idx] = tuple(["alpha", val])
while count < neededCount:
newPoly: dict = {}
modGenPoly: dict = {}
for key in genPoly.keys():
modGenPoly[key + 8 - count] = genPoly[key]
# print("modGenPoly count {count}".format(count=count), modGenPoly, "\n\n")
maxKey: int = max([key for key in msgPoly.keys()])
alphaExponent: int = someDicts.decToAlphaTable[msgPoly[maxKey][1]]
for key in modGenPoly.keys():
newPoly[key] = tuple(
[
"dec",
someDicts.alphaToDecTable[
(modGenPoly[key][1] + alphaExponent) % 255
],
]
)
# print("newPoly count {count}".format(count=count), newPoly, "\n")
# print("msgPoly count {count}".format(count=count), msgPoly, "\n\n")
for key in newPoly.keys():
newPoly[key] = tuple(["dec", newPoly[key][1] ^ msgPoly[key][1]])
# print("newPoly after XOR count {count}".format(count=count), newPoly, "\n\n")
maxKey: int = max([key for key in newPoly.keys()])
del newPoly[maxKey]
count += 1
while True:
maxKey: int = max([key for key in newPoly.keys()])
if newPoly[maxKey][1] == 0:
del newPoly[maxKey]
count += 1
else:
break
msgPoly = newPoly.copy()
minKey: int = min([key for key in msgPoly.keys()])
for idx in range(minKey - 1, -1, -1):
msgPoly[idx] = tuple(["dec", 0])
while max([key for key in msgPoly.keys()]) < 17:
maxKey: int = max([key for key in msgPoly.keys()])
msgPoly[maxKey + 1] = tuple(["dec", 0])
sortedMsgPoly = sorted(msgPoly.items(), key=lambda x: x[0], reverse=True)
return [i[1][1] for i in sortedMsgPoly][1:]
def createQR_part1(message: str) -> str:
"""Creates a QR binary string from a message of no longer than 7 charcters."""
messageString: str = "0100"
messageLength: int = len(message)
if len(message) > 7:
raise LengthError("The length of the message is too long. (more than 7 chars)")
mlb: str = bin(messageLength)[2:].zfill(8)
messageString += mlb
for char in message:
charBin: str = bin(ord(char))[2:].zfill(8)
messageString += charBin
mSLength: int = len(messageString)
if mSLength == 72:
pass
elif mSLength == 71:
messageString += "0"
elif mSLength == 70:
messageString += "00"
elif mSLength == 69:
messageString += "000"
else:
messageString += "0000"
mSLength = len(messageString)
while mSLength < 72:
messageString += "11101100"
mSLength = len(messageString)
if mSLength < 72:
messageString += "00010001"
mSLength = len(messageString)
return messageString
def createQR_part2(messageString: str, maskNum: int) -> list:
errorCorrection: list = createErrorCorrectionH(messageString)
for num in errorCorrection:
messageString += bin(num)[2:].zfill(8)
m: str = messageString
qr: list = [
# 0v 1v 2v 3v 4v 5v 6v 7v 8v 9d Ad Bd Cd Dv Ev Fv Gv Hv Iv Jv Kv
[1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1], # 0 block
[1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1], # 1 block
[1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1], # 2 block
[1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1], # 3 block
[1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1], # 4 block
[1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1], # 5 block
[1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1], # 6 timing
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # 7 block
[0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1], # 8 block
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # 9 hi data
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # A hi data
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # B hi data
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # C hi data
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # D block
[1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # E block
[1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # F block
[1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # G block
[1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # H block
[1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # I block
[1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # J block
[1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # K block
]
qr = buildQR(qr, (20, 20), (9, 19), m, 0, maskNum)
qr = buildQR(qr, (9, 18), (20, 17), m, 24, maskNum)
qr = buildQR(qr, (20, 16), (9, 15), m, 48, maskNum)
qr = buildQR(qr, (9, 14), (20, 13), m, 72, maskNum)
qr = buildQR(qr, (20, 12), (7, 11), m, 96, maskNum)
qr = buildQR(qr, (5, 12), (0, 11), m, 124, maskNum)
qr = buildQR(qr, (0, 10), (5, 9), m, 136, maskNum)
qr = buildQR(qr, (7, 10), (20, 9), m, 148, maskNum)
qr = buildQR(qr, (12, 8), (9, 7), m, 176, maskNum)
qr = buildQR(qr, (9, 5), (12, 4), m, 184, maskNum)
qr = buildQR(qr, (12, 3), (9, 2), m, 192, maskNum)
qr = buildQR(qr, (9, 1), (12, 0), m, 200, maskNum)
return qr
def createQR(message: str, maskNum: int) -> list:
msg = createQR_part1(message)
qr = createQR_part2(msg, maskNum)
return qr
if __name__ == "__main__":
print("You have to use this in as import package.")
print(printQR(createQR("Hello", 0)))