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
/
qrcode_generate_module.py
96 lines (77 loc) · 2.92 KB
/
qrcode_generate_module.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
#!/usr/bin/env python
""" Generate python module (qrcode.mem) 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
ROOT_DIR = os.path.dirname(__file__)
QRCODE_DIR = os.path.join(ROOT_DIR, 'qrcode')
QRCODE_DATA_DIR = os.path.join(QRCODE_DIR, 'qrcode_data')
matrix_d_dict = {}
format_info_dict = {}
rs_ecc_codewords_dict = {}
rs_block_order_dict = {}
rs_cal_table_dict = {}
frame_data_dict = {}
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()
key = '%s_%s' % (version, ecl)
matrix_d_dict[key] = matrix_d
format_info_dict[key] = format_info
rs_ecc_codewords_dict[key] = rs_ecc_codewords
rs_block_order_dict[key] = rs_block_order
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()
rs_cal_table_dict[rs_ecc_codewords] = rs_cal_table
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()
frame_data_dict[version] = frame_data
mem = open('qrcode/mem.py', 'w')
mem.write('matrix_d = %s\n' % repr(matrix_d_dict))
mem.write('format_info = %s\n' % repr(format_info_dict))
mem.write('rs_ecc_codewords = %s\n' % repr(rs_ecc_codewords_dict))
mem.write('rs_block_order = %s\n' % repr(rs_block_order_dict))
mem.write('rs_cal_table = %s\n' % repr(rs_cal_table_dict))
mem.write('frame_data = %s\n' % repr(frame_data_dict))
mem.close()