-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathqr_code_pixels_source.py
69 lines (59 loc) · 2.13 KB
/
qr_code_pixels_source.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
'''
Defines a class representing a black&white image of a QR-Code.
'''
# SPDX-FileCopyrightText: 2021 Robin Vobruba <hoijui.quaero@gmail.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later
from pixels_source import PixelsSource
# see https://github.com/kazuhikoarase/qrcode-generator/blob/master/python/qrcode.py
#import kicad_qrcode as qrcode # TODO: local qrcode package is prefered, so we renamed it
import qrcode
class QrCodePixelsSource(PixelsSource):
'''
Allows to use a string of data as sources for black&white pixels,
encoded as a QR-Code.
'''
def __init__(self, content, border=1):
self.content = content
self.border = border
# Build QR-Code
self.qrc = qrcode.QRCode()
#self.qrc.setTypeNumber(4)
# ErrorCorrectLevel: L = 7%, M = 15% Q = 25% H = 30%
#self.qrc.setErrorCorrectLevel(qrcode.ErrorCorrectLevel.M)
self.qrc.setErrorCorrectLevel(qrcode.ErrorCorrectLevel.L)
self.qrc.addData(str(content))
self.qrc.make()
self.len = self.qrc.modules.__len__() + (self.border * 2)
def __str__(self):
return f"QR-Code-PixelsSource[data: '{self.content}']"
def getSize(self):
return (self.len, self.len)
def getData(self):
if self.border >= 0:
# Adding border: Create a new array larger than the self.qrc.modules
array2d = [ [ 0 for a in range(self.len) ] for b in range(self.len) ]
line_position = self.border
for i in self.qrc.modules:
column_position = self.border
for j in i:
array2d[line_position][column_position] = j
column_position += 1
line_position += 1
else:
# No border: using array as is
array2d = self.qrc.modules
data = []
# convert 2D to 1D array
for line in array2d:
data.extend(line)
return list(data)
def testing():
'''
Testing - output to stdout.
'''
data = "My Data"
pixels = QrCodePixelsSource(data, 1)
pixels.debug_to_stdout()
if __name__ == "__main__":
testing()