#!/usr/bin/env python3 """ Generate reference QR code using qrcode library and output binary matrix """ import qrcode # Generate QR code with same settings as our implementation qr = qrcode.QRCode( version=1, # Version 1 (21x21) error_correction=qrcode.constants.ERROR_CORRECT_M, # Level M box_size=1, border=0 # No border for matrix comparison ) qr.add_data("HELLO WORLD") qr.make(fit=False) # Don't auto-fit, use version 1 # Get the matrix matrix = qr.get_matrix() print("=== Reference QR Code (Python qrcode library) ===\n") print(f"Data: 'HELLO WORLD'") print(f"Version: 1 (21x21)") print(f"Error Correction: M") print(f"Matrix Size: {len(matrix)}x{len(matrix[0])}\n") # Output binary matrix print("=== Binary Matrix ===\n") for row in matrix: for cell in row: print('1' if cell else '0', end='') print() print("\n=== Critical Sections ===\n") # Top-Left Finder Pattern print("Top-Left Finder Pattern (0-6, 0-6):") for i in range(7): row_str = ''.join('1' if matrix[i][j] else '0' for j in range(7)) print(f"Row {i}: {row_str}") print() # Format Information print("Format Information:") # Horizontal (Row 8) format_cols = [0, 1, 2, 3, 4, 5, 7, 8, 20, 19, 18, 17, 16, 15, 14] format_h = ''.join('1' if matrix[8][col] else '0' for col in format_cols) print(f"Horizontal (Row 8): {format_h}") # Vertical (Col 8) format_rows = [20, 19, 18, 17, 16, 15, 14, 8, 7, 5, 4, 3, 2, 1, 0] format_v = ''.join('1' if matrix[row][8] else '0' for row in format_rows) print(f"Vertical (Col 8): {format_v}") print(f"Match: {'✅' if format_h == format_v else '❌'}") print() # Decode format information print("Format Info Decoding:") print(f"Raw: {format_h}") # Unmask with XOR mask xor_mask = "101010000010010" unmasked_bits = ''.join( str(int(format_h[i]) ^ int(xor_mask[i])) for i in range(15) ) print(f"Unmasked: {unmasked_bits}") ec_bits = unmasked_bits[0:2] mask_bits = unmasked_bits[2:5] ec_map = {'01': 'L', '00': 'M', '11': 'Q', '10': 'H'} print(f"EC Level: {ec_bits} = {ec_map.get(ec_bits, 'Unknown')}") print(f"Mask Pattern: {mask_bits} = Pattern {int(mask_bits, 2)}") print() # Save image for scanning img = qr.make_image(fill_color="black", back_color="white") img_path = "/home/michael/dev/michaelschiemer/public/qrcode-python-reference.png" img.save(img_path) print(f"✅ Reference image saved: {img_path}")