qrcodegen-worker.py
1 # 2 # QR Code generator test worker (Python) 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 29 import sys 30 import qrcodegen 31 32 33 def read_int() -> int: 34 return int(input()) 35 36 37 def main() -> None: 38 while True: 39 40 # Read data or exit 41 length = read_int() 42 if length == -1: 43 break 44 data = bytearray(read_int() for _ in range(length)) 45 46 # Read encoding parameters 47 errcorlvl = read_int() 48 minversion = read_int() 49 maxversion = read_int() 50 mask = read_int() 51 boostecl = read_int() 52 53 # Make segments for encoding 54 if all((b < 128) for b in data): # Is ASCII 55 segs = qrcodegen.QrSegment.make_segments(data.decode("ASCII")) 56 else: 57 segs = [qrcodegen.QrSegment.make_bytes(data)] 58 59 try: # Try to make QR Code symbol 60 qr = qrcodegen.QrCode.encode_segments(segs, ECC_LEVELS[errcorlvl], minversion, maxversion, mask, boostecl != 0) 61 # Print grid of modules 62 print(qr.get_version()) 63 for y in range(qr.get_size()): 64 for x in range(qr.get_size()): 65 print(1 if qr.get_module(x, y) else 0) 66 67 except qrcodegen.DataTooLongError: 68 print(-1) 69 sys.stdout.flush() 70 71 72 ECC_LEVELS = ( 73 qrcodegen.QrCode.Ecc.LOW, 74 qrcodegen.QrCode.Ecc.MEDIUM, 75 qrcodegen.QrCode.Ecc.QUARTILE, 76 qrcodegen.QrCode.Ecc.HIGH, 77 ) 78 79 80 if __name__ == "__main__": 81 main()