二维码生成文件资源下载
链接:qrgen下载地址
提取码:6n8x
将下载好的二维码生成文件加入到工程
加入路径到C/C++附加包含目录
使用canvas实现二维码绘制
void draw_qrcode(uint16_t x1,uint16_t y1,uint8_t *src)
{
#define CANVAS_WIDTH 390
#define CANVAS_HEIGHT 390
EncodeData(src);
static lv_color_t* cbuf;
cbuf = lv_mem_alloc(LV_CANVAS_BUF_SIZE_TRUE_COLOR(CANVAS_WIDTH, CANVAS_WIDTH));
lv_obj_t* canvas = lv_canvas_create(lv_scr_act(), NULL);
lv_obj_set_pos(canvas, 0, 0);
lv_obj_set_size(canvas, CANVAS_WIDTH, CANVAS_WIDTH);
lv_canvas_set_buffer(canvas, cbuf, CANVAS_WIDTH, CANVAS_WIDTH, LV_IMG_CF_TRUE_COLOR);
lv_canvas_fill_bg(canvas, LV_COLOR_WHITE, LV_OPA_COVER);
lv_draw_rect_dsc_t dsc;
lv_draw_rect_dsc_init(&dsc);
dsc.bg_color = LV_COLOR_BLACK;
dsc.bg_opa = LV_OPA_COVER;
uint16_t size_num = 360 / MAX_MODULESIZE;
/*Create hole on the canvas*/
uint32_t x;
uint32_t y;
for (y = 0; y < MAX_MODULESIZE; y++)
{
for (x = 0; x < MAX_MODULESIZE; x++) {
if (!m_byModuleData[y][x])
{
dsc.bg_color = LV_COLOR_WHITE;
}
else
{
dsc.bg_color = LV_COLOR_BLACK;
}
lv_canvas_draw_rect(canvas, x1+x * size_num, y1+y * size_num, size_num, size_num, &dsc);
}
}
lv_obj_set_event_cb(canvas, canvas_release_evt);
lv_obj_set_user_data(canvas, cbuf);
}
- 使用了user数据,在lv_conf.h中需要将LV_USE_USER_DATA宏配置为1
- 需要释放资源,在canvas被删除后会产生一个LV_EVENT_DELETE事件,在此事件中释放分配的内存
static void canvas_release_evt(lv_obj_t* obj, lv_event_t event)
{
if (event == LV_EVENT_DELETE)
{
lv_color_t* cbuf = lv_obj_get_user_data(obj);
lv_mem_free(cbuf);
}
}