You've already forked MicroPythonOS
mirror of
https://github.com/m5stack/MicroPythonOS.git
synced 2026-05-20 11:51:27 -07:00
QR decoding works on unix desktop target
This commit is contained in:
@@ -8,6 +8,11 @@ set(MPOS_C_INCLUDES)
|
||||
|
||||
set(MPOS_C_SOURCES
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/hello_world.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/quirc_decode.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/quirc/lib/identify.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/quirc/lib/version_db.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/quirc/lib/decode.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/quirc/lib/quirc.c
|
||||
)
|
||||
|
||||
# Add our source files to the lib
|
||||
|
||||
@@ -10,10 +10,11 @@ ifneq (,$(findstring -Wno-missing-field-initializers, $(CFLAGS_USERMOD)))
|
||||
endif
|
||||
|
||||
SRC_USERMOD_C += $(MOD_DIR)/src/hello_world.c
|
||||
SRC_USERMOD_C += $(MOD_DIR)/src/quirc_decode.c
|
||||
|
||||
SRC_USERMOD_C += $(MOD_DIR)/esp32-quirc/lib/identify.c
|
||||
SRC_USERMOD_C += $(MOD_DIR)/esp32-quirc/lib/version_db.c
|
||||
SRC_USERMOD_C += $(MOD_DIR)/esp32-quirc/lib/decode.c
|
||||
SRC_USERMOD_C += $(MOD_DIR)/esp32-quirc/lib/quirc.c
|
||||
SRC_USERMOD_C += $(MOD_DIR)/esp32-quirc/openmv/collections.c
|
||||
SRC_USERMOD_C += $(MOD_DIR)/quirc/lib/identify.c
|
||||
SRC_USERMOD_C += $(MOD_DIR)/quirc/lib/version_db.c
|
||||
SRC_USERMOD_C += $(MOD_DIR)/quirc/lib/decode.c
|
||||
SRC_USERMOD_C += $(MOD_DIR)/quirc/lib/quirc.c
|
||||
#SRC_USERMOD_C += $(MOD_DIR)/quirc/openmv/collections.c
|
||||
|
||||
|
||||
Submodule
+1
Submodule c_mpos/quirc added at a9c52e7928
@@ -1,18 +1,12 @@
|
||||
#include "py/obj.h"
|
||||
#include "py/runtime.h"
|
||||
|
||||
#include "../esp32-quirc/lib/quirc.h"
|
||||
|
||||
//#error "building hello world from lcd_utils"
|
||||
|
||||
// C function to print "Hello World"
|
||||
static mp_obj_t hello_world(void) {
|
||||
printf("Hello World from C with quirc!\n");
|
||||
|
||||
struct quirc *qr;
|
||||
qr = quirc_new();
|
||||
quirc_destroy(qr);
|
||||
|
||||
printf("Hello World from C!\n");
|
||||
return mp_const_none; // MicroPython functions typically return None
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
#include "py/obj.h"
|
||||
#include "py/runtime.h"
|
||||
#include "py/mperrno.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "../quirc/lib/quirc.h"
|
||||
|
||||
// Function to decode a QR code from a grayscale image buffer
|
||||
static mp_obj_t qrdecode(mp_uint_t n_args, const mp_obj_t *args) {
|
||||
printf("qrdecode running\n")
|
||||
// Check argument count (expecting buffer, width, height)
|
||||
if (n_args != 3) {
|
||||
mp_raise_ValueError(MP_ERROR_TEXT("quirc_decode expects 3 arguments: buffer, width, height"));
|
||||
}
|
||||
|
||||
// Extract buffer
|
||||
mp_buffer_info_t bufinfo;
|
||||
mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
|
||||
|
||||
// Extract width and height
|
||||
mp_int_t width = mp_obj_get_int(args[1]);
|
||||
mp_int_t height = mp_obj_get_int(args[2]);
|
||||
|
||||
// Validate dimensions
|
||||
if (width <= 0 || height <= 0) {
|
||||
mp_raise_ValueError(MP_ERROR_TEXT("width and height must be positive"));
|
||||
}
|
||||
if (bufinfo.len != (size_t)(width * height)) {
|
||||
mp_raise_ValueError(MP_ERROR_TEXT("buffer size must match width * height"));
|
||||
}
|
||||
|
||||
// Initialize quirc
|
||||
struct quirc *qr = quirc_new();
|
||||
if (!qr) {
|
||||
mp_raise_OSError(MP_ENOMEM);
|
||||
}
|
||||
|
||||
// Resize quirc for the image dimensions
|
||||
if (quirc_resize(qr, width, height) < 0) {
|
||||
quirc_destroy(qr);
|
||||
mp_raise_OSError(MP_ENOMEM);
|
||||
}
|
||||
|
||||
// Get quirc image buffer and copy grayscale data
|
||||
uint8_t *image;
|
||||
quirc_begin(qr, NULL, NULL);
|
||||
image = quirc_begin(qr, NULL, NULL); // Get pointer to quirc's image buffer
|
||||
memcpy(image, bufinfo.buf, width * height); // Copy buffer directly (grayscale, 8-bit)
|
||||
quirc_end(qr);
|
||||
|
||||
// Check for QR codes
|
||||
int count = quirc_count(qr);
|
||||
if (count == 0) {
|
||||
quirc_destroy(qr);
|
||||
mp_raise_ValueError(MP_ERROR_TEXT("no QR code found"));
|
||||
}
|
||||
|
||||
// Extract and decode the first QR code
|
||||
struct quirc_code code;
|
||||
struct quirc_data data;
|
||||
quirc_extract(qr, 0, &code); // Extract first QR code
|
||||
|
||||
// Decode the QR code
|
||||
int err = quirc_decode(&code, &data);
|
||||
if (err != QUIRC_SUCCESS) {
|
||||
quirc_destroy(qr);
|
||||
mp_raise_ValueError(MP_ERROR_TEXT("failed to decode QR code"));
|
||||
}
|
||||
|
||||
// Convert decoded data to Python string
|
||||
mp_obj_t result = mp_obj_new_str((const char *)data.payload, data.payload_len);
|
||||
|
||||
// Clean up
|
||||
quirc_destroy(qr);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Define the MicroPython function
|
||||
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(qrdecode_obj, 3, 3, qrdecode);
|
||||
|
||||
// Module definition
|
||||
static const mp_rom_map_elem_t qrdecode_module_globals_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_qrdecode) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_qrdecode), MP_ROM_PTR(&qrdecode_obj) },
|
||||
};
|
||||
|
||||
static MP_DEFINE_CONST_DICT(qrdecode_module_globals, qrdecode_module_globals_table);
|
||||
|
||||
const mp_obj_module_t qrdecode_module = {
|
||||
.base = { &mp_type_module },
|
||||
.globals = (mp_obj_dict_t *)&qrdecode_module_globals,
|
||||
};
|
||||
|
||||
// Register the module
|
||||
MP_REGISTER_MODULE(MP_QSTR_qrdecode, qrdecode_module);
|
||||
@@ -0,0 +1,29 @@
|
||||
import qrdecode
|
||||
|
||||
|
||||
# Image dimensions
|
||||
width = 240
|
||||
height = 240
|
||||
buffer_size = width * height # 240 * 240 = 57600 bytes
|
||||
try:
|
||||
# Allocate buffer for grayscale image
|
||||
buffer = bytearray(buffer_size)
|
||||
# Read the raw grayscale image file
|
||||
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")
|
||||
# Decode QR code using qrdecode module
|
||||
print("decoding...")
|
||||
result = qrdecode.qrdecode(buffer, width, height)
|
||||
print(f"result: {result}")
|
||||
# Remove BOM (\ufeff) from the start of the decoded string, if present
|
||||
if result.startswith('\ufeff'):
|
||||
result = result[1:]
|
||||
print(f"result: {result}")
|
||||
except OSError as e:
|
||||
print("Error reading file:", e)
|
||||
raise
|
||||
except ValueError as e:
|
||||
print("Error:", e)
|
||||
raise
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user