Bodmer / JPEGDecoder

A JPEG decoder library
Other
220 stars 64 forks source link

Decoded image does not work with LVGL #83

Open Pbaodoge opened 10 months ago

Pbaodoge commented 10 months ago

The issue

So I tried to use this library for fetching necessary information, along with LVGL running on ESP32, and after I follow the example code using createArray(), the decoding does works and does splitting out image data. Now, after I tried to modify it to work with LVGL and use it to display an image from the extracted data. All I got was a completely destroyed/blurred/interferences image. Sometimes, when the image got too big I also got a Guru Meditation error

Libraries that I'm using

LVGL v8.0, JpegDecoder, LovyanGFX

Here is my code


lv_img_dsc_t *image;

void loadimage(String filename, lv_img_dsc_t *img)
{
    File file = SD.open(filename, FILE_READ); // or, file handle reference for SD library
    if (!file) return;
    bool dec = JpegDec.decodeSdFile(filename);
    if (!dec) return;

    Serial.printf("Width: %d, Height: %d\n", JpegDec.width, JpegDec.height);
    Serial.printf("Size: %d bytes\n", JpegDec.width*JpegDec.height);
    Serial.printf("File size: %d bytes\n", file.size());
    Serial.println("\n\n");

    uint8_t *imagemap = new uint8_t[file.size()];
    int pos = 0;
    while (file.available())
    {
      uint8_t data = file.read();
      imagemap[pos] = data;
      pos++;
    }
    Serial.printf("pos: %d\n", pos);

    file.close();
    lv_img_dsc_t image = {
        {
            LV_IMG_CF_TRUE_COLOR,     // Header CF
            0,                        // header.alwayszero
            0,                        // Unknown
            JpegDec.width,  // Width
            JpegDec.height, // height
        },
        JpegDec.width * JpegDec.height * LV_COLOR_SIZE / 8, // data size
        imagemap                                            // data
    };
    memcpy(img, &image, sizeof(lv_img_dsc_t));
    return;
}

void initLVGL() {
    display.clear();
    display.setSwapBytes(true); //Needed for LVGL graphics
    lv_init();
    lv_disp_draw_buf_init(&draw_buf, buf1, NULL, TFT_WIDTH * TFT_HEIGHT / 10);  /*Initialize the display buffer.*/

    lv_disp_drv_init(&disp_drv);          /*Basic initialization*/
    disp_drv.flush_cb = my_disp_flush;    /*Set your driver function*/
    disp_drv.draw_buf = &draw_buf;        /*Assign the buffer to the display*/
    disp_drv.hor_res = TFT_WIDTH;   /*Set the horizontal resolution of the display*/
    disp_drv.ver_res = TFT_HEIGHT;   /*Set the vertical resolution of the display*/

    //LVGL registering
    lv_disp_t *disp = lv_disp_drv_register(&disp_drv);

    lv_indev_drv_init(&indev_drv);             /*Basic initialization*/
    indev_drv.type = LV_INDEV_TYPE_POINTER;    /*Touch pad is a pointer-like device*/
    indev_drv.read_cb = my_touchpad_read;      /*Set your driver function*/
    lv_indev_drv_register(&indev_drv);         /*Finally register the driver*/

    disp->refr_timer->period = (uint32_t)(1000 / FPS);

    //Create a theme

    loadimage("/images/image1.jpg", image);
    theme = lv_img_create(lv_scr_act());
    lv_img_set_src(theme, image);

    lv_obj_set_width(theme, LV_SIZE_CONTENT); /// 1
    lv_obj_set_height(theme, LV_SIZE_CONTENT);
    lv_obj_set_align(theme, LV_ALIGN_CENTER);
}

If you recognize any problems in the code, please help me. Thanks!