PNG 解码器

允许在 LVGL 中使用 PNG 图像。此实现使用 lodepng 库。

如果在 lv_conf.h 中通过 LV_USE_PNG 启用,LVGL 将自动注册一个新的图像解码器,因此 PNG 文件可以像其他图像源一样直接使用。

注意,需要注册一个文件系统驱动程序才能从文件中打开图像。可以在此处阅读更多内容,或者只需在 lv_conf.h 中启用一个文件系统驱动程序,例如 LV_USE_FS_...

整个 PNG 图像会被解码,因此在解码期间需要的 RAM 等于 图像宽度 x 图像高度 x 4 字节。

由于解码 PNG 图像可能需要较长时间,LVGL 的图像缓存功能可能会很有用。

示例

从文件和变量中打开 PNG 图像

C code  

 GitHub
#include "../../lv_examples.h"
#if LV_USE_PNG && LV_USE_IMG && LV_BUILD_EXAMPLES

/**
 * Open a PNG image from a file and a variable
 */
void lv_example_png_1(void)
{
    LV_IMG_DECLARE(img_wink_png);
    lv_obj_t * img;

    img = lv_img_create(lv_scr_act());
    lv_img_set_src(img, &img_wink_png);
    lv_obj_align(img, LV_ALIGN_LEFT_MID, 20, 0);

    img = lv_img_create(lv_scr_act());
    /* Assuming a File system is attached to letter 'A'
     * E.g. set LV_USE_FS_STDIO 'A' in lv_conf.h */
    lv_img_set_src(img, "A:lvgl/examples/libs/png/wink.png");
    lv_obj_align(img, LV_ALIGN_RIGHT_MID, -20, 0);
}

#endif

MicroPython code  

 GitHub Simulator
#!/opt/bin/lv_micropython -i
import lvgl as lv
import display_driver
from imagetools import get_png_info, open_png
from img_wink_png import img_wink_png_map
# Register PNG image decoder
decoder = lv.img.decoder_create()
decoder.info_cb = get_png_info
decoder.open_cb = open_png

img_wink_png = lv.img_dsc_t(
    {
        "header": {"always_zero": 0, "w": 50, "h": 50,  "cf": lv.img.CF.RAW_ALPHA},
        "data_size": 5158,
        "data": img_wink_png_map,
    }
)
img1 = lv.img(lv.scr_act())
img1.set_src(img_wink_png)
img1.align(lv.ALIGN.RIGHT_MID, -250, 0)

# Create an image from the png file
try:
    with open('wink.png','rb') as f:
        png_data = f.read()
except:
    print("Could not find wink.png")
    sys.exit()

wink_argb = lv.img_dsc_t({
  'data_size': len(png_data),
  'data': png_data
})

img2 = lv.img(lv.scr_act())
img2.set_src(wink_argb)
img2.align(lv.ALIGN.RIGHT_MID, -150, 0)

API

Functions

void lv_png_init(void)

Register the PNG decoder functions in LVGL