QR code

QR code generation with LVGL. Uses QR-Code-generator by nayuki.

Usage

Enable LV_USE_QRCODE in lv_conf.h.

Use lv_qrcode_create() to create a qrcode object, and use lv_qrcode_update() to generate a QR code.

If you need to re-modify the size and color, use lv_qrcode_set_size() and lv_qrcode_set_dark/light_color(), and call lv_qrcode_update() again to regenerate the QR code.

Notes

  • QR codes with less data are smaller, but they scaled by an integer number to best fit to the given size.

Example

Create a QR Code

C code  

 GitHub
#include "../../lv_examples.h"
#if LV_USE_QRCODE && LV_BUILD_EXAMPLES

/**
 * Create a QR Code
 */
void lv_example_qrcode_1(void)
{
    lv_color_t bg_color = lv_palette_lighten(LV_PALETTE_LIGHT_BLUE, 5);
    lv_color_t fg_color = lv_palette_darken(LV_PALETTE_BLUE, 4);

    lv_obj_t * qr = lv_qrcode_create(lv_scr_act());
    lv_qrcode_set_size(qr, 150);
    lv_qrcode_set_dark_color(qr, fg_color);
    lv_qrcode_set_light_color(qr, bg_color);

    /*Set data*/
    const char * data = "https://lvgl.io";
    lv_qrcode_update(qr, data, strlen(data));
    lv_obj_center(qr);

    /*Add a border with bg_color*/
    lv_obj_set_style_border_color(qr, bg_color, 0);
    lv_obj_set_style_border_width(qr, 5, 0);
}

#endif

MicroPython code  

 GitHub Simulator
#!/opt/bin/lv_micropython -i
import lvgl as lv
import display_driver

bg_color = lv.palette_lighten(lv.PALETTE.LIGHT_BLUE, 5)
fg_color = lv.palette_darken(lv.PALETTE.BLUE, 4)

qr = lv.qrcode(lv.scr_act())
qr.set_size(150)
qr.set_dark_color(fg_color)
qr.set_light_color(bg_color)

# Set data
data = "https://lvgl.io"
qr.update(data,len(data))
qr.center()

# Add a border with bg_color
qr.set_style_border_color(bg_color, 0)
qr.set_style_border_width(5, 0)

API

Functions

lv_obj_t *lv_qrcode_create(lv_obj_t *parent)

Create an empty QR code (an lv_canvas) object.

Parameters

parent -- point to an object where to create the QR code

Returns

pointer to the created QR code object

void lv_qrcode_set_size(lv_obj_t *obj, lv_coord_t size)

Set QR code size.

Parameters
  • obj -- pointer to a QR code object

  • size -- width and height of the QR code

void lv_qrcode_set_dark_color(lv_obj_t *obj, lv_color_t color)

Set QR code dark color.

Parameters
  • obj -- pointer to a QR code object

  • color -- dark color of the QR code

void lv_qrcode_set_light_color(lv_obj_t *obj, lv_color_t color)

Set QR code light color.

Parameters
  • obj -- pointer to a QR code object

  • color -- light color of the QR code

lv_res_t lv_qrcode_update(lv_obj_t *obj, const void *data, uint32_t data_len)

Set the data of a QR code object

Parameters
  • obj -- pointer to a QR code object

  • data -- data to display

  • data_len -- length of data in bytes

Returns

LV_RES_OK: if no error; LV_RES_INV: on error

Variables

const lv_obj_class_t lv_qrcode_class
struct lv_qrcode_t

Public Members

lv_canvas_t canvas
lv_color_t dark_color
lv_color_t light_color