• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#
2# QR Code generator test worker (Python 2, 3)
3#
4# This program reads data and encoding parameters from standard input and writes
5# QR Code bitmaps to standard output. The I/O format is one integer per line.
6# Run with no command line arguments. The program is intended for automated
7# batch testing of end-to-end functionality of this QR Code generator library.
8#
9# Copyright (c) Project Nayuki. (MIT License)
10# https://www.nayuki.io/page/qr-code-generator-library
11#
12# Permission is hereby granted, free of charge, to any person obtaining a copy of
13# this software and associated documentation files (the "Software"), to deal in
14# the Software without restriction, including without limitation the rights to
15# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
16# the Software, and to permit persons to whom the Software is furnished to do so,
17# subject to the following conditions:
18# - The above copyright notice and this permission notice shall be included in
19#   all copies or substantial portions of the Software.
20# - The Software is provided "as is", without warranty of any kind, express or
21#   implied, including but not limited to the warranties of merchantability,
22#   fitness for a particular purpose and noninfringement. In no event shall the
23#   authors or copyright holders be liable for any claim, damages or other
24#   liability, whether in an action of contract, tort or otherwise, arising from,
25#   out of or in connection with the Software or the use or other dealings in the
26#   Software.
27#
28
29import sys
30import qrcodegen
31py3 = sys.version_info.major >= 3
32
33
34def read_int():
35	return int((input if py3 else raw_input)())
36
37
38def main():
39	while True:
40
41		# Read data or exit
42		length = read_int()
43		if length == -1:
44			break
45		data = bytearray(read_int() for _ in range(length))
46
47		# Read encoding parameters
48		errcorlvl  = read_int()
49		minversion = read_int()
50		maxversion = read_int()
51		mask       = read_int()
52		boostecl   = read_int()
53
54		# Make segments for encoding
55		if all((b < 128) for b in data):  # Is ASCII
56			segs = qrcodegen.QrSegment.make_segments(data.decode("ASCII"))
57		else:
58			segs = [qrcodegen.QrSegment.make_bytes(data)]
59
60		try:  # Try to make QR Code symbol
61			qr = qrcodegen.QrCode.encode_segments(segs, ECC_LEVELS[errcorlvl], minversion, maxversion, mask, boostecl != 0)
62			# Print grid of modules
63			print(qr.get_version())
64			for y in range(qr.get_size()):
65				for x in range(qr.get_size()):
66					print(1 if qr.get_module(x, y) else 0)
67
68		except qrcodegen.DataTooLongError:
69			print(-1)
70		sys.stdout.flush()
71
72
73ECC_LEVELS = (
74	qrcodegen.QrCode.Ecc.LOW,
75	qrcodegen.QrCode.Ecc.MEDIUM,
76	qrcodegen.QrCode.Ecc.QUARTILE,
77	qrcodegen.QrCode.Ecc.HIGH,
78)
79
80
81if __name__ == "__main__":
82	main()
83