You've already forked amazing-qr
mirror of
https://github.com/x-hw/amazing-qr.git
synced 2025-07-13 01:20:29 +02:00
complete more
This commit is contained in:
3
ecc.py
3
ecc.py
@ -38,8 +38,9 @@ def XOR(MP, GP):
|
|||||||
GP += [0] * a
|
GP += [0] * a
|
||||||
|
|
||||||
remainder = []
|
remainder = []
|
||||||
for i in range(1, len(MP)):
|
for i in range(len(MP)):
|
||||||
remainder.append(MP[i]^GP[i])
|
remainder.append(MP[i]^GP[i])
|
||||||
|
remainder = [i for i in remainder if i]
|
||||||
return remainder
|
return remainder
|
||||||
|
|
||||||
def get_power_of_2_list():
|
def get_power_of_2_list():
|
||||||
|
83
matrix.py
83
matrix.py
@ -1,2 +1,85 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- 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])
|
@ -71,4 +71,9 @@ alphanum_list = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:'
|
|||||||
mindex = {'numeric':0, 'alphanumeric':1, 'byte':2, 'kanji':3}
|
mindex = {'numeric':0, 'alphanumeric':1, 'byte':2, 'kanji':3}
|
||||||
lindex = {'L':0, 'M':1, 'Q':2, 'H':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)
|
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)
|
||||||
|
]
|
13
myqrcode.py
13
myqrcode.py
@ -1,6 +1,6 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import draw, ECC, data, structure
|
import data, ECC, structure, matrix, draw
|
||||||
|
|
||||||
# ecl: Error Correction Level(L,M,Q,H)
|
# ecl: Error Correction Level(L,M,Q,H)
|
||||||
def get_qrcode(ecl, str):
|
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)
|
final_bits = structure.structure_final_bits(ver, ecl, data_codewords, ecc)
|
||||||
|
|
||||||
# Get the QR Matrix
|
# 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:
|
except UnicodeEncodeError:
|
||||||
print('Error input!!')
|
print('Error input!!')
|
||||||
@ -24,5 +28,6 @@ def get_qrcode(ecl, str):
|
|||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# test:
|
# test:
|
||||||
str = 'HELLO WORLD'
|
str = 'HELLO WORLD'
|
||||||
str2 = '💩'
|
str2 = 'http://www.thonky.com/qr-code-tutorial/log-antilog-table'
|
||||||
get_qrcode('M',str)
|
err = '💩'
|
||||||
|
get_qrcode('H',str2)
|
Reference in New Issue
Block a user