How to used LV_IMG_CF_INDEXED_1BIT to draw img

Description

I am using CANVS to draw QR code. When I set the canvas color format to LV_IMG_CF_TRUE_COLOR, everything was fine, but the memory requirement was up to 100K. My MCU could not meet this requirement, so I tried to change the color format to LV_IMG_CF_indexed_1bit, and then the program could not work properly.

What MCU/Processor/Board and compiler are you using?

PC SIM and STM32L475VG

What LVGL version are you using?

v7.9.0

What do you want to achieve?

What have you tried so far?

Code to reproduce

#define CANVAS_WIDTH  232
#define CANVAS_HEIGHT  232
uint8_t qr_code_data[] = { 0xfe, 0x4c, 0xcb, 0xfc, 0x14, 0xf9, 0x50, 0x6e, 0xbf, 0x28, 0xbb, 0x74, 0xee, 0xe5, 0xdb, 0xa8,
0x6b, 0x2e, 0xc1, 0x6b, 0x05, 0x07, 0xfa, 0xaa, 0xaf, 0xe0, 0x19, 0x4f, 0x00, 0xd3, 0x21, 0x7b,
0xb2, 0x2d, 0x99, 0x89, 0xeb, 0xd8, 0x35, 0xfc, 0xe2, 0x06, 0xbc, 0x6c, 0x60, 0x88, 0xc9, 0x12,
0x5c, 0xa7, 0x7a, 0xce, 0x27, 0xcd, 0xea, 0xb9, 0xdd, 0x18, 0x46, 0x97, 0xa0, 0xd0, 0xad, 0x0b,
0xc0, 0xe2, 0xc9, 0x11, 0x2e, 0x61, 0xeb, 0x08, 0xbb, 0x35, 0xac, 0xfe, 0x00, 0x4e, 0xec, 0x4f,
0xfa, 0xfc, 0xea, 0x50, 0x4b, 0x95, 0x17, 0xba, 0x13, 0x3f, 0x85, 0xd4, 0xbd, 0x4f, 0xee, 0x9d,
0x86, 0xbb, 0x05, 0x22, 0x0a, 0x2f, 0xea, 0xc1, 0xd5, 0x00};
void lv_ex_canvas_test(void)
{
    lv_draw_rect_dsc_t rect_dsc1;
    lv_draw_rect_dsc_init(&rect_dsc1);
   
    rect_dsc1.bg_grad_dir = LV_GRAD_DIR_HOR;
    rect_dsc1.bg_color = LV_COLOR_BLACK;
    rect_dsc1.bg_grad_color = LV_COLOR_BLACK;
   
    lv_draw_rect_dsc_t rect_dsc0;
    lv_draw_rect_dsc_init(&rect_dsc0);

    rect_dsc0.bg_grad_dir = LV_GRAD_DIR_HOR;
    rect_dsc0.bg_color = LV_COLOR_WHITE;
    rect_dsc0.bg_grad_color = LV_COLOR_WHITE;
  

    static lv_color_t cbuf[LV_CANVAS_BUF_SIZE_TRUE_COLOR(CANVAS_WIDTH, CANVAS_HEIGHT)];

// static lv_color_t cbuf[LV_CANVAS_BUF_SIZE_TRUE_COLOR(CANVAS_WIDTH, CANVAS_HEIGHT)];
    static lv_color_t cbuf[LV_CANVAS_BUF_SIZE_INDEXED_1BIT(CANVAS_WIDTH, CANVAS_HEIGHT)];
    lv_obj_t* canvas = lv_canvas_create(lv_scr_act(), NULL);
    //lv_canvas_set_buffer(canvas, cbuf, CANVAS_WIDTH, CANVAS_HEIGHT, LV_IMG_CF_TRUE_COLOR);
    lv_canvas_set_buffer(canvas, cbuf, CANVAS_WIDTH, CANVAS_HEIGHT, LV_IMG_CF_INDEXED_1BIT);
    lv_obj_align(canvas, NULL, LV_ALIGN_CENTER, 0, 0);
    //lv_canvas_fill_bg(canvas, LV_COLOR_WHITE, LV_OPA_COVER);



    uint16_t count = 0;
    uint8_t temp_data = 0;
    uint8_t x=0, y=0;
    printf("\n");
    for (uint8_t j = 0; j < 106; j++)
    {
        temp_data = qr_code_data[j];
        for (uint8_t i = 0; i < 8; i++)
        {
            if (temp_data & 0x80)
            {
                //printf("█");
                lv_canvas_draw_rect(canvas, x, y, x + 8, y + 8, &rect_dsc1);
            }
            else
            {
                //printf("  ");
                lv_canvas_draw_rect(canvas, x, y, x + 8, y + 8, &rect_dsc0);
            }
            
        
            x += 8;
            count++;
            if ((count) % 29 == 0)
            {
                printf("\n");
                y += 8;
                x = 0;
            }
            temp_data <<= 1;
        }
    }
}

Screenshot and/or video

normal
image
The fault

The warning message is telling you that lv_canvas_draw_xxx functions do not work on indexed canvases. (The spelling is wrong and has been corrected here.)

Yes, I checked the source code for lv_canvas_draw_rect ().

This function does not support LV_IMG_CF_INDEXED_1BIT, which means that Canvas does not support LV_IMG_CF_INDEXED_1BIT drawing. Therefore, there seems to be no better way to implement QR code on small capacity MCU at present. Do you have any plans to add QR code widget functionality in future versions?

There is a QR code widget here.

thanks,i had clon it yet,it’s very cool!