1. PNG图片显示
使用教程参考官方文档
1、先打开文件系统的支持
*API for open, read, etc*/
#define LV_USE_FS_POSIX 1
#if LV_USE_FS_POSIX
#define LV_FS_POSIX_LETTER 'A' /*Set an upper cased letter on which the drive will accessible (e.g. 'A')*/
#define LV_FS_POSIX_PATH "A:/home/xj/asm/data/" /*Set the working directory. File/directory paths will be appended to it.*/
#define LV_FS_POSIX_CACHE_SIZE 0 /*>0 to cache this number of bytes in lv_fs_read()*/
#endif
2、再打开相对应的图片格式支持
*PNG decoder library*/ #define LV_USE_PNG 1 /*BMP decoder library*/ #define LV_USE_BMP 0 /* JPG + split JPG decoder library. * Split JPG is a custom format optimized for embedded systems. */ #define LV_USE_SJPG 0 /*GIF decoder library*/ #define LV_USE_GIF 0这里有个注意的点:
同时只能开启一种图片格式。 如果我把PNG和BMP都开了,然后在使用PNG的时候就会发现,打开图片的时候使用的是BMP的驱动。这从LVGL的初始化代码能看出问题所在
void lv_extra_init(void)
{
...
#if LV_USE_PNG
lv_png_init();
#endif
#if LV_USE_SJPG
lv_split_jpeg_init();
#endif
#if LV_USE_BMP
lv_bmp_init();
#endif
...
}
从这里可以看到,它是先初始化的PNG,最后初始化的BMP,BMP注册的驱动会把PNG的驱动覆盖掉。
=====================================================================================================
2、 图片打开显示NO DATA
在配置完上述操作后,会发现图片能打开,但是屏幕上却显示NO DATA 网上也没找到解决方法。 跟踪LVGL源码进去
static lv_res_t decoder_info(lv_img_decoder_t * decoder, const void * src, lv_img_header_t * header)
{
LV_UNUSED(decoder);
lv_img_src_t src_type = lv_img_src_get_type(src); /*Get the source type*/
/*If it's a BMP file...*/
if(src_type == LV_IMG_SRC_FILE) {
const char * fn = src;
if(strcmp(lv_fs_get_ext(fn), "bmp") == 0) { /*Check the extension*/
/*Save the data in the header*/
lv_fs_file_t f;
lv_fs_res_t res = lv_fs_open(&f, src, LV_FS_MODE_RD);
if(res != LV_FS_RES_OK) return LV_RES_INV;
uint8_t headers[54];
lv_fs_read(&f, headers, 54, NULL);
uint32_t w;
uint32_t h;
memcpy(&w, headers + 18, 4);
memcpy(&h, headers + 22, 4);
header->w = w;
header->h = h;
header->always_zero = 0;
lv_fs_close(&f);
#if LV_COLOR_DEPTH == 32
uint16_t bpp;
memcpy(&bpp, headers + 28, 2);
header->cf = bpp == 32 ? LV_IMG_CF_TRUE_COLOR_ALPHA : LV_IMG_CF_TRUE_COLOR;
#else
header->cf = LV_IMG_CF_TRUE_COLOR;
#endif
return LV_RES_OK;
}
}
/* BMP file as data not supported for simplicity.
* Convert them to LVGL compatible C arrays directly. */
else if(src_type == LV_IMG_SRC_VARIABLE) {
return LV_RES_INV;
}
return LV_RES_INV; /*If didn't succeeded earlier then it's an error*/
}
跟踪可以发现代码在执行到#if LV_COLOR_DEPTH == 32这里跳过去了,执行的是#else里的内容。这里其实就能知道原因了。然后回到lv_conf.h里改下颜色深度#define LV_COLOR_DEPTH 32即可显示PNG图片了
其他图片格式显示一样
本文由博客一文多发平台 OpenWrite 发布!