|
|
@ -23,41 +23,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
from __future__ import annotations
|
|
|
|
import collections, itertools, re
|
|
|
|
import collections, itertools, re
|
|
|
|
from typing import List, Optional, Tuple
|
|
|
|
from collections.abc import Sequence
|
|
|
|
|
|
|
|
from typing import Callable, Dict, List, Optional, Tuple, Union
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
This module "qrcodegen", public members:
|
|
|
|
|
|
|
|
- Class QrCode:
|
|
|
|
|
|
|
|
- Function encode_text(str text, QrCode.Ecc ecl) -> QrCode
|
|
|
|
|
|
|
|
- Function encode_binary(bytes data, QrCode.Ecc ecl) -> QrCode
|
|
|
|
|
|
|
|
- Function encode_segments(list<QrSegment> segs, QrCode.Ecc ecl,
|
|
|
|
|
|
|
|
int minversion=1, int maxversion=40, mask=-1, boostecl=true) -> QrCode
|
|
|
|
|
|
|
|
- Constants int MIN_VERSION, MAX_VERSION
|
|
|
|
|
|
|
|
- Constructor QrCode(int version, QrCode.Ecc ecl, bytes datacodewords, int mask)
|
|
|
|
|
|
|
|
- Method get_version() -> int
|
|
|
|
|
|
|
|
- Method get_size() -> int
|
|
|
|
|
|
|
|
- Method get_error_correction_level() -> QrCode.Ecc
|
|
|
|
|
|
|
|
- Method get_mask() -> int
|
|
|
|
|
|
|
|
- Method get_module(int x, int y) -> bool
|
|
|
|
|
|
|
|
- Method to_svg_str(int border) -> str
|
|
|
|
|
|
|
|
- Enum Ecc:
|
|
|
|
|
|
|
|
- Constants LOW, MEDIUM, QUARTILE, HIGH
|
|
|
|
|
|
|
|
- Field int ordinal
|
|
|
|
|
|
|
|
- Class QrSegment:
|
|
|
|
|
|
|
|
- Function make_bytes(bytes data) -> QrSegment
|
|
|
|
|
|
|
|
- Function make_numeric(str digits) -> QrSegment
|
|
|
|
|
|
|
|
- Function make_alphanumeric(str text) -> QrSegment
|
|
|
|
|
|
|
|
- Function make_segments(str text) -> list<QrSegment>
|
|
|
|
|
|
|
|
- Function make_eci(int assignval) -> QrSegment
|
|
|
|
|
|
|
|
- Constructor QrSegment(QrSegment.Mode mode, int numch, list<int> bitdata)
|
|
|
|
|
|
|
|
- Method get_mode() -> QrSegment.Mode
|
|
|
|
|
|
|
|
- Method get_num_chars() -> int
|
|
|
|
|
|
|
|
- Method get_data() -> list<int>
|
|
|
|
|
|
|
|
- Constants regex NUMERIC_REGEX, ALPHANUMERIC_REGEX
|
|
|
|
|
|
|
|
- Enum Mode:
|
|
|
|
|
|
|
|
- Constants NUMERIC, ALPHANUMERIC, BYTE, KANJI, ECI
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---- QR Code symbol class ----
|
|
|
|
# ---- QR Code symbol class ----
|
|
|
@ -87,25 +54,23 @@ class QrCode:
|
|
|
|
Unicode code points (not UTF-16 code units) if the low error correction level is used. The smallest possible
|
|
|
|
Unicode code points (not UTF-16 code units) if the low error correction level is used. The smallest possible
|
|
|
|
QR Code version is automatically chosen for the output. The ECC level of the result may be higher than the
|
|
|
|
QR Code version is automatically chosen for the output. The ECC level of the result may be higher than the
|
|
|
|
ecl argument if it can be done without increasing the version."""
|
|
|
|
ecl argument if it can be done without increasing the version."""
|
|
|
|
segs = QrSegment.make_segments(text)
|
|
|
|
segs: List[QrSegment] = QrSegment.make_segments(text)
|
|
|
|
return QrCode.encode_segments(segs, ecl)
|
|
|
|
return QrCode.encode_segments(segs, ecl)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@staticmethod
|
|
|
|
def encode_binary(data: bytes, ecl: QrCode.Ecc) -> QrCode:
|
|
|
|
def encode_binary(data: Union[bytes,Sequence[int]], ecl: QrCode.Ecc) -> QrCode:
|
|
|
|
"""Returns a QR Code representing the given binary data at the given error correction level.
|
|
|
|
"""Returns a QR Code representing the given binary data at the given error correction level.
|
|
|
|
This function always encodes using the binary segment mode, not any text mode. The maximum number of
|
|
|
|
This function always encodes using the binary segment mode, not any text mode. The maximum number of
|
|
|
|
bytes allowed is 2953. The smallest possible QR Code version is automatically chosen for the output.
|
|
|
|
bytes allowed is 2953. The smallest possible QR Code version is automatically chosen for the output.
|
|
|
|
The ECC level of the result may be higher than the ecl argument if it can be done without increasing the version."""
|
|
|
|
The ECC level of the result may be higher than the ecl argument if it can be done without increasing the version."""
|
|
|
|
if not isinstance(data, (bytes, bytearray)):
|
|
|
|
|
|
|
|
raise TypeError("Byte string/list expected")
|
|
|
|
|
|
|
|
return QrCode.encode_segments([QrSegment.make_bytes(data)], ecl)
|
|
|
|
return QrCode.encode_segments([QrSegment.make_bytes(data)], ecl)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---- Static factory functions (mid level) ----
|
|
|
|
# ---- Static factory functions (mid level) ----
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@staticmethod
|
|
|
|
def encode_segments(segs: List[QrSegment], ecl: QrCode.Ecc, minversion: int = 1, maxversion: int = 40, mask: int = -1, boostecl: bool = True) -> QrCode:
|
|
|
|
def encode_segments(segs: Sequence[QrSegment], ecl: QrCode.Ecc, minversion: int = 1, maxversion: int = 40, mask: int = -1, boostecl: bool = True) -> QrCode:
|
|
|
|
"""Returns a QR Code representing the given segments with the given encoding parameters.
|
|
|
|
"""Returns a QR Code representing the given segments with the given encoding parameters.
|
|
|
|
The smallest possible QR Code version within the given range is automatically
|
|
|
|
The smallest possible QR Code version within the given range is automatically
|
|
|
|
chosen for the output. Iff boostecl is true, then the ECC level of the result
|
|
|
|
chosen for the output. Iff boostecl is true, then the ECC level of the result
|
|
|
@ -121,12 +86,12 @@ class QrCode:
|
|
|
|
|
|
|
|
|
|
|
|
# Find the minimal version number to use
|
|
|
|
# Find the minimal version number to use
|
|
|
|
for version in range(minversion, maxversion + 1):
|
|
|
|
for version in range(minversion, maxversion + 1):
|
|
|
|
datacapacitybits = QrCode._get_num_data_codewords(version, ecl) * 8 # Number of data bits available
|
|
|
|
datacapacitybits: int = QrCode._get_num_data_codewords(version, ecl) * 8 # Number of data bits available
|
|
|
|
datausedbits = QrSegment.get_total_bits(segs, version)
|
|
|
|
datausedbits: Optional[int] = QrSegment.get_total_bits(segs, version)
|
|
|
|
if datausedbits is not None and datausedbits <= datacapacitybits:
|
|
|
|
if (datausedbits is not None) and (datausedbits <= datacapacitybits):
|
|
|
|
break # This version number is found to be suitable
|
|
|
|
break # This version number is found to be suitable
|
|
|
|
if version >= maxversion: # All versions in the range could not fit the given data
|
|
|
|
if version >= maxversion: # All versions in the range could not fit the given data
|
|
|
|
msg = "Segment too long"
|
|
|
|
msg: str = "Segment too long"
|
|
|
|
if datausedbits is not None:
|
|
|
|
if datausedbits is not None:
|
|
|
|
msg = "Data length = {} bits, Max capacity = {} bits".format(datausedbits, datacapacitybits)
|
|
|
|
msg = "Data length = {} bits, Max capacity = {} bits".format(datausedbits, datacapacitybits)
|
|
|
|
raise DataTooLongError(msg)
|
|
|
|
raise DataTooLongError(msg)
|
|
|
@ -135,7 +100,7 @@ class QrCode:
|
|
|
|
|
|
|
|
|
|
|
|
# Increase the error correction level while the data still fits in the current version number
|
|
|
|
# Increase the error correction level while the data still fits in the current version number
|
|
|
|
for newecl in (QrCode.Ecc.MEDIUM, QrCode.Ecc.QUARTILE, QrCode.Ecc.HIGH): # From low to high
|
|
|
|
for newecl in (QrCode.Ecc.MEDIUM, QrCode.Ecc.QUARTILE, QrCode.Ecc.HIGH): # From low to high
|
|
|
|
if boostecl and datausedbits <= QrCode._get_num_data_codewords(version, newecl) * 8:
|
|
|
|
if boostecl and (datausedbits <= QrCode._get_num_data_codewords(version, newecl) * 8):
|
|
|
|
ecl = newecl
|
|
|
|
ecl = newecl
|
|
|
|
|
|
|
|
|
|
|
|
# Concatenate all segments to create the data bit string
|
|
|
|
# Concatenate all segments to create the data bit string
|
|
|
@ -160,7 +125,7 @@ class QrCode:
|
|
|
|
bb.append_bits(padbyte, 8)
|
|
|
|
bb.append_bits(padbyte, 8)
|
|
|
|
|
|
|
|
|
|
|
|
# Pack bits into bytes in big endian
|
|
|
|
# Pack bits into bytes in big endian
|
|
|
|
datacodewords = [0] * (len(bb) // 8)
|
|
|
|
datacodewords = bytearray([0] * (len(bb) // 8))
|
|
|
|
for (i, bit) in enumerate(bb):
|
|
|
|
for (i, bit) in enumerate(bb):
|
|
|
|
datacodewords[i >> 3] |= bit << (7 - (i & 7))
|
|
|
|
datacodewords[i >> 3] |= bit << (7 - (i & 7))
|
|
|
|
|
|
|
|
|
|
|
@ -168,9 +133,35 @@ class QrCode:
|
|
|
|
return QrCode(version, ecl, datacodewords, mask)
|
|
|
|
return QrCode(version, ecl, datacodewords, mask)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---- Private fields ----
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# The version number of this QR Code, which is between 1 and 40 (inclusive).
|
|
|
|
|
|
|
|
# This determines the size of this barcode.
|
|
|
|
|
|
|
|
_version: int
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# The width and height of this QR Code, measured in modules, between
|
|
|
|
|
|
|
|
# 21 and 177 (inclusive). This is equal to version * 4 + 17.
|
|
|
|
|
|
|
|
_size: int
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# The error correction level used in this QR Code.
|
|
|
|
|
|
|
|
_errcorlvl: QrCode.Ecc
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# The index of the mask pattern used in this QR Code, which is between 0 and 7 (inclusive).
|
|
|
|
|
|
|
|
# Even if a QR Code is created with automatic masking requested (mask = -1),
|
|
|
|
|
|
|
|
# the resulting object still has a mask value between 0 and 7.
|
|
|
|
|
|
|
|
_mask: int
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# The modules of this QR Code (False = white, True = black).
|
|
|
|
|
|
|
|
# Immutable after constructor finishes. Accessed through get_module().
|
|
|
|
|
|
|
|
_modules: List[List[bool]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Indicates function modules that are not subjected to masking. Discarded when constructor finishes.
|
|
|
|
|
|
|
|
_isfunction: List[List[bool]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---- Constructor (low level) ----
|
|
|
|
# ---- Constructor (low level) ----
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, version: int, errcorlvl: QrCode.Ecc, datacodewords: List[int], mask: int) -> None:
|
|
|
|
def __init__(self, version: int, errcorlvl: QrCode.Ecc, datacodewords: Union[bytes,Sequence[int]], mask: int) -> None:
|
|
|
|
"""Creates a new QR Code with the given version number,
|
|
|
|
"""Creates a new QR Code with the given version number,
|
|
|
|
error correction level, data codeword bytes, and mask number.
|
|
|
|
error correction level, data codeword bytes, and mask number.
|
|
|
|
This is a low-level API that most users should not use directly.
|
|
|
|
This is a low-level API that most users should not use directly.
|
|
|
@ -184,32 +175,22 @@ class QrCode:
|
|
|
|
if not isinstance(errcorlvl, QrCode.Ecc):
|
|
|
|
if not isinstance(errcorlvl, QrCode.Ecc):
|
|
|
|
raise TypeError("QrCode.Ecc expected")
|
|
|
|
raise TypeError("QrCode.Ecc expected")
|
|
|
|
|
|
|
|
|
|
|
|
# The version number of this QR Code, which is between 1 and 40 (inclusive).
|
|
|
|
|
|
|
|
# This determines the size of this barcode.
|
|
|
|
|
|
|
|
self._version = version
|
|
|
|
self._version = version
|
|
|
|
|
|
|
|
|
|
|
|
# The width and height of this QR Code, measured in modules, between
|
|
|
|
|
|
|
|
# 21 and 177 (inclusive). This is equal to version * 4 + 17.
|
|
|
|
|
|
|
|
self._size = version * 4 + 17
|
|
|
|
self._size = version * 4 + 17
|
|
|
|
|
|
|
|
|
|
|
|
# The error correction level used in this QR Code.
|
|
|
|
|
|
|
|
self._errcorlvl = errcorlvl
|
|
|
|
self._errcorlvl = errcorlvl
|
|
|
|
|
|
|
|
|
|
|
|
# Initialize both grids to be size*size arrays of Boolean false
|
|
|
|
# Initialize both grids to be size*size arrays of Boolean false
|
|
|
|
# The modules of this QR Code (False = white, True = black).
|
|
|
|
|
|
|
|
# Immutable after constructor finishes. Accessed through get_module().
|
|
|
|
|
|
|
|
self._modules = [[False] * self._size for _ in range(self._size)] # Initially all white
|
|
|
|
self._modules = [[False] * self._size for _ in range(self._size)] # Initially all white
|
|
|
|
# Indicates function modules that are not subjected to masking. Discarded when constructor finishes
|
|
|
|
|
|
|
|
self._isfunction = [[False] * self._size for _ in range(self._size)]
|
|
|
|
self._isfunction = [[False] * self._size for _ in range(self._size)]
|
|
|
|
|
|
|
|
|
|
|
|
# Compute ECC, draw modules
|
|
|
|
# Compute ECC, draw modules
|
|
|
|
self._draw_function_patterns()
|
|
|
|
self._draw_function_patterns()
|
|
|
|
allcodewords = self._add_ecc_and_interleave(datacodewords)
|
|
|
|
allcodewords: bytes = self._add_ecc_and_interleave(bytearray(datacodewords))
|
|
|
|
self._draw_codewords(allcodewords)
|
|
|
|
self._draw_codewords(allcodewords)
|
|
|
|
|
|
|
|
|
|
|
|
# Do masking
|
|
|
|
# Do masking
|
|
|
|
if mask == -1: # Automatically choose best mask
|
|
|
|
if mask == -1: # Automatically choose best mask
|
|
|
|
minpenalty = 1 << 32
|
|
|
|
minpenalty: int = 1 << 32
|
|
|
|
for i in range(8):
|
|
|
|
for i in range(8):
|
|
|
|
self._apply_mask(i)
|
|
|
|
self._apply_mask(i)
|
|
|
|
self._draw_format_bits(i)
|
|
|
|
self._draw_format_bits(i)
|
|
|
@ -221,10 +202,6 @@ class QrCode:
|
|
|
|
assert 0 <= mask <= 7
|
|
|
|
assert 0 <= mask <= 7
|
|
|
|
self._apply_mask(mask) # Apply the final choice of mask
|
|
|
|
self._apply_mask(mask) # Apply the final choice of mask
|
|
|
|
self._draw_format_bits(mask) # Overwrite old format bits
|
|
|
|
self._draw_format_bits(mask) # Overwrite old format bits
|
|
|
|
|
|
|
|
|
|
|
|
# The index of the mask pattern used in this QR Code, which is between 0 and 7 (inclusive).
|
|
|
|
|
|
|
|
# Even if a QR Code is created with automatic masking requested (mask = -1),
|
|
|
|
|
|
|
|
# the resulting object still has a mask value between 0 and 7.
|
|
|
|
|
|
|
|
self._mask = mask
|
|
|
|
self._mask = mask
|
|
|
|
|
|
|
|
|
|
|
|
del self._isfunction
|
|
|
|
del self._isfunction
|
|
|
@ -248,7 +225,7 @@ class QrCode:
|
|
|
|
"""Returns this QR Code's mask, in the range [0, 7]."""
|
|
|
|
"""Returns this QR Code's mask, in the range [0, 7]."""
|
|
|
|
return self._mask
|
|
|
|
return self._mask
|
|
|
|
|
|
|
|
|
|
|
|
def get_module(self, x, y) -> bool:
|
|
|
|
def get_module(self, x: int, y: int) -> bool:
|
|
|
|
"""Returns the color of the module (pixel) at the given coordinates, which is False
|
|
|
|
"""Returns the color of the module (pixel) at the given coordinates, which is False
|
|
|
|
for white or True for black. The top left corner has the coordinates (x=0, y=0).
|
|
|
|
for white or True for black. The top left corner has the coordinates (x=0, y=0).
|
|
|
|
If the given coordinates are out of bounds, then False (white) is returned."""
|
|
|
|
If the given coordinates are out of bounds, then False (white) is returned."""
|
|
|
@ -262,7 +239,7 @@ class QrCode:
|
|
|
|
of border modules. The string always uses Unix newlines (\n), regardless of the platform."""
|
|
|
|
of border modules. The string always uses Unix newlines (\n), regardless of the platform."""
|
|
|
|
if border < 0:
|
|
|
|
if border < 0:
|
|
|
|
raise ValueError("Border must be non-negative")
|
|
|
|
raise ValueError("Border must be non-negative")
|
|
|
|
parts = []
|
|
|
|
parts: List[str] = []
|
|
|
|
for y in range(self._size):
|
|
|
|
for y in range(self._size):
|
|
|
|
for x in range(self._size):
|
|
|
|
for x in range(self._size):
|
|
|
|
if self.get_module(x, y):
|
|
|
|
if self.get_module(x, y):
|
|
|
@ -291,9 +268,9 @@ class QrCode:
|
|
|
|
self._draw_finder_pattern(3, self._size - 4)
|
|
|
|
self._draw_finder_pattern(3, self._size - 4)
|
|
|
|
|
|
|
|
|
|
|
|
# Draw numerous alignment patterns
|
|
|
|
# Draw numerous alignment patterns
|
|
|
|
alignpatpos = self._get_alignment_pattern_positions()
|
|
|
|
alignpatpos: List[int] = self._get_alignment_pattern_positions()
|
|
|
|
numalign = len(alignpatpos)
|
|
|
|
numalign: int = len(alignpatpos)
|
|
|
|
skips = ((0, 0), (0, numalign - 1), (numalign - 1, 0))
|
|
|
|
skips: Sequence[Tuple[int,int]] = ((0, 0), (0, numalign - 1), (numalign - 1, 0))
|
|
|
|
for i in range(numalign):
|
|
|
|
for i in range(numalign):
|
|
|
|
for j in range(numalign):
|
|
|
|
for j in range(numalign):
|
|
|
|
if (i, j) not in skips: # Don't draw on the three finder corners
|
|
|
|
if (i, j) not in skips: # Don't draw on the three finder corners
|
|
|
@ -304,15 +281,15 @@ class QrCode:
|
|
|
|
self._draw_version()
|
|
|
|
self._draw_version()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _draw_format_bits(self, mask) -> None:
|
|
|
|
def _draw_format_bits(self, mask: int) -> None:
|
|
|
|
"""Draws two copies of the format bits (with its own error correction code)
|
|
|
|
"""Draws two copies of the format bits (with its own error correction code)
|
|
|
|
based on the given mask and this object's error correction level field."""
|
|
|
|
based on the given mask and this object's error correction level field."""
|
|
|
|
# Calculate error correction code and pack bits
|
|
|
|
# Calculate error correction code and pack bits
|
|
|
|
data = self._errcorlvl.formatbits << 3 | mask # errCorrLvl is uint2, mask is uint3
|
|
|
|
data: int = self._errcorlvl.formatbits << 3 | mask # errCorrLvl is uint2, mask is uint3
|
|
|
|
rem = data
|
|
|
|
rem: int = data
|
|
|
|
for _ in range(10):
|
|
|
|
for _ in range(10):
|
|
|
|
rem = (rem << 1) ^ ((rem >> 9) * 0x537)
|
|
|
|
rem = (rem << 1) ^ ((rem >> 9) * 0x537)
|
|
|
|
bits = (data << 10 | rem) ^ 0x5412 # uint15
|
|
|
|
bits: int = (data << 10 | rem) ^ 0x5412 # uint15
|
|
|
|
assert bits >> 15 == 0
|
|
|
|
assert bits >> 15 == 0
|
|
|
|
|
|
|
|
|
|
|
|
# Draw first copy
|
|
|
|
# Draw first copy
|
|
|
@ -339,22 +316,22 @@ class QrCode:
|
|
|
|
return
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
# Calculate error correction code and pack bits
|
|
|
|
# Calculate error correction code and pack bits
|
|
|
|
rem = self._version # version is uint6, in the range [7, 40]
|
|
|
|
rem: int = self._version # version is uint6, in the range [7, 40]
|
|
|
|
for _ in range(12):
|
|
|
|
for _ in range(12):
|
|
|
|
rem = (rem << 1) ^ ((rem >> 11) * 0x1F25)
|
|
|
|
rem = (rem << 1) ^ ((rem >> 11) * 0x1F25)
|
|
|
|
bits = self._version << 12 | rem # uint18
|
|
|
|
bits: int = self._version << 12 | rem # uint18
|
|
|
|
assert bits >> 18 == 0
|
|
|
|
assert bits >> 18 == 0
|
|
|
|
|
|
|
|
|
|
|
|
# Draw two copies
|
|
|
|
# Draw two copies
|
|
|
|
for i in range(18):
|
|
|
|
for i in range(18):
|
|
|
|
bit = _get_bit(bits, i)
|
|
|
|
bit: bool = _get_bit(bits, i)
|
|
|
|
a = self._size - 11 + i % 3
|
|
|
|
a: int = self._size - 11 + i % 3
|
|
|
|
b = i // 3
|
|
|
|
b: int = i // 3
|
|
|
|
self._set_function_module(a, b, bit)
|
|
|
|
self._set_function_module(a, b, bit)
|
|
|
|
self._set_function_module(b, a, bit)
|
|
|
|
self._set_function_module(b, a, bit)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _draw_finder_pattern(self, x, y) -> None:
|
|
|
|
def _draw_finder_pattern(self, x: int, y: int) -> None:
|
|
|
|
"""Draws a 9*9 finder pattern including the border separator,
|
|
|
|
"""Draws a 9*9 finder pattern including the border separator,
|
|
|
|
with the center module at (x, y). Modules can be out of bounds."""
|
|
|
|
with the center module at (x, y). Modules can be out of bounds."""
|
|
|
|
for dy in range(-4, 5):
|
|
|
|
for dy in range(-4, 5):
|
|
|
@ -365,7 +342,7 @@ class QrCode:
|
|
|
|
self._set_function_module(xx, yy, max(abs(dx), abs(dy)) not in (2, 4))
|
|
|
|
self._set_function_module(xx, yy, max(abs(dx), abs(dy)) not in (2, 4))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _draw_alignment_pattern(self, x, y) -> None:
|
|
|
|
def _draw_alignment_pattern(self, x: int, y: int) -> None:
|
|
|
|
"""Draws a 5*5 alignment pattern, with the center module
|
|
|
|
"""Draws a 5*5 alignment pattern, with the center module
|
|
|
|
at (x, y). All modules must be in bounds."""
|
|
|
|
at (x, y). All modules must be in bounds."""
|
|
|
|
for dy in range(-2, 3):
|
|
|
|
for dy in range(-2, 3):
|
|
|
@ -383,59 +360,59 @@ class QrCode:
|
|
|
|
|
|
|
|
|
|
|
|
# ---- Private helper methods for constructor: Codewords and masking ----
|
|
|
|
# ---- Private helper methods for constructor: Codewords and masking ----
|
|
|
|
|
|
|
|
|
|
|
|
def _add_ecc_and_interleave(self, data: List[int]) -> List[int]:
|
|
|
|
def _add_ecc_and_interleave(self, data: bytearray) -> bytes:
|
|
|
|
"""Returns a new byte string representing the given data with the appropriate error correction
|
|
|
|
"""Returns a new byte string representing the given data with the appropriate error correction
|
|
|
|
codewords appended to it, based on this object's version and error correction level."""
|
|
|
|
codewords appended to it, based on this object's version and error correction level."""
|
|
|
|
version = self._version
|
|
|
|
version: int = self._version
|
|
|
|
assert len(data) == QrCode._get_num_data_codewords(version, self._errcorlvl)
|
|
|
|
assert len(data) == QrCode._get_num_data_codewords(version, self._errcorlvl)
|
|
|
|
|
|
|
|
|
|
|
|
# Calculate parameter numbers
|
|
|
|
# Calculate parameter numbers
|
|
|
|
numblocks = QrCode._NUM_ERROR_CORRECTION_BLOCKS[self._errcorlvl.ordinal][version]
|
|
|
|
numblocks: int = QrCode._NUM_ERROR_CORRECTION_BLOCKS[self._errcorlvl.ordinal][version]
|
|
|
|
blockecclen = QrCode._ECC_CODEWORDS_PER_BLOCK [self._errcorlvl.ordinal][version]
|
|
|
|
blockecclen: int = QrCode._ECC_CODEWORDS_PER_BLOCK [self._errcorlvl.ordinal][version]
|
|
|
|
rawcodewords = QrCode._get_num_raw_data_modules(version) // 8
|
|
|
|
rawcodewords: int = QrCode._get_num_raw_data_modules(version) // 8
|
|
|
|
numshortblocks = numblocks - rawcodewords % numblocks
|
|
|
|
numshortblocks: int = numblocks - rawcodewords % numblocks
|
|
|
|
shortblocklen = rawcodewords // numblocks
|
|
|
|
shortblocklen: int = rawcodewords // numblocks
|
|
|
|
|
|
|
|
|
|
|
|
# Split data into blocks and append ECC to each block
|
|
|
|
# Split data into blocks and append ECC to each block
|
|
|
|
blocks = []
|
|
|
|
blocks: List[bytes] = []
|
|
|
|
rsdiv = QrCode._reed_solomon_compute_divisor(blockecclen)
|
|
|
|
rsdiv: bytes = QrCode._reed_solomon_compute_divisor(blockecclen)
|
|
|
|
k = 0
|
|
|
|
k: int = 0
|
|
|
|
for i in range(numblocks):
|
|
|
|
for i in range(numblocks):
|
|
|
|
dat = data[k : k + shortblocklen - blockecclen + (0 if i < numshortblocks else 1)]
|
|
|
|
dat: bytearray = data[k : k + shortblocklen - blockecclen + (0 if i < numshortblocks else 1)]
|
|
|
|
k += len(dat)
|
|
|
|
k += len(dat)
|
|
|
|
ecc = QrCode._reed_solomon_compute_remainder(dat, rsdiv)
|
|
|
|
ecc: bytes = QrCode._reed_solomon_compute_remainder(dat, rsdiv)
|
|
|
|
if i < numshortblocks:
|
|
|
|
if i < numshortblocks:
|
|
|
|
dat.append(0)
|
|
|
|
dat.append(0)
|
|
|
|
blocks.append(dat + ecc)
|
|
|
|
blocks.append(dat + ecc)
|
|
|
|
assert k == len(data)
|
|
|
|
assert k == len(data)
|
|
|
|
|
|
|
|
|
|
|
|
# Interleave (not concatenate) the bytes from every block into a single sequence
|
|
|
|
# Interleave (not concatenate) the bytes from every block into a single sequence
|
|
|
|
result = []
|
|
|
|
result = bytearray()
|
|
|
|
for i in range(len(blocks[0])):
|
|
|
|
for i in range(len(blocks[0])):
|
|
|
|
for (j, blk) in enumerate(blocks):
|
|
|
|
for (j, blk) in enumerate(blocks):
|
|
|
|
# Skip the padding byte in short blocks
|
|
|
|
# Skip the padding byte in short blocks
|
|
|
|
if i != shortblocklen - blockecclen or j >= numshortblocks:
|
|
|
|
if (i != shortblocklen - blockecclen) or (j >= numshortblocks):
|
|
|
|
result.append(blk[i])
|
|
|
|
result.append(blk[i])
|
|
|
|
assert len(result) == rawcodewords
|
|
|
|
assert len(result) == rawcodewords
|
|
|
|
return result
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _draw_codewords(self, data: List[int]) -> None:
|
|
|
|
def _draw_codewords(self, data: bytes) -> None:
|
|
|
|
"""Draws the given sequence of 8-bit codewords (data and error correction) onto the entire
|
|
|
|
"""Draws the given sequence of 8-bit codewords (data and error correction) onto the entire
|
|
|
|
data area of this QR Code. Function modules need to be marked off before this is called."""
|
|
|
|
data area of this QR Code. Function modules need to be marked off before this is called."""
|
|
|
|
assert len(data) == QrCode._get_num_raw_data_modules(self._version) // 8
|
|
|
|
assert len(data) == QrCode._get_num_raw_data_modules(self._version) // 8
|
|
|
|
|
|
|
|
|
|
|
|
i = 0 # Bit index into the data
|
|
|
|
i: int = 0 # Bit index into the data
|
|
|
|
# Do the funny zigzag scan
|
|
|
|
# Do the funny zigzag scan
|
|
|
|
for right in range(self._size - 1, 0, -2): # Index of right column in each column pair
|
|
|
|
for right in range(self._size - 1, 0, -2): # Index of right column in each column pair
|
|
|
|
if right <= 6:
|
|
|
|
if right <= 6:
|
|
|
|
right -= 1
|
|
|
|
right -= 1
|
|
|
|
for vert in range(self._size): # Vertical counter
|
|
|
|
for vert in range(self._size): # Vertical counter
|
|
|
|
for j in range(2):
|
|
|
|
for j in range(2):
|
|
|
|
x = right - j # Actual x coordinate
|
|
|
|
x: int = right - j # Actual x coordinate
|
|
|
|
upward = (right + 1) & 2 == 0
|
|
|
|
upward: bool = (right + 1) & 2 == 0
|
|
|
|
y = (self._size - 1 - vert) if upward else vert # Actual y coordinate
|
|
|
|
y: int = (self._size - 1 - vert) if upward else vert # Actual y coordinate
|
|
|
|
if not self._isfunction[y][x] and i < len(data) * 8:
|
|
|
|
if (not self._isfunction[y][x]) and (i < len(data) * 8):
|
|
|
|
self._modules[y][x] = _get_bit(data[i >> 3], 7 - (i & 7))
|
|
|
|
self._modules[y][x] = _get_bit(data[i >> 3], 7 - (i & 7))
|
|
|
|
i += 1
|
|
|
|
i += 1
|
|
|
|
# If this QR Code has any remainder bits (0 to 7), they were assigned as
|
|
|
|
# If this QR Code has any remainder bits (0 to 7), they were assigned as
|
|
|
@ -446,12 +423,12 @@ class QrCode:
|
|
|
|
def _apply_mask(self, mask: int) -> None:
|
|
|
|
def _apply_mask(self, mask: int) -> None:
|
|
|
|
"""XORs the codeword modules in this QR Code with the given mask pattern.
|
|
|
|
"""XORs the codeword modules in this QR Code with the given mask pattern.
|
|
|
|
The function modules must be marked and the codeword bits must be drawn
|
|
|
|
The function modules must be marked and the codeword bits must be drawn
|
|
|
|
before masking. Due to the arithmetic of XOR, calling applyMask() with
|
|
|
|
before masking. Due to the arithmetic of XOR, calling _apply_mask() with
|
|
|
|
the same mask value a second time will undo the mask. A final well-formed
|
|
|
|
the same mask value a second time will undo the mask. A final well-formed
|
|
|
|
QR Code needs exactly one (not zero, two, etc.) mask applied."""
|
|
|
|
QR Code needs exactly one (not zero, two, etc.) mask applied."""
|
|
|
|
if not (0 <= mask <= 7):
|
|
|
|
if not (0 <= mask <= 7):
|
|
|
|
raise ValueError("Mask value out of range")
|
|
|
|
raise ValueError("Mask value out of range")
|
|
|
|
masker = QrCode._MASK_PATTERNS[mask]
|
|
|
|
masker: Callable[[int,int],int] = QrCode._MASK_PATTERNS[mask]
|
|
|
|
for y in range(self._size):
|
|
|
|
for y in range(self._size):
|
|
|
|
for x in range(self._size):
|
|
|
|
for x in range(self._size):
|
|
|
|
self._modules[y][x] ^= (masker(x, y) == 0) and (not self._isfunction[y][x])
|
|
|
|
self._modules[y][x] ^= (masker(x, y) == 0) and (not self._isfunction[y][x])
|
|
|
@ -460,14 +437,14 @@ class QrCode:
|
|
|
|
def _get_penalty_score(self) -> int:
|
|
|
|
def _get_penalty_score(self) -> int:
|
|
|
|
"""Calculates and returns the penalty score based on state of this QR Code's current modules.
|
|
|
|
"""Calculates and returns the penalty score based on state of this QR Code's current modules.
|
|
|
|
This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score."""
|
|
|
|
This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score."""
|
|
|
|
result = 0
|
|
|
|
result: int = 0
|
|
|
|
size = self._size
|
|
|
|
size: int = self._size
|
|
|
|
modules = self._modules
|
|
|
|
modules: List[List[bool]] = self._modules
|
|
|
|
|
|
|
|
|
|
|
|
# Adjacent modules in row having same color, and finder-like patterns
|
|
|
|
# Adjacent modules in row having same color, and finder-like patterns
|
|
|
|
for y in range(size):
|
|
|
|
for y in range(size):
|
|
|
|
runcolor = False
|
|
|
|
runcolor: bool = False
|
|
|
|
runx = 0
|
|
|
|
runx: int = 0
|
|
|
|
runhistory = collections.deque([0] * 7, 7)
|
|
|
|
runhistory = collections.deque([0] * 7, 7)
|
|
|
|
for x in range(size):
|
|
|
|
for x in range(size):
|
|
|
|
if modules[y][x] == runcolor:
|
|
|
|
if modules[y][x] == runcolor:
|
|
|
@ -510,10 +487,10 @@ class QrCode:
|
|
|
|
result += QrCode._PENALTY_N2
|
|
|
|
result += QrCode._PENALTY_N2
|
|
|
|
|
|
|
|
|
|
|
|
# Balance of black and white modules
|
|
|
|
# Balance of black and white modules
|
|
|
|
black = sum((1 if cell else 0) for row in modules for cell in row)
|
|
|
|
black: int = sum((1 if cell else 0) for row in modules for cell in row)
|
|
|
|
total = size**2 # Note that size is odd, so black/total != 1/2
|
|
|
|
total: int = size**2 # Note that size is odd, so black/total != 1/2
|
|
|
|
# Compute the smallest integer k >= 0 such that (45-5k)% <= black/total <= (55+5k)%
|
|
|
|
# Compute the smallest integer k >= 0 such that (45-5k)% <= black/total <= (55+5k)%
|
|
|
|
k = (abs(black * 20 - total * 10) + total - 1) // total - 1
|
|
|
|
k: int = (abs(black * 20 - total * 10) + total - 1) // total - 1
|
|
|
|
result += k * QrCode._PENALTY_N4
|
|
|
|
result += k * QrCode._PENALTY_N4
|
|
|
|
return result
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
@ -524,27 +501,27 @@ class QrCode:
|
|
|
|
"""Returns an ascending list of positions of alignment patterns for this version number.
|
|
|
|
"""Returns an ascending list of positions of alignment patterns for this version number.
|
|
|
|
Each position is in the range [0,177), and are used on both the x and y axes.
|
|
|
|
Each position is in the range [0,177), and are used on both the x and y axes.
|
|
|
|
This could be implemented as lookup table of 40 variable-length lists of integers."""
|
|
|
|
This could be implemented as lookup table of 40 variable-length lists of integers."""
|
|
|
|
ver = self._version
|
|
|
|
ver: int = self._version
|
|
|
|
if ver == 1:
|
|
|
|
if ver == 1:
|
|
|
|
return []
|
|
|
|
return []
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
numalign = ver // 7 + 2
|
|
|
|
numalign: int = ver // 7 + 2
|
|
|
|
step = 26 if (ver == 32) else \
|
|
|
|
step: int = 26 if (ver == 32) else \
|
|
|
|
(ver*4 + numalign*2 + 1) // (numalign*2 - 2) * 2
|
|
|
|
(ver*4 + numalign*2 + 1) // (numalign*2 - 2) * 2
|
|
|
|
result = [(self._size - 7 - i * step) for i in range(numalign - 1)] + [6]
|
|
|
|
result: List[int] = [(self._size - 7 - i * step) for i in range(numalign - 1)] + [6]
|
|
|
|
return list(reversed(result))
|
|
|
|
return list(reversed(result))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@staticmethod
|
|
|
|
def _get_num_raw_data_modules(ver) -> int:
|
|
|
|
def _get_num_raw_data_modules(ver: int) -> int:
|
|
|
|
"""Returns the number of data bits that can be stored in a QR Code of the given version number, after
|
|
|
|
"""Returns the number of data bits that can be stored in a QR Code of the given version number, after
|
|
|
|
all function modules are excluded. This includes remainder bits, so it might not be a multiple of 8.
|
|
|
|
all function modules are excluded. This includes remainder bits, so it might not be a multiple of 8.
|
|
|
|
The result is in the range [208, 29648]. This could be implemented as a 40-entry lookup table."""
|
|
|
|
The result is in the range [208, 29648]. This could be implemented as a 40-entry lookup table."""
|
|
|
|
if not (QrCode.MIN_VERSION <= ver <= QrCode.MAX_VERSION):
|
|
|
|
if not (QrCode.MIN_VERSION <= ver <= QrCode.MAX_VERSION):
|
|
|
|
raise ValueError("Version number out of range")
|
|
|
|
raise ValueError("Version number out of range")
|
|
|
|
result = (16 * ver + 128) * ver + 64
|
|
|
|
result: int = (16 * ver + 128) * ver + 64
|
|
|
|
if ver >= 2:
|
|
|
|
if ver >= 2:
|
|
|
|
numalign = ver // 7 + 2
|
|
|
|
numalign: int = ver // 7 + 2
|
|
|
|
result -= (25 * numalign - 10) * numalign - 55
|
|
|
|
result -= (25 * numalign - 10) * numalign - 55
|
|
|
|
if ver >= 7:
|
|
|
|
if ver >= 7:
|
|
|
|
result -= 36
|
|
|
|
result -= 36
|
|
|
@ -553,7 +530,7 @@ class QrCode:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@staticmethod
|
|
|
|
def _get_num_data_codewords(ver, ecl: QrCode.Ecc) -> int:
|
|
|
|
def _get_num_data_codewords(ver: int, ecl: QrCode.Ecc) -> int:
|
|
|
|
"""Returns the number of 8-bit data (i.e. not error correction) codewords contained in any
|
|
|
|
"""Returns the number of 8-bit data (i.e. not error correction) codewords contained in any
|
|
|
|
QR Code of the given version number and error correction level, with remainder bits discarded.
|
|
|
|
QR Code of the given version number and error correction level, with remainder bits discarded.
|
|
|
|
This stateless pure function could be implemented as a (40*4)-cell lookup table."""
|
|
|
|
This stateless pure function could be implemented as a (40*4)-cell lookup table."""
|
|
|
@ -563,19 +540,19 @@ class QrCode:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@staticmethod
|
|
|
|
def _reed_solomon_compute_divisor(degree: int) -> List[int]:
|
|
|
|
def _reed_solomon_compute_divisor(degree: int) -> bytes:
|
|
|
|
"""Returns a Reed-Solomon ECC generator polynomial for the given degree. This could be
|
|
|
|
"""Returns a Reed-Solomon ECC generator polynomial for the given degree. This could be
|
|
|
|
implemented as a lookup table over all possible parameter values, instead of as an algorithm."""
|
|
|
|
implemented as a lookup table over all possible parameter values, instead of as an algorithm."""
|
|
|
|
if not (1 <= degree <= 255):
|
|
|
|
if not (1 <= degree <= 255):
|
|
|
|
raise ValueError("Degree out of range")
|
|
|
|
raise ValueError("Degree out of range")
|
|
|
|
# Polynomial coefficients are stored from highest to lowest power, excluding the leading term which is always 1.
|
|
|
|
# Polynomial coefficients are stored from highest to lowest power, excluding the leading term which is always 1.
|
|
|
|
# For example the polynomial x^3 + 255x^2 + 8x + 93 is stored as the uint8 array [255, 8, 93].
|
|
|
|
# For example the polynomial x^3 + 255x^2 + 8x + 93 is stored as the uint8 array [255, 8, 93].
|
|
|
|
result = [0] * (degree - 1) + [1] # Start off with the monomial x^0
|
|
|
|
result = bytearray([0] * (degree - 1) + [1]) # Start off with the monomial x^0
|
|
|
|
|
|
|
|
|
|
|
|
# Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}),
|
|
|
|
# Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}),
|
|
|
|
# and drop the highest monomial term which is always 1x^degree.
|
|
|
|
# and drop the highest monomial term which is always 1x^degree.
|
|
|
|
# Note that r = 0x02, which is a generator element of this field GF(2^8/0x11D).
|
|
|
|
# Note that r = 0x02, which is a generator element of this field GF(2^8/0x11D).
|
|
|
|
root = 1
|
|
|
|
root: int = 1
|
|
|
|
for _ in range(degree): # Unused variable i
|
|
|
|
for _ in range(degree): # Unused variable i
|
|
|
|
# Multiply the current product by (x - r^i)
|
|
|
|
# Multiply the current product by (x - r^i)
|
|
|
|
for j in range(degree):
|
|
|
|
for j in range(degree):
|
|
|
@ -587,11 +564,11 @@ class QrCode:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@staticmethod
|
|
|
|
def _reed_solomon_compute_remainder(data: List[int], divisor: List[int]) -> List[int]:
|
|
|
|
def _reed_solomon_compute_remainder(data: bytes, divisor: bytes) -> bytes:
|
|
|
|
"""Returns the Reed-Solomon error correction codeword for the given data and divisor polynomials."""
|
|
|
|
"""Returns the Reed-Solomon error correction codeword for the given data and divisor polynomials."""
|
|
|
|
result = [0] * len(divisor)
|
|
|
|
result = bytearray([0] * len(divisor))
|
|
|
|
for b in data: # Polynomial division
|
|
|
|
for b in data: # Polynomial division
|
|
|
|
factor = b ^ result.pop(0)
|
|
|
|
factor: int = b ^ result.pop(0)
|
|
|
|
result.append(0)
|
|
|
|
result.append(0)
|
|
|
|
for (i, coef) in enumerate(divisor):
|
|
|
|
for (i, coef) in enumerate(divisor):
|
|
|
|
result[i] ^= QrCode._reed_solomon_multiply(coef, factor)
|
|
|
|
result[i] ^= QrCode._reed_solomon_multiply(coef, factor)
|
|
|
@ -602,10 +579,10 @@ class QrCode:
|
|
|
|
def _reed_solomon_multiply(x: int, y: int) -> int:
|
|
|
|
def _reed_solomon_multiply(x: int, y: int) -> int:
|
|
|
|
"""Returns the product of the two given field elements modulo GF(2^8/0x11D). The arguments and result
|
|
|
|
"""Returns the product of the two given field elements modulo GF(2^8/0x11D). The arguments and result
|
|
|
|
are unsigned 8-bit integers. This could be implemented as a lookup table of 256*256 entries of uint8."""
|
|
|
|
are unsigned 8-bit integers. This could be implemented as a lookup table of 256*256 entries of uint8."""
|
|
|
|
if x >> 8 != 0 or y >> 8 != 0:
|
|
|
|
if (x >> 8 != 0) or (y >> 8 != 0):
|
|
|
|
raise ValueError("Byte out of range")
|
|
|
|
raise ValueError("Byte out of range")
|
|
|
|
# Russian peasant multiplication
|
|
|
|
# Russian peasant multiplication
|
|
|
|
z = 0
|
|
|
|
z: int = 0
|
|
|
|
for i in reversed(range(8)):
|
|
|
|
for i in reversed(range(8)):
|
|
|
|
z = (z << 1) ^ ((z >> 7) * 0x11D)
|
|
|
|
z = (z << 1) ^ ((z >> 7) * 0x11D)
|
|
|
|
z ^= ((y >> i) & 1) * x
|
|
|
|
z ^= ((y >> i) & 1) * x
|
|
|
@ -616,9 +593,9 @@ class QrCode:
|
|
|
|
def _finder_penalty_count_patterns(self, runhistory: collections.deque) -> int:
|
|
|
|
def _finder_penalty_count_patterns(self, runhistory: collections.deque) -> int:
|
|
|
|
"""Can only be called immediately after a white run is added, and
|
|
|
|
"""Can only be called immediately after a white run is added, and
|
|
|
|
returns either 0, 1, or 2. A helper function for _get_penalty_score()."""
|
|
|
|
returns either 0, 1, or 2. A helper function for _get_penalty_score()."""
|
|
|
|
n = runhistory[1]
|
|
|
|
n: int = runhistory[1]
|
|
|
|
assert n <= self._size * 3
|
|
|
|
assert n <= self._size * 3
|
|
|
|
core = n > 0 and (runhistory[2] == runhistory[4] == runhistory[5] == n) and runhistory[3] == n * 3
|
|
|
|
core: bool = n > 0 and (runhistory[2] == runhistory[4] == runhistory[5] == n) and runhistory[3] == n * 3
|
|
|
|
return (1 if (core and runhistory[0] >= n * 4 and runhistory[6] >= n) else 0) \
|
|
|
|
return (1 if (core and runhistory[0] >= n * 4 and runhistory[6] >= n) else 0) \
|
|
|
|
+ (1 if (core and runhistory[6] >= n * 4 and runhistory[0] >= n) else 0)
|
|
|
|
+ (1 if (core and runhistory[6] >= n * 4 and runhistory[0] >= n) else 0)
|
|
|
|
|
|
|
|
|
|
|
@ -641,16 +618,16 @@ class QrCode:
|
|
|
|
|
|
|
|
|
|
|
|
# ---- Constants and tables ----
|
|
|
|
# ---- Constants and tables ----
|
|
|
|
|
|
|
|
|
|
|
|
MIN_VERSION = 1 # The minimum version number supported in the QR Code Model 2 standard
|
|
|
|
MIN_VERSION: int = 1 # The minimum version number supported in the QR Code Model 2 standard
|
|
|
|
MAX_VERSION = 40 # The maximum version number supported in the QR Code Model 2 standard
|
|
|
|
MAX_VERSION: int = 40 # The maximum version number supported in the QR Code Model 2 standard
|
|
|
|
|
|
|
|
|
|
|
|
# For use in getPenaltyScore(), when evaluating which mask is best.
|
|
|
|
# For use in _get_penalty_score(), when evaluating which mask is best.
|
|
|
|
_PENALTY_N1 = 3
|
|
|
|
_PENALTY_N1: int = 3
|
|
|
|
_PENALTY_N2 = 3
|
|
|
|
_PENALTY_N2: int = 3
|
|
|
|
_PENALTY_N3 = 40
|
|
|
|
_PENALTY_N3: int = 40
|
|
|
|
_PENALTY_N4 = 10
|
|
|
|
_PENALTY_N4: int = 10
|
|
|
|
|
|
|
|
|
|
|
|
_ECC_CODEWORDS_PER_BLOCK = (
|
|
|
|
_ECC_CODEWORDS_PER_BLOCK: Sequence[Sequence[int]] = (
|
|
|
|
# Version: (note that index 0 is for padding, and is set to an illegal value)
|
|
|
|
# Version: (note that index 0 is for padding, and is set to an illegal value)
|
|
|
|
# 0, 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 Error correction level
|
|
|
|
# 0, 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 Error correction level
|
|
|
|
(-1, 7, 10, 15, 20, 26, 18, 20, 24, 30, 18, 20, 24, 26, 30, 22, 24, 28, 30, 28, 28, 28, 28, 30, 30, 26, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30), # Low
|
|
|
|
(-1, 7, 10, 15, 20, 26, 18, 20, 24, 30, 18, 20, 24, 26, 30, 22, 24, 28, 30, 28, 28, 28, 28, 30, 30, 26, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30), # Low
|
|
|
@ -658,7 +635,7 @@ class QrCode:
|
|
|
|
(-1, 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30, 28, 30, 30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30), # Quartile
|
|
|
|
(-1, 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30, 28, 30, 30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30), # Quartile
|
|
|
|
(-1, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30)) # High
|
|
|
|
(-1, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30)) # High
|
|
|
|
|
|
|
|
|
|
|
|
_NUM_ERROR_CORRECTION_BLOCKS = (
|
|
|
|
_NUM_ERROR_CORRECTION_BLOCKS: Sequence[Sequence[int]] = (
|
|
|
|
# Version: (note that index 0 is for padding, and is set to an illegal value)
|
|
|
|
# Version: (note that index 0 is for padding, and is set to an illegal value)
|
|
|
|
# 0, 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 Error correction level
|
|
|
|
# 0, 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 Error correction level
|
|
|
|
(-1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 6, 7, 8, 8, 9, 9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25), # Low
|
|
|
|
(-1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 6, 7, 8, 8, 9, 9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25), # Low
|
|
|
@ -666,7 +643,7 @@ class QrCode:
|
|
|
|
(-1, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8, 8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68), # Quartile
|
|
|
|
(-1, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8, 8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68), # Quartile
|
|
|
|
(-1, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81)) # High
|
|
|
|
(-1, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81)) # High
|
|
|
|
|
|
|
|
|
|
|
|
_MASK_PATTERNS = (
|
|
|
|
_MASK_PATTERNS: Sequence[Callable[[int,int],int]] = (
|
|
|
|
(lambda x, y: (x + y) % 2 ),
|
|
|
|
(lambda x, y: (x + y) % 2 ),
|
|
|
|
(lambda x, y: y % 2 ),
|
|
|
|
(lambda x, y: y % 2 ),
|
|
|
|
(lambda x, y: x % 3 ),
|
|
|
|
(lambda x, y: x % 3 ),
|
|
|
@ -681,11 +658,14 @@ class QrCode:
|
|
|
|
# ---- Public helper enumeration ----
|
|
|
|
# ---- Public helper enumeration ----
|
|
|
|
|
|
|
|
|
|
|
|
class Ecc:
|
|
|
|
class Ecc:
|
|
|
|
|
|
|
|
ordinal: int # (Public) In the range 0 to 3 (unsigned 2-bit integer)
|
|
|
|
|
|
|
|
formatbits: int # (Package-private) In the range 0 to 3 (unsigned 2-bit integer)
|
|
|
|
|
|
|
|
|
|
|
|
"""The error correction level in a QR Code symbol. Immutable."""
|
|
|
|
"""The error correction level in a QR Code symbol. Immutable."""
|
|
|
|
# Private constructor
|
|
|
|
# Private constructor
|
|
|
|
def __init__(self, i: int, fb: int) -> None:
|
|
|
|
def __init__(self, i: int, fb: int) -> None:
|
|
|
|
self.ordinal = i # (Public) In the range 0 to 3 (unsigned 2-bit integer)
|
|
|
|
self.ordinal = i
|
|
|
|
self.formatbits = fb # (Package-private) In the range 0 to 3 (unsigned 2-bit integer)
|
|
|
|
self.formatbits = fb
|
|
|
|
|
|
|
|
|
|
|
|
# Placeholders
|
|
|
|
# Placeholders
|
|
|
|
LOW : QrCode.Ecc
|
|
|
|
LOW : QrCode.Ecc
|
|
|
@ -717,12 +697,10 @@ class QrSegment:
|
|
|
|
# ---- Static factory functions (mid level) ----
|
|
|
|
# ---- Static factory functions (mid level) ----
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@staticmethod
|
|
|
|
def make_bytes(data) -> QrSegment:
|
|
|
|
def make_bytes(data: Union[bytes,Sequence[int]]) -> QrSegment:
|
|
|
|
"""Returns a segment representing the given binary data encoded in byte mode.
|
|
|
|
"""Returns a segment representing the given binary data encoded in byte mode.
|
|
|
|
All input byte lists are acceptable. Any text string can be converted to
|
|
|
|
All input byte lists are acceptable. Any text string can be converted to
|
|
|
|
UTF-8 bytes (s.encode("UTF-8")) and encoded as a byte mode segment."""
|
|
|
|
UTF-8 bytes (s.encode("UTF-8")) and encoded as a byte mode segment."""
|
|
|
|
if isinstance(data, str):
|
|
|
|
|
|
|
|
raise TypeError("Byte string/list expected")
|
|
|
|
|
|
|
|
bb = _BitBuffer()
|
|
|
|
bb = _BitBuffer()
|
|
|
|
for b in data:
|
|
|
|
for b in data:
|
|
|
|
bb.append_bits(b, 8)
|
|
|
|
bb.append_bits(b, 8)
|
|
|
@ -735,9 +713,9 @@ class QrSegment:
|
|
|
|
if QrSegment.NUMERIC_REGEX.fullmatch(digits) is None:
|
|
|
|
if QrSegment.NUMERIC_REGEX.fullmatch(digits) is None:
|
|
|
|
raise ValueError("String contains non-numeric characters")
|
|
|
|
raise ValueError("String contains non-numeric characters")
|
|
|
|
bb = _BitBuffer()
|
|
|
|
bb = _BitBuffer()
|
|
|
|
i = 0
|
|
|
|
i: int = 0
|
|
|
|
while i < len(digits): # Consume up to 3 digits per iteration
|
|
|
|
while i < len(digits): # Consume up to 3 digits per iteration
|
|
|
|
n = min(len(digits) - i, 3)
|
|
|
|
n: int = min(len(digits) - i, 3)
|
|
|
|
bb.append_bits(int(digits[i : i + n]), n * 3 + 1)
|
|
|
|
bb.append_bits(int(digits[i : i + n]), n * 3 + 1)
|
|
|
|
i += n
|
|
|
|
i += n
|
|
|
|
return QrSegment(QrSegment.Mode.NUMERIC, len(digits), bb)
|
|
|
|
return QrSegment(QrSegment.Mode.NUMERIC, len(digits), bb)
|
|
|
@ -752,7 +730,7 @@ class QrSegment:
|
|
|
|
raise ValueError("String contains unencodable characters in alphanumeric mode")
|
|
|
|
raise ValueError("String contains unencodable characters in alphanumeric mode")
|
|
|
|
bb = _BitBuffer()
|
|
|
|
bb = _BitBuffer()
|
|
|
|
for i in range(0, len(text) - 1, 2): # Process groups of 2
|
|
|
|
for i in range(0, len(text) - 1, 2): # Process groups of 2
|
|
|
|
temp = QrSegment._ALPHANUMERIC_ENCODING_TABLE[text[i]] * 45
|
|
|
|
temp: int = QrSegment._ALPHANUMERIC_ENCODING_TABLE[text[i]] * 45
|
|
|
|
temp += QrSegment._ALPHANUMERIC_ENCODING_TABLE[text[i + 1]]
|
|
|
|
temp += QrSegment._ALPHANUMERIC_ENCODING_TABLE[text[i + 1]]
|
|
|
|
bb.append_bits(temp, 11)
|
|
|
|
bb.append_bits(temp, 11)
|
|
|
|
if len(text) % 2 > 0: # 1 character remaining
|
|
|
|
if len(text) % 2 > 0: # 1 character remaining
|
|
|
@ -798,9 +776,24 @@ class QrSegment:
|
|
|
|
return QrSegment(QrSegment.Mode.ECI, 0, bb)
|
|
|
|
return QrSegment(QrSegment.Mode.ECI, 0, bb)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---- Private fields ----
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# The mode indicator of this segment. Accessed through get_mode().
|
|
|
|
|
|
|
|
_mode: QrSegment.Mode
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# The length of this segment's unencoded data. Measured in characters for
|
|
|
|
|
|
|
|
# numeric/alphanumeric/kanji mode, bytes for byte mode, and 0 for ECI mode.
|
|
|
|
|
|
|
|
# Always zero or positive. Not the same as the data's bit length.
|
|
|
|
|
|
|
|
# Accessed through get_num_chars().
|
|
|
|
|
|
|
|
_numchars: int
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# The data bits of this segment. Accessed through get_data().
|
|
|
|
|
|
|
|
_bitdata: List[int]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---- Constructor (low level) ----
|
|
|
|
# ---- Constructor (low level) ----
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, mode: QrSegment.Mode, numch: int, bitdata: List[int]) -> None:
|
|
|
|
def __init__(self, mode: QrSegment.Mode, numch: int, bitdata: Sequence[int]) -> None:
|
|
|
|
"""Creates a new QR Code segment with the given attributes and data.
|
|
|
|
"""Creates a new QR Code segment with the given attributes and data.
|
|
|
|
The character count (numch) must agree with the mode and the bit buffer length,
|
|
|
|
The character count (numch) must agree with the mode and the bit buffer length,
|
|
|
|
but the constraint isn't checked. The given bit buffer is cloned and stored."""
|
|
|
|
but the constraint isn't checked. The given bit buffer is cloned and stored."""
|
|
|
@ -808,17 +801,8 @@ class QrSegment:
|
|
|
|
raise TypeError("QrSegment.Mode expected")
|
|
|
|
raise TypeError("QrSegment.Mode expected")
|
|
|
|
if numch < 0:
|
|
|
|
if numch < 0:
|
|
|
|
raise ValueError()
|
|
|
|
raise ValueError()
|
|
|
|
|
|
|
|
|
|
|
|
# The mode indicator of this segment. Accessed through get_mode().
|
|
|
|
|
|
|
|
self._mode = mode
|
|
|
|
self._mode = mode
|
|
|
|
|
|
|
|
|
|
|
|
# The length of this segment's unencoded data. Measured in characters for
|
|
|
|
|
|
|
|
# numeric/alphanumeric/kanji mode, bytes for byte mode, and 0 for ECI mode.
|
|
|
|
|
|
|
|
# Always zero or positive. Not the same as the data's bit length.
|
|
|
|
|
|
|
|
# Accessed through get_num_chars().
|
|
|
|
|
|
|
|
self._numchars = numch
|
|
|
|
self._numchars = numch
|
|
|
|
|
|
|
|
|
|
|
|
# The data bits of this segment. Accessed through get_data().
|
|
|
|
|
|
|
|
self._bitdata = list(bitdata) # Make defensive copy
|
|
|
|
self._bitdata = list(bitdata) # Make defensive copy
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -845,7 +829,7 @@ class QrSegment:
|
|
|
|
returns None if a segment has too many characters to fit its length field."""
|
|
|
|
returns None if a segment has too many characters to fit its length field."""
|
|
|
|
result = 0
|
|
|
|
result = 0
|
|
|
|
for seg in segs:
|
|
|
|
for seg in segs:
|
|
|
|
ccbits = seg.get_mode().num_char_count_bits(version)
|
|
|
|
ccbits: int = seg.get_mode().num_char_count_bits(version)
|
|
|
|
if seg.get_num_chars() >= (1 << ccbits):
|
|
|
|
if seg.get_num_chars() >= (1 << ccbits):
|
|
|
|
return None # The segment's length doesn't fit the field's bit width
|
|
|
|
return None # The segment's length doesn't fit the field's bit width
|
|
|
|
result += 4 + ccbits + len(seg._bitdata)
|
|
|
|
result += 4 + ccbits + len(seg._bitdata)
|
|
|
@ -857,17 +841,16 @@ class QrSegment:
|
|
|
|
# (Public) Describes precisely all strings that are encodable in numeric mode.
|
|
|
|
# (Public) Describes precisely all strings that are encodable in numeric mode.
|
|
|
|
# To test whether a string s is encodable: ok = NUMERIC_REGEX.fullmatch(s) is not None
|
|
|
|
# To test whether a string s is encodable: ok = NUMERIC_REGEX.fullmatch(s) is not None
|
|
|
|
# A string is encodable iff each character is in the range 0 to 9.
|
|
|
|
# A string is encodable iff each character is in the range 0 to 9.
|
|
|
|
NUMERIC_REGEX = re.compile(r"[0-9]*")
|
|
|
|
NUMERIC_REGEX: re.Pattern = re.compile(r"[0-9]*")
|
|
|
|
|
|
|
|
|
|
|
|
# (Public) Describes precisely all strings that are encodable in alphanumeric mode.
|
|
|
|
# (Public) Describes precisely all strings that are encodable in alphanumeric mode.
|
|
|
|
# To test whether a string s is encodable: ok = ALPHANUMERIC_REGEX.fullmatch(s) is not None
|
|
|
|
# To test whether a string s is encodable: ok = ALPHANUMERIC_REGEX.fullmatch(s) is not None
|
|
|
|
# A string is encodable iff each character is in the following set: 0 to 9, A to Z
|
|
|
|
# A string is encodable iff each character is in the following set: 0 to 9, A to Z
|
|
|
|
# (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon.
|
|
|
|
# (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon.
|
|
|
|
|
|
|
|
ALPHANUMERIC_REGEX: re.Pattern = re.compile(r"[A-Z0-9 $%*+./:-]*")
|
|
|
|
ALPHANUMERIC_REGEX = re.compile(r"[A-Z0-9 $%*+./:-]*")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# (Private) Dictionary of "0"->0, "A"->10, "$"->37, etc.
|
|
|
|
# (Private) Dictionary of "0"->0, "A"->10, "$"->37, etc.
|
|
|
|
_ALPHANUMERIC_ENCODING_TABLE = {ch: i for (i, ch) in enumerate("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:")}
|
|
|
|
_ALPHANUMERIC_ENCODING_TABLE: Dict[str,int] = {ch: i for (i, ch) in enumerate("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:")}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---- Public helper enumeration ----
|
|
|
|
# ---- Public helper enumeration ----
|
|
|
@ -875,10 +858,13 @@ class QrSegment:
|
|
|
|
class Mode:
|
|
|
|
class Mode:
|
|
|
|
"""Describes how a segment's data bits are interpreted. Immutable."""
|
|
|
|
"""Describes how a segment's data bits are interpreted. Immutable."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_modebits: int # The mode indicator bits, which is a uint4 value (range 0 to 15)
|
|
|
|
|
|
|
|
_charcounts: Tuple[int,int,int] # Number of character count bits for three different version ranges
|
|
|
|
|
|
|
|
|
|
|
|
# Private constructor
|
|
|
|
# Private constructor
|
|
|
|
def __init__(self, modebits: int, charcounts: Tuple[int,int,int]):
|
|
|
|
def __init__(self, modebits: int, charcounts: Tuple[int,int,int]):
|
|
|
|
self._modebits = modebits # The mode indicator bits, which is a uint4 value (range 0 to 15)
|
|
|
|
self._modebits = modebits
|
|
|
|
self._charcounts = charcounts # Number of character count bits for three different version ranges
|
|
|
|
self._charcounts = charcounts
|
|
|
|
|
|
|
|
|
|
|
|
# Package-private method
|
|
|
|
# Package-private method
|
|
|
|
def get_mode_bits(self) -> int:
|
|
|
|
def get_mode_bits(self) -> int:
|
|
|
@ -915,7 +901,7 @@ class _BitBuffer(list):
|
|
|
|
def append_bits(self, val: int, n: int) -> None:
|
|
|
|
def append_bits(self, val: int, n: int) -> None:
|
|
|
|
"""Appends the given number of low-order bits of the given
|
|
|
|
"""Appends the given number of low-order bits of the given
|
|
|
|
value to this buffer. Requires n >= 0 and 0 <= val < 2^n."""
|
|
|
|
value to this buffer. Requires n >= 0 and 0 <= val < 2^n."""
|
|
|
|
if n < 0 or val >> n != 0:
|
|
|
|
if (n < 0) or (val >> n != 0):
|
|
|
|
raise ValueError("Value out of range")
|
|
|
|
raise ValueError("Value out of range")
|
|
|
|
self.extend(((val >> i) & 1) for i in reversed(range(n)))
|
|
|
|
self.extend(((val >> i) & 1) for i in reversed(range(n)))
|
|
|
|
|
|
|
|
|
|
|
|