How to get a screenshot of the lv_chart graph?


I want to print a lv_chart graph through a thermal printer, how should I get a screenshot of the lv_chart graph?

What MCU/Processor/Board and compiler are you using?


What LVGL version are you using?


What do you want to achieve?

Get the display memory of the display area through API or simple method or redraw in the background

Here you will find out how to get the currently display area. But you should also be able to add whatever is drawn to the screen by the chart to another buffer and send that as input to the image function. then you need to get the file system repo from github and save the image to a file.

lv_disp_buf_t * buffer

Pointer to a buffer initialized with lv_disp_buf_init() . LVGL will use this buffer(s) to draw the screens contents

lv_disp_buf_t * lv_disp_get_buf (lv_disp_t * disp )

Get the internal buffer of a display

@excitedbox How to get the screenshot of the lv_chart graph?

You take a screenshot of the display using @excitedbox’s method and then crop the area that the chart is inside. You can get the chart’s absolute coordinates on the display using lv_obj_get_coords.

I actually think it might be better to hook into the draw call for the chart. That way you wouldn’t need to find the chart in the frame buffer. If you look in the chart code you should find the code that draws the chart and returns it to the display buffer. Take that data and send it to the image converter function. Then you need to use the filesystem to save that image.

This is exactly what I need, but I don’t know how to do it :sweat:

I took a quick look and that method wouldn’t work because of the way the system draws the screen. So the first suggestion of getting the screen buffer and trimming what is around the chart is the way to go. I will take another look and see if I can get help get you started.

I think a screen capture feature is actually quite useful and once we get a plugin collection going someone will surely add it.

Edit: I am an idiot, lv_obj_t * lv_chart_create returns the charts pointer. just convert that to an image and save it using the file system function.

looking at the file lv_img_decoder.c starting on line 106 it tells you how to convert an image. Option 2 or 3 apply for you.

    1. Variable: Pointer to an lv_img_dsc_t variable
    1. Symbol: E.g. LV_SYMBOL_OK

which you will find in lv_chart.c Line 602 - 627 LV_DESIGN_RES_OK; <-- Ok symbol containing your chart.

Then you will need to open the lv_fs.c and figure out how to save that image :smiley:

Return the Buf of the screenshot of the graph.

Like : char* GetScreenBuf(lv_obj_t* obj); :smiley:

I am not using file system.

I didn’t understand this paragraph。I know the function lv_chart_design .

Go to the line numbers I listed and look at the instructions. According to the comments in the image decoder function the SYMBOL_OK can be sent to the decoder to return an image of the widget. So LV_DESIGN_RES_OK should be what you want to use as the argument for the decoder. It is in the file lv_chart.c around line 627. create a variable to store the image src. Then use a btn with an event to call the decoder when the button is pushed.

The other option is using the chart_create() function. it returns “chart” which is supposed to contain the chart “background”. If I understood correctly it draws all the chart contents onto the background before returning it.