Barcode

Barcode generation with LVGL. Uses code128 by fhunleth.

Usage

Enable LV_USE_BARCODE in lv_conf.h.

Use lv_barcode_create() to create a barcode object, and use lv_barcode_update() to generate a barcode.

Call lv_barcode_set_scale() or lv_barcode_set_dark/light_color() to adjust scaling and color, and call lv_barcode_update() again to regenerate the barcode.

Notes

  • It is best not to manually set the width of the barcode, because when the width of the object is lower than the width of the barcode, the display will be incomplete due to truncation.

  • The scale adjustment can only be an integer multiple, for example, lv_barcode_set_scale(barcode, 2) means 2x scaling.

Example

Create a Barcode

C code  

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

/**
 * Create a Barcode
 */
void lv_example_barcode_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 * barcode = lv_barcode_create(lv_scr_act());
    lv_obj_set_height(barcode, 50);
    lv_obj_center(barcode);

    /*Set color*/
    lv_barcode_set_dark_color(barcode, fg_color);
    lv_barcode_set_light_color(barcode, bg_color);

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

    /*Set data*/
    lv_barcode_update(barcode, "https://lvgl.io");
}

#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)

barcode = lv.barcode(lv.scr_act())
barcode.set_height(50)
barcode.center()

# Set color
barcode.set_dark_color(fg_color)
barcode.set_light_color(bg_color)

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

# Set data
barcode.update("https://lvgl.io")

API

Functions

lv_obj_t *lv_barcode_create(lv_obj_t *parent)

Create an empty barcode (an lv_canvas) object.

Parameters

parent -- point to an object where to create the barcode

Returns

pointer to the created barcode object

void lv_barcode_set_dark_color(lv_obj_t *obj, lv_color_t color)

Set the dark color of a barcode object

Parameters
  • obj -- pointer to barcode object

  • color -- dark color of the barcode

void lv_barcode_set_light_color(lv_obj_t *obj, lv_color_t color)

Set the light color of a barcode object

Parameters
  • obj -- pointer to barcode object

  • color -- light color of the barcode

void lv_barcode_set_scale(lv_obj_t *obj, uint16_t scale)

Set the scale of a barcode object

Parameters
  • obj -- pointer to barcode object

  • scale -- scale factor

lv_res_t lv_barcode_update(lv_obj_t *obj, const char *data)

Set the data of a barcode object

Parameters
  • obj -- pointer to barcode object

  • data -- data to display

Returns

LV_RES_OK: if no error; LV_RES_INV: on error

lv_color_t lv_barcode_get_dark_color(lv_obj_t *obj)

Get the dark color of a barcode object

Parameters

obj -- pointer to barcode object

Returns

dark color of the barcode

lv_color_t lv_barcode_get_light_color(lv_obj_t *obj)

Get the light color of a barcode object

Parameters

obj -- pointer to barcode object

Returns

light color of the barcode

uint16_t lv_barcode_get_scale(lv_obj_t *obj)

Get the scale of a barcode object

Parameters

obj -- pointer to barcode object

Returns

scale factor

Variables

const lv_obj_class_t lv_barcode_class
struct lv_barcode_t

Public Members

lv_canvas_t canvas
lv_color_t dark_color
lv_color_t light_color
uint16_t scale