This repository has been archived by the owner on Nov 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathqrencode.pyx
80 lines (63 loc) · 2.04 KB
/
qrencode.pyx
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
from ImageOps import expand
from Image import fromstring
cdef extern from "qrencode.h":
int QR_ECLEVEL_L
int QR_ECLEVEL_M
int QR_ECLEVEL_Q
int QR_ECLEVEL_H
int QR_MODE_NUM
int QR_MODE_AN
int QR_MODE_8
int QR_MODE_KANJI
ctypedef struct QRcode:
int version
int width
unsigned char *data
QRcode *QRcode_encodeString(char *string, int version, int level, int hint, int casesensitive)
cdef class Encoder:
default_options = {
'mode' : QR_MODE_8,
'ec_level': QR_ECLEVEL_L,
'width': 400,
'border': 10,
'version': 5,
'case_sensitive': True
}
def __cinit__(self):
pass
def __dealloc__(self):
pass
def encode(self, char *text, options={}):
cdef QRcode *_c_code
cdef unsigned char *data
opt = self.default_options
opt.update(options)
border = opt.get('border')
w = opt.get('width') - (border * 2)
v = opt.get('version')
mode = opt.get('mode')
ec_level = opt.get('ec_level')
case_sensitive = opt.get('case_sensitive')
# encode the test as a QR code
str_copy = text
str_copy = str_copy + '\0'
_c_code = QRcode_encodeString(str_copy, int(v), int(ec_level), int(mode), int(case_sensitive))
version = _c_code.version
width = _c_code.width
data = _c_code.data
rawdata = ''
dotsize = w / width
realwidth = width * dotsize
# build raw image data
for y in range(width):
line = ''
for x in range(width):
if data[y * width + x] % 2:
line += dotsize * chr(0)
else:
line += dotsize * chr(255)
lines = dotsize * line
rawdata += lines
# create PIL image w/ border
image = expand(fromstring('L', (realwidth, realwidth), rawdata), border, 255)
return image