From e6fe986e6d024b3ac95a6fcd274129cb8f8988a7 Mon Sep 17 00:00:00 2001 From: sylnsfar Date: Sun, 28 Aug 2016 23:07:55 +0800 Subject: [PATCH] complete more --- ecc.py | 3 +- matrix.py | 83 ++++++++++++++++++++++++++++++++++++++++++++++ mylibs/constant.py | 7 +++- myqrcode.py | 13 +++++--- 4 files changed, 100 insertions(+), 6 deletions(-) diff --git a/ecc.py b/ecc.py index fe0465b..522645b 100644 --- a/ecc.py +++ b/ecc.py @@ -38,8 +38,9 @@ def XOR(MP, GP): GP += [0] * a remainder = [] - for i in range(1, len(MP)): + for i in range(len(MP)): remainder.append(MP[i]^GP[i]) + remainder = [i for i in remainder if i] return remainder def get_power_of_2_list(): diff --git a/matrix.py b/matrix.py index 633f866..eca2d99 100644 --- a/matrix.py +++ b/matrix.py @@ -1,2 +1,85 @@ # -*- coding: utf-8 -*- +from mylibs.constant import * + +def get_qrmatrix(ver, ecl, bits): + num = (ver - 1) * 4 + 21 + qrmatrix = [([None] * num * num)[i:i+num] for i in range(num * num) if i % num == 0] + + # Add the Finder Patterns & Add the Separators + add_finder_and_separator(qrmatrix) + + # Add the Alignment Patterns + add_alignment(ver, qrmatrix) + + # Add the Timing Patterns + add_timing(qrmatrix) + + # Add the Dark Module and Reserved Areas + add_dark_and_reserving(ver, qrmatrix) + + # Place the Data Bits + place_bits(bits, qrmatrix) + + # Data Masking + + + return qrmatrix + +def add_finder_and_separator(m): + for i in range(8): + for j in range(8): + if i in (0, 6): + m[i][j] = m[-i-1][j] = m[i][-j-1] = 0 if j == 7 else 1 + elif i in (1, 5): + m[i][j] = m[-i-1][j] = m[i][-j-1] = 1 if j in (0, 6) else 0 + elif i == 7: + m[i][j] = m[-i-1][j] = m[i][-j-1] = 0 + else: + m[i][j] = m[-i-1][j] = m[i][-j-1] = 0 if j in (1, 5, 7) else 1 + +def add_alignment(ver, m): + if ver > 1: + coordinates = alig_location[ver-2] + for i in coordinates: + for j in coordinates: + if m[i][j] is None: + add_an_alignment(i, j, m) + +def add_an_alignment(row, column, m): + for i in range(row-2, row+3): + for j in range(column-2, column+3): + m[i][j] = 1 if i in (row-2, row+2) or j in (column-2, column+2) else 0 + m[row][column] = 1 + +def add_timing(m): + for i in range(8, len(m)-8): + m[i][6] = m[6][i] = 1 if i % 2 ==0 else 0 + +def add_dark_and_reserving(ver, m): + for j in range(9): + m[8][j] = m[8][-j-1] = m[j][8] = m[-j-1][8] = 0 + m[8][6] = m[6][8] = m[-8][8] = 1 + + if ver > 6: + for i in range(6): + for j in (-9, -10, -11): + m[i][j] = m[j][i] = 0 + +def place_bits(bits, m): + bit = (int(i) for i in bits) + + up = True + for a in range(len(m)-1, 0, -2): + a = a-1 if a <= 6 else a + irange = range(len(m)-1, -1, -1) if up else range(len(m)) + for i in irange: + for j in (a, a-1): + if m[i][j] is None: + m[i][j] = next(bit) + up = not up + +if __name__ == '__main__': + m = get_qrmatrix(7,'H',1) + for i in range(len(m)): + print(m[i]) \ No newline at end of file diff --git a/mylibs/constant.py b/mylibs/constant.py index c5a5173..a9e3f8d 100644 --- a/mylibs/constant.py +++ b/mylibs/constant.py @@ -71,4 +71,9 @@ alphanum_list = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:' mindex = {'numeric':0, 'alphanumeric':1, 'byte':2, 'kanji':3} lindex = {'L':0, 'M':1, 'Q':2, 'H':3} -required_remainder_bits = (0,7,7,7,7,7,0,0,0,0,0,0,0,3,3,3,3,3,3,3,4,4,4,4,4,4,4,3,3,3,3,3,3,3,0,0,0,0,0,0) \ No newline at end of file +required_remainder_bits = (0,7,7,7,7,7,0,0,0,0,0,0,0,3,3,3,3,3,3,3,4,4,4,4,4,4,4,3,3,3,3,3,3,3,0,0,0,0,0,0) + +# Alignment Pattern Locations +alig_location = [ + (6, 18), (6, 22), (6, 26), (6, 30), (6, 34), (6, 22, 38), (6, 24, 42), (6, 26, 46), (6, 28, 50), (6, 30, 54), (6, 32, 58), (6, 34, 62), (6, 26, 46, 66), (6, 26, 48, 70), (6, 26, 50, 74), (6, 30, 54, 78), (6, 30, 56, 82), (6, 30, 58, 86), (6, 34, 62, 90), (6, 28, 50, 72, 94), (6, 26, 50, 74, 98), (6, 30, 54, 78, 102), (6, 28, 54, 80, 106), (6, 32, 58, 84, 110), (6, 30, 58, 86, 114), (6, 34, 62, 90, 118), (6, 26, 50, 74, 98, 122), (6, 30, 54, 78, 102, 126), (6, 26, 52, 78, 104, 130), (6, 30, 56, 82, 108, 134), (6, 34, 60, 86, 112, 138), (6, 30, 58, 86, 114, 142), (6, 34, 62, 90, 118, 146), (6, 30, 54, 78, 102, 126, 150), (6, 24, 50, 76, 102, 128, 154), (6, 28, 54, 80, 106, 132, 158), (6, 32, 58, 84, 110, 136, 162), (6, 26, 54, 82, 110, 138, 166), (6, 30, 58, 86, 114, 142, 170) + ] \ No newline at end of file diff --git a/myqrcode.py b/myqrcode.py index 2f8adcd..9804f63 100644 --- a/myqrcode.py +++ b/myqrcode.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -import draw, ECC, data, structure +import data, ECC, structure, matrix, draw # ecl: Error Correction Level(L,M,Q,H) def get_qrcode(ecl, str): @@ -15,7 +15,11 @@ def get_qrcode(ecl, str): final_bits = structure.structure_final_bits(ver, ecl, data_codewords, ecc) # Get the QR Matrix - + qrmatrix = matrix.get_qrmatrix(ver, ecl, final_bits) + for i in qrmatrix: + print(i) + # Draw the picture + draw.draw_qrcode(qrmatrix) except UnicodeEncodeError: print('Error input!!') @@ -24,5 +28,6 @@ def get_qrcode(ecl, str): if __name__ == '__main__': # test: str = 'HELLO WORLD' - str2 = '💩' - get_qrcode('M',str) \ No newline at end of file + str2 = 'http://www.thonky.com/qr-code-tutorial/log-antilog-table' + err = '💩' + get_qrcode('H',str2) \ No newline at end of file