This repository has been archived by the owner on Jan 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathqrcode_generate_Data.py
executable file
·99 lines (84 loc) · 3.56 KB
/
qrcode_generate_Data.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python
""" Generate python modules from QR Code data files.
Just run it in directory which contains 'qrcode' (QR Code python library)
and 'qrcode_data' subdirs.
"""
import hubarcode.qrcode.isodata as isodata
import cPickle
import os
if not os.path.exists('qrcode/data'):
os.mkdir('qrcode/data')
INIT = open('qrcode/data/__init__.py', 'w')
INIT.close()
ROOT_DIR = os.path.dirname(__file__)
QRCODE_DIR = os.path.join(ROOT_DIR, 'qrcode')
QRCODE_DATA_DIR = os.path.join(QRCODE_DIR, 'qrcode_data')
for version in range(1, 41):
for ecl in range(0, 4):
byte_num = (isodata.MATRIX_REMAIN_BIT[version] +
(isodata.MAX_CODEWORDS[version] << 3))
filename = 'qrv%s_%s.dat' % (version, ecl)
filename = os.path.join(QRCODE_DATA_DIR, filename)
fhndl = open(filename, "rb")
unpack = lambda y: [ord(x) for x in y]
matrix_d = []
matrix_d.append(unpack(fhndl.read(byte_num)))
matrix_d.append(unpack(fhndl.read(byte_num)))
matrix_d.append(unpack(fhndl.read(byte_num)))
format_info = []
format_info.append(unpack(fhndl.read(15)))
format_info.append(unpack(fhndl.read(15)))
rs_ecc_codewords = ord(fhndl.read(1))
rs_block_order = unpack(fhndl.read(128))
fhndl.close()
qrv = open('qrcode/data/qrv%d_%d.py' % (version, ecl), 'w')
qrv.write('import cPickle\n\n')
qrv.write('byte_num = %d\n' % byte_num)
qrv.write('matrix_d = cPickle.loads("""%s""")\n' %
cPickle.dumps(matrix_d))
qrv.write('format_info = cPickle.loads("""%s""")\n' %
cPickle.dumps(format_info))
qrv.write('rs_ecc_codewords = %d\n' % rs_ecc_codewords)
qrv.write('rs_block_order = cPickle.loads("""%s""")\n' %
cPickle.dumps(rs_block_order))
qrv.write('from hubarcode.qrcode.data.rsc%d import rs_cal_table\n' %
rs_ecc_codewords)
qrv.write('from hubarcode.qrcode.data.fr%d import frame_data\n' % version)
qrv.close()
filename = 'rsc%s.dat' % rs_ecc_codewords
filename = os.path.join(QRCODE_DATA_DIR, filename)
fhndl = open(filename, "rb")
rs_cal_table = []
for _ in range(0, 256):
rs_cal_table.append(unpack(fhndl.read(rs_ecc_codewords)))
# end for
fhndl.close()
rsc = open('qrcode/data/rsc%d.py' % rs_ecc_codewords, 'w')
rsc.write('import cPickle\n\n')
rsc.write('rs_cal_table = cPickle.loads("""%s""")\n' %
cPickle.dumps(rs_cal_table))
rsc.close()
filename = 'qrvfr%s.dat' % version
filename = os.path.join(QRCODE_DATA_DIR, filename)
fhndl = open(filename, "rb")
frame_data_str = fhndl.read(65535)
frame_data = []
for line in frame_data_str.split("\n"):
frame_line = []
for char in line:
if char == '1':
frame_line.append(1)
elif char == '0':
frame_line.append(0)
else:
raise ValueError("Corrupted frame data file")
# end if
# end for
frame_data.append(frame_line)
# end for
fhndl.close()
fr = open('qrcode/data/fr%d.py' % version, 'w')
fr.write('import cPickle\n\n')
fr.write('frame_data = cPickle.loads("""%s""")\n' %
cPickle.dumps(frame_data))
fr.close()