Files
MicroPythonOS/draft_code/qrdecode.py
T

40 lines
1.1 KiB
Python
Raw Normal View History

2025-05-12 17:18:24 +02:00
import qrdecode
2025-05-12 21:37:22 +02:00
import random
2025-05-12 17:18:24 +02:00
2025-05-12 21:37:22 +02:00
# Create a 240x240 byte buffer
buffer = bytearray(240 * 240)
# Fill buffer with random bytes
2025-05-12 17:18:24 +02:00
# Image dimensions
width = 240
height = 240
buffer_size = width * height # 240 * 240 = 57600 bytes
try:
# Allocate buffer for grayscale image
buffer = bytearray(buffer_size)
2025-05-12 22:10:15 +02:00
#for i in range(240 * 240):
# buffer[i] = random.getrandbits(8)
2025-05-12 17:18:24 +02:00
# Read the raw grayscale image file
2025-05-12 22:10:15 +02:00
with open('qrcode2.raw', 'rb') as f:
bytes_read = f.readinto(buffer)
if bytes_read != buffer_size:
raise ValueError("File size does not match expected 240x240 grayscale image")
2025-05-12 17:18:24 +02:00
# Decode QR code using qrdecode module
print("decoding...")
2025-05-12 21:01:16 +02:00
print(f"buffer length: {len(buffer)}")
for i in range(15):
print(buffer[i])
2025-05-12 17:18:24 +02:00
result = qrdecode.qrdecode(buffer, width, height)
print(f"result: {result}")
# Remove BOM (\ufeff) from the start of the decoded string, if present
2025-05-12 22:10:15 +02:00
#if result.startswith('\ufeff'):
# result = result[1:]
2025-05-12 17:18:24 +02:00
print(f"result: {result}")
except OSError as e:
print("Error reading file:", e)
raise
except ValueError as e:
print("Error:", e)
raise