PyQRCode Builder Documentation

This module does the actual generation of the QR codes. The QRCodeBuilder builds the code. While the various output methods draw the code into a file.

class pyqrcode.builder.QRCodeBuilder(data, version, mode, error)[source]

This class generates a QR code based on the standard. It is meant to be used internally, not by users!!!

This class implements the tutorials found at:

This class also uses the standard, which can be read online at:
http://raidenii.net/files/datasheets/misc/qr_code.pdf
Test codes were tested against:
http://zxing.org/w/decode.jspx
Also, reference codes were generat/ed at:
http://www.morovia.com/free-online-barcode-generator/qrcode-maker.php http://demos.telerik.com/aspnet-ajax/barcode/examples/qrcode/defaultcs.aspx
QR code Debugger:
http://qrlogo.kaarposoft.dk/qrdecode.html
__init__(data, version, mode, error)[source]

See pyqrcode.QRCode for information on the parameters.

add_data()[source]

This function properly constructs a QR code’s data string. It takes into account the interleaving pattern required by the standard.

add_detection_pattern(m)[source]

This method add the detection patterns to the QR code. This lets the scanner orient the pattern. It is required for all QR codes. The detection pattern consists of three boxes located at the upper left, upper right, and lower left corners of the matrix. Also, two special lines called the timing pattern is also necessary. Finally, a single black pixel is added just above the lower left black box.

add_position_pattern(m)[source]

This method draws the position adjustment patterns onto the QR Code. All QR code versions larger than one require these special boxes called position adjustment patterns.

add_type_pattern(m, type_bits)[source]

This will add the pattern to the QR code that represents the error level and the type of mask used to make the code.

add_version_pattern(m)[source]

For QR codes with a version 7 or higher, a special pattern specifying the code’s version is required.

For further information see: http://www.thonky.com/qr-code-tutorial/format-version-information/#example-of-version-7-information-string

add_words()[source]

The data block must fill the entire data capacity of the QR code. If we fall short, then we must add bytes to the end of the encoded data field. The value of these bytes are specified in the standard.

binary_string(data, length)[source]

This method returns a string of length n that is the binary representation of the given data. This function is used to basically create bit fields of a given size.

choose_best_mask()[source]

This method returns the index of the “best” mask as defined by having the lowest total penalty score. The penalty rules are defined by the standard. The mask with the lowest total score should be the easiest to read by optical scanners.

delimit_words()[source]

This method takes the existing encoded binary string and returns a binary string that will pad it such that the encoded string contains only full bytes.

encode()[source]

This method encodes the data into a binary string using the appropriate algorithm specified by the mode.

encode_alphanumeric()[source]

This method encodes the QR code’s data if its mode is alphanumeric. It returns the data encoded as a binary string.

encode_bytes()[source]

This method encodes the QR code’s data if its mode is 8 bit mode. It returns the data encoded as a binary string.

encode_kanji()[source]

This method encodes the QR code’s data if its mode is kanji. It returns the data encoded as a binary string.

encode_numeric()[source]

This method encodes the QR code’s data if its mode is numeric. It returns the data encoded as a binary string.

get_data_length()[source]

QR codes contain a “data length” field. This method creates this field. A binary string representing the appropriate length is returned.

grouper(n, iterable, fillvalue=None)[source]

This generator yields a set of tuples, where the iterable is broken into n sized chunks. If the iterable is not evenly sized then fillvalue will be appended to the last tuple to make up the difference.

This function is copied from the standard docs on itertools.

make_code()[source]

This method returns the best possible QR code.

make_error_block(block, block_number)[source]

This function constructs the error correction block of the given data block. This is very complicated process. To understand the code you need to read:

make_masks(template)[source]

This method generates all seven masks so that the best mask can be determined. The template parameter is a code matrix that will server as the base for all the generated masks.

terminate_bits(payload)[source]

This method adds zeros to the end of the encoded data so that the encoded data is of the correct length. It returns a binary string containing the bits to be added.