feat(snapshot) add API to take snapshot for object (#2353)
* Fix image zooming causes unexpected object size. Signed-off-by: Xu Xingliang <xuxingliang@xiaomi.com> * add lv_snapshot_take API. * fix(img) invalidate size and layout on zoom and angle change * fix(img) not self-repeating under some zoom level. * fix(snapshot) fix to keep the original position * Move various set_px_cb_xx functions to lv_hal_disp.c * add snapshot API to store image to provided buffer * minor fixes and refactoring * Move snapshot source to extra/others/snapshot. 1. Update parameter buff to buf. 2. Add macro to disable lv_snapshot, enabled by default. * docs(others) add the others folder with snapshot.md * docs(snapshot) added doc and example for snapshot. 1. Update doc snapshot.md 2. Add example lv_example_snapshot_1 to folder examples/others/snapshot 3. Update lv_conf_template.h and lv_conf_internal.h 4. Remove lv_snapshot.c from lv_misc.mk 5. Add others to index.md Signed-off-by: Xu Xingliang <xuxingliang@xiaomi.com> * add micropython example for snapshot Co-authored-by: Xu Xingliang <xuxingliang@xiaomi.com> Co-authored-by: Gabor Kiss-Vamosi <kisvegabor@gmail.com>
This commit is contained in:
@@ -23,6 +23,7 @@ extern "C" {
|
||||
#include "anim/lv_example_anim.h"
|
||||
#include "event/lv_example_event.h"
|
||||
#include "styles/lv_example_style.h"
|
||||
#include "others/lv_example_others.h"
|
||||
|
||||
/*********************
|
||||
* DEFINES
|
||||
|
||||
37
examples/others/lv_example_others.h
Normal file
37
examples/others/lv_example_others.h
Normal file
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* @file lv_example_others.h
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef LV_EXAMPLE_OTHERS_H
|
||||
#define LV_EXAMPLE_OTHERS_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*********************
|
||||
* INCLUDES
|
||||
*********************/
|
||||
#include "snapshot/lv_example_snapshot.h"
|
||||
/*********************
|
||||
* DEFINES
|
||||
*********************/
|
||||
|
||||
/**********************
|
||||
* TYPEDEFS
|
||||
**********************/
|
||||
|
||||
/**********************
|
||||
* GLOBAL PROTOTYPES
|
||||
**********************/
|
||||
|
||||
/**********************
|
||||
* MACROS
|
||||
**********************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /*extern "C"*/
|
||||
#endif
|
||||
|
||||
#endif /*LV_EX_OTHERS_H*/
|
||||
8
examples/others/snapshot/index.rst
Normal file
8
examples/others/snapshot/index.rst
Normal file
@@ -0,0 +1,8 @@
|
||||
|
||||
Simple snapshot example
|
||||
"""""""""""""""""""
|
||||
|
||||
.. lv_example:: others/snapshot/lv_example_snapshot_1
|
||||
:language: c
|
||||
|
||||
|
||||
38
examples/others/snapshot/lv_example_snapshot.h
Normal file
38
examples/others/snapshot/lv_example_snapshot.h
Normal file
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* @file lv_example_snapshot.h
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef LV_EX_SNAPSHOT_H
|
||||
#define LV_EX_SNAPSHOT_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*********************
|
||||
* INCLUDES
|
||||
*********************/
|
||||
|
||||
/*********************
|
||||
* DEFINES
|
||||
*********************/
|
||||
|
||||
/**********************
|
||||
* TYPEDEFS
|
||||
**********************/
|
||||
|
||||
/**********************
|
||||
* GLOBAL PROTOTYPES
|
||||
**********************/
|
||||
void lv_example_snapshot_1(void);
|
||||
|
||||
/**********************
|
||||
* MACROS
|
||||
**********************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /*extern "C"*/
|
||||
#endif
|
||||
|
||||
#endif /*LV_EX_GET_STARTED_H*/
|
||||
57
examples/others/snapshot/lv_example_snapshot_1.c
Normal file
57
examples/others/snapshot/lv_example_snapshot_1.c
Normal file
@@ -0,0 +1,57 @@
|
||||
#include "../../lv_examples.h"
|
||||
#if LV_USE_SNAPSHOT && LV_BUILD_EXAMPLES
|
||||
|
||||
static void event_cb(lv_event_t* e)
|
||||
{
|
||||
lv_obj_t * snapshot_obj = lv_event_get_user_data(e);
|
||||
lv_obj_t * img = lv_event_get_target(e);
|
||||
|
||||
if(snapshot_obj) {
|
||||
lv_img_dsc_t* snapshot = (void*)lv_img_get_src(snapshot_obj);
|
||||
if(snapshot){
|
||||
lv_snapshot_free(snapshot);
|
||||
}
|
||||
|
||||
/*Update the snapshot, we know parent of object is the container.*/
|
||||
snapshot = lv_snapshot_take(img->parent, LV_IMG_CF_TRUE_COLOR_ALPHA);
|
||||
if(snapshot == NULL)
|
||||
return;
|
||||
lv_img_set_src(snapshot_obj, snapshot);
|
||||
}
|
||||
}
|
||||
|
||||
void lv_example_snapshot_1(void)
|
||||
{
|
||||
LV_IMG_DECLARE(img_star);
|
||||
lv_obj_t * root = lv_scr_act();
|
||||
lv_obj_set_style_bg_color(root, lv_palette_main(LV_PALETTE_LIGHT_BLUE), 0);
|
||||
|
||||
/*Create an image object to show snapshot*/
|
||||
lv_obj_t * snapshot_obj = lv_img_create(root);
|
||||
lv_obj_set_style_bg_color(snapshot_obj, lv_palette_main(LV_PALETTE_PURPLE), 0);
|
||||
lv_obj_set_style_bg_opa(snapshot_obj, LV_OPA_100, 0);
|
||||
lv_img_set_zoom(snapshot_obj, 128);
|
||||
|
||||
/*Create the container and its children*/
|
||||
lv_obj_t * container = lv_obj_create(root);
|
||||
|
||||
lv_obj_align(container, LV_ALIGN_CENTER, 0, 0);
|
||||
lv_obj_set_size(container, 180, 180);
|
||||
lv_obj_set_flex_flow(container, LV_FLEX_FLOW_ROW_WRAP);
|
||||
lv_obj_set_flex_align(container, LV_FLEX_ALIGN_SPACE_EVENLY, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
|
||||
lv_obj_set_style_radius(container, 50, 0);
|
||||
lv_obj_t * img;
|
||||
int i;
|
||||
for(i = 0; i < 4; i++) {
|
||||
img = lv_img_create(container);
|
||||
lv_img_set_src(img, &img_star);
|
||||
lv_obj_set_style_bg_color(img, lv_color_black(), 0);
|
||||
lv_obj_set_style_bg_opa(img, LV_OPA_COVER, 0);
|
||||
lv_obj_set_style_transform_zoom(img, 400, LV_STATE_PRESSED);
|
||||
lv_obj_add_flag(img, LV_OBJ_FLAG_CLICKABLE);
|
||||
lv_obj_add_event_cb(img, event_cb, LV_EVENT_PRESSED, snapshot_obj);
|
||||
lv_obj_add_event_cb(img, event_cb, LV_EVENT_RELEASED, snapshot_obj);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
70
examples/others/snapshot/lv_example_snapshot_1.py
Normal file
70
examples/others/snapshot/lv_example_snapshot_1.py
Normal file
@@ -0,0 +1,70 @@
|
||||
import lvgl as lv
|
||||
from imagetools import get_png_info, open_png
|
||||
|
||||
# Register PNG image decoder
|
||||
decoder = lv.img.decoder_create()
|
||||
decoder.info_cb = get_png_info
|
||||
decoder.open_cb = open_png
|
||||
|
||||
# Measure memory usage
|
||||
gc.enable()
|
||||
gc.collect()
|
||||
mem_free = gc.mem_free()
|
||||
|
||||
label = lv.label(lv.scr_act())
|
||||
label.align(lv.ALIGN.BOTTOM_MID, 0, -10)
|
||||
label.set_text(" memory free:" + str(mem_free/1024) + " kB")
|
||||
|
||||
# Create an image from the png file
|
||||
try:
|
||||
with open('../../assets/img_star.png','rb') as f:
|
||||
png_data = f.read()
|
||||
except:
|
||||
print("Could not find img_star.png")
|
||||
sys.exit()
|
||||
|
||||
img_star = lv.img_dsc_t({
|
||||
'data_size': len(png_data),
|
||||
'data': png_data
|
||||
})
|
||||
|
||||
def event_cb(e, snapshot_obj):
|
||||
img = e.get_target()
|
||||
|
||||
if snapshot_obj:
|
||||
# no need to free the old source for snapshot_obj, gc will free it for us.
|
||||
|
||||
# take a new snapshot, overwrite the old one
|
||||
dsc = lv.snapshot_take(img.get_parent(), lv.img.CF.TRUE_COLOR_ALPHA)
|
||||
snapshot_obj.set_src(dsc)
|
||||
|
||||
gc.collect()
|
||||
mem_used = mem_free - gc.mem_free()
|
||||
label.set_text("memory used:" + str(mem_used/1024) + " kB")
|
||||
|
||||
root = lv.scr_act()
|
||||
root.set_style_bg_color(lv.palette_main(lv.PALETTE.LIGHT_BLUE), 0)
|
||||
|
||||
# Create an image object to show snapshot
|
||||
snapshot_obj = lv.img(root)
|
||||
snapshot_obj.set_style_bg_color(lv.palette_main(lv.PALETTE.PURPLE), 0)
|
||||
snapshot_obj.set_style_bg_opa(lv.OPA.COVER, 0)
|
||||
snapshot_obj.set_zoom(128)
|
||||
|
||||
# Create the container and its children
|
||||
container = lv.obj(root)
|
||||
container.align(lv.ALIGN.CENTER, 0, 0)
|
||||
container.set_size(180, 180)
|
||||
container.set_flex_flow(lv.FLEX_FLOW.ROW_WRAP)
|
||||
container.set_flex_align(lv.FLEX_ALIGN.SPACE_EVENLY, lv.FLEX_ALIGN.CENTER, lv.FLEX_ALIGN.CENTER)
|
||||
container.set_style_radius(50, 0)
|
||||
|
||||
for i in range(4):
|
||||
img = lv.img(container)
|
||||
img.set_src(img_star)
|
||||
img.set_style_bg_color(lv.palette_main(lv.PALETTE.GREY), 0)
|
||||
img.set_style_bg_opa(lv.OPA.COVER, 0)
|
||||
img.set_style_transform_zoom(400, lv.STATE.PRESSED)
|
||||
img.add_flag(img.FLAG.CLICKABLE)
|
||||
img.add_event_cb(lambda e: event_cb(e, snapshot_obj), lv.EVENT.PRESSED, None)
|
||||
img.add_event_cb(lambda e: event_cb(e, snapshot_obj), lv.EVENT.RELEASED, None)
|
||||
Reference in New Issue
Block a user