久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

標題: esp32利用Arduino_GFX_Library庫顯示圖片問題 [打印本頁]

作者: 美琴的備胎    時間: 2023-2-21 13:29
標題: esp32利用Arduino_GFX_Library庫顯示圖片問題
好兄弟們,我在用esp32c3讀取SD卡中圖片并顯示到tft1.8屏幕上,單獨一張圖片顯示正常顯示,但是循環一組圖片時,就一直顯示第一張圖片。代碼貼到下面
///////////////////////////////////////代碼開始///////////////////////////////////////////////
  1. #include <SPI.h>
  2. #include <mySD.h>
  3. #include <Arduino_GFX_Library.h>
  4. #include<String.h>
  5. ext::File bmpFile;
  6. unsigned short pic[0x5000]; // 128*160圖像0x5000像素
  7. byte aByte;                 // 1字節數據
  8. /*******重復播放圖片所需變量*******************************/
  9. char* aa[]={"1.bmp","2.bmp","3.bmp","4.bmp","5.bmp"};
  10. unsigned int bmp;
  11. /******************************************************/
  12. Arduino_DataBus *bus = new Arduino_ESP32SPI(5 /* DC */, 7 /* CS */, 2 /* SCK */, 3 /* MOSI */);
  13. Arduino_GFX *gfx = new Arduino_ST7735(bus, 4 /* RST */, 2 /* 旋轉屏幕180度 */, false);

  14. void setup()
  15. {
  16.     Serial.begin(115200);
  17.     //從SD卡讀取圖片
  18.     SD.begin(6, 3, 10, 2);
  19. }
  20. void loop()
  21. {
  22.    for(bmp=0;bmp<5;bmp++){
  23.    bmpFile = SD.open(aa[bmp], FILE_READ);
  24.    if (bmpFile){
  25.         //顯示圖片
  26.         while (bmpFile.available())
  27.         {
  28.             //跳過前0x36個字節的頭
  29.             for (int i = 0; i < 0x36; i++)
  30.             {
  31.                 Serial.print(bmpFile.read(), HEX);
  32.             }
  33.             //把rgb24轉換成rgb16
  34.             for (int i = 0; i < 0x5000; i++)
  35.             {
  36.                 aByte = bmpFile.read();
  37.                 pic[i] = (aByte * 32 / 256) << 11;
  38.                 aByte = bmpFile.read();
  39.                 pic[i] |= (aByte * 64 / 256) << 5;
  40.                 aByte = bmpFile.read();
  41.                 pic[i] |= (aByte * 32 / 256);
  42.             }
  43.         }
  44.        bmpFile.close();
  45.     }
  46.     //顯示圖片
  47.     gfx->begin();
  48.     gfx->fillScreen(WHITE);
  49.     gfx->draw16bitRGBBitmap(0, 0, pic, 128, 160);
  50.     delay(100);
  51.   }
  52. }
復制代碼
///////////////////////////////////////代碼結束///////////////////////////////////////////////
就是這句bmpFile = SD.open(aa[bmp], FILE_READ);,我aa[]數組放的是圖片的名稱:{"1.bmp","2.bmp","3.bmp","4.bmp","5.bmp"},當寫為aa[0]時,顯示的是"1.bmp"這張圖片,是正確的。但是用循環 for(bmp=0;bmp<5;bmp++),然后aa[bmp]時,只會顯示第一張圖片5次,而不會顯示"2.bmp","3.bmp","4.bmp","5.bmp",各位好兄弟有什么想法嗎?

作者: watsonbu    時間: 2023-2-21 14:48
<SPI.h>
<mySD.h>
<Arduino_GFX_Library.h>
<String.h>
如果可以把這幾個頭文件提供一下,愿意幫你調試一下
作者: 美琴的備胎    時間: 2023-2-21 15:30
watsonbu 發表于 2023-2-21 14:48
如果可以把這幾個頭文件提供一下,愿意幫你調試一下

謝謝好兄弟,spi.h和string.h是自帶庫,其他兩個我怎么發給你?
作者: 人中狼    時間: 2023-2-21 16:26
可以先不用數組,直接把顯示圖片部分重復五次,每次開不同的圖片,就可以確定問題點了
作者: 美琴的備胎    時間: 2023-2-21 16:33
人中狼 發表于 2023-2-21 16:26
可以先不用數組,直接把顯示圖片部分重復五次,每次開不同的圖片,就可以確定問題點了

這個方法也試過,我是把顯示部分復制了兩次,結果是第一張圖片顯示了兩次。刷新函數也有,我就想不通是哪里出現了問題,你可以看下我的程序,我是把顯示部分除了gfx變量的所有變量都新定義了新變量,gfx是屏幕引腳定義變量,我覺得應該不是這個方法問題。
作者: watsonbu    時間: 2023-2-21 17:18
就在黑51發消息給我就可以了
作者: 美琴的備胎    時間: 2023-2-21 17:45
watsonbu 發表于 2023-2-21 17:18
就在黑51發消息給我就可以了

我手機沒看到怎么發文件給你,是要用電腦發給你嗎?
作者: 人中狼    時間: 2023-2-21 17:49
美琴的備胎 發表于 2023-2-21 16:33
這個方法也試過,我是把顯示部分復制了兩次,結果是第一張圖片顯示了兩次。刷新函數也有,我就想不通是哪 ...

我的意思是這里bmpFile = SD.open(aa[bmp], FILE_READ);不用數組,看代碼應該是這個函數調用的問題,只打開了一個文件,可以查查這個函數的具體用法和參數要求。
作者: 人中狼    時間: 2023-2-21 17:51
美琴的備胎 發表于 2023-2-21 16:33
這個方法也試過,我是把顯示部分復制了兩次,結果是第一張圖片顯示了兩次。刷新函數也有,我就想不通是哪 ...

還有一個已經打開的文件,在開第二個文件時是否需要關閉的問題
作者: 美琴的備胎    時間: 2023-2-21 18:55
人中狼 發表于 2023-2-21 17:49
我的意思是這里bmpFile = SD.open(aa, FILE_READ);不用數組,看代碼應該是這個函數調用的問題,只打開了 ...

這個原始程序就是bmpFile = SD.open(“1.bmp”, FILE_READ);,我改成數組調用了,aa[0]的話也是可以正常顯示的,我試過了
作者: 美琴的備胎    時間: 2023-2-21 18:58
人中狼 發表于 2023-2-21 17:51
還有一個已經打開的文件,在開第二個文件時是否需要關閉的問題

bmpFile.close();  有的,要關的
作者: 美琴的備胎    時間: 2023-2-21 19:24
#include <SPI.h>
#include <mySD.h>
#include <Arduino_GFX_Library.h>
#include<String.h>
ext::File bmpFile,bmpFile1;
unsigned short pic[0x5000],pic1[0x5000]; // 128*160圖像0x5000像素
byte aByte,aByte1;                 // 1字節數據
/*******重復播放圖片所需變量*******************************/
char* aa[]={"1.bmp","2.bmp","3.bmp","4.bmp","5.bmp"};
unsigned int bmp;
/******************************************************/
Arduino_DataBus *bus = new Arduino_ESP32SPI(5 /* DC */, 7 /* CS */, 2 /* SCK */, 3 /* MOSI */);
Arduino_GFX *gfx = new Arduino_ST7735(bus, 4 /* RST */, 2 /* 旋轉屏幕180度 */, false);

void setup()
{
    Serial.begin(115200);
    //從SD卡讀取圖片
    SD.begin(6, 3, 10, 2);
}
void loop()
{
   //for(bmp=0;bmp<5;bmp++){
   bmpFile = SD.open(/*aa[bmp]*/"1.bmp", FILE_READ);
   if (bmpFile){
        //顯示圖片
        while (bmpFile.available())
        {
            //跳過前0x36個字節的頭
            for (int i = 0; i < 0x36; i++)
            {
                Serial.print(bmpFile.read(), HEX);
            }
            //把rgb24轉換成rgb16
            for (int i = 0; i < 0x5000; i++)
            {
                aByte = bmpFile.read();
                pic[i] = (aByte * 32 / 256) << 11;
                aByte = bmpFile.read();
                pic[i] |= (aByte * 64 / 256) << 5;
                aByte = bmpFile.read();
                pic[i] |= (aByte * 32 / 256);
            }
        }  
       bmpFile.close();
   }


    bmpFile1 = SD.open(/*aa[bmp]*/"2.bmp", FILE_READ);
   if (bmpFile1){
        //顯示圖片
        while (bmpFile1.available())
        {
            //跳過前0x36個字節的頭
            for (int i = 0; i < 0x36; i++)
            {
                Serial.print(bmpFile1.read(), HEX);
            }
            //把rgb24轉換成rgb16
            for (int i = 0; i < 0x5000; i++)
            {
                aByte1 = bmpFile1.read();
                pic1[i] = (aByte1 * 32 / 256) << 11;
                aByte1 = bmpFile1.read();
                pic1[i] |= (aByte1 * 64 / 256) << 5;
                aByte1 = bmpFile1.read();
                pic1[i] |= (aByte1 * 32 / 256);
            }
        }  
       bmpFile1.close();
   }

   
    //顯示圖片
    gfx->begin();
    gfx->fillScreen(WHITE);
    gfx->draw16bitRGBBitmap(0, 0, pic, 128, 160);
    delay(100);
    gfx->draw16bitRGBBitmap(0, 0, pic1, 128, 160);
    delay(100);
   //}
}
這樣寫代碼,可以兩個圖片循環顯示了
作者: 人中狼    時間: 2023-2-21 20:12
返回一下文件打開狀態吧,如果if (bmpFile)這個不成立的話,顯示數據pic[ i] 不會改變,應該就還是之前打開文件的數據,問題應該還是在文件的打開上,后面的文件都沒有成功打開,只打開了第一個
作者: watsonbu    時間: 2023-2-21 21:27
你的程序有個小問題,從頭到尾再找吧。

ESP32.PNG (30.6 KB, 下載次數: 75)

可能程序有點小問題

可能程序有點小問題

作者: 人中狼    時間: 2023-2-21 22:28
美琴的備胎 發表于 2023-2-21 19:24
#include
#include
#include

aa[]定義錯誤,https://blog.csdn.net/qiaoermeng/article/details/88366250,可以看看這里的解釋
作者: watsonbu    時間: 2023-2-21 22:31
修改了如下,不知道能不能成功

#include <SPI.h>
#include <mySD.h>
#include <Arduino_GFX_Library.h>
#include<String.h>
ext::File bmpFile;
unsigned short pic[0x5000]; // 128*160圖像0x5000像素
byte aByte;                 // 1字節數據
/*******重復播放圖片所需變量*******************************/
char* aa[5]={"1.bmp","2.bmp","3.bmp","4.bmp","5.bmp"};
unsigned int bmp;
/******************************************************/
Arduino_DataBus *bus = new Arduino_ESP32SPI(5 /* DC */, 7 /* CS */, 2 /* SCK */, 3 /* MOSI */);
Arduino_GFX *gfx = new Arduino_ST7735(bus, 4 /* RST */, 2 /* 旋轉屏幕180度 */, false);

void setup()
{
    Serial.begin(115200);
    //從SD卡讀取圖片
    SD.begin(6, 3, 10, 2);
}
void loop()
{
   for(bmp=0;bmp<5;bmp++){
   bmpFile = SD.open(aa[bmp], FILE_READ);
   if (bmpFile){
        //顯示圖片
        while (bmpFile.available())
        {
            //跳過前0x36個字節的頭
            for (int i = 0; i < 0x36; i++)
            {
                Serial.print(bmpFile.read(), HEX);
            }
            //把rgb24轉換成rgb16
            for (int i = 0; i < 0x5000; i++)
            {
                aByte = bmpFile.read();
                pic[i] = (aByte * 32 / 256) << 11;
                aByte = bmpFile.read();
                pic[i] |= (aByte * 64 / 256) << 5;
                aByte = bmpFile.read();
                pic[i] |= (aByte * 32 / 256);
            }
        }
        /***********************************/
       if (++bmp == 5)
       bmp = 0;          //計數復位
       /***********************************/
       bmpFile.close();
      
      // bmpFile.delete();//試試不行再加這句再試試***********
    }
    //顯示圖片
    gfx->begin();
    gfx->fillScreen(WHITE);
    gfx->draw16bitRGBBitmap(0, 0, pic, 128, 160);
   
    delay(100);//前面修改完還不行 ,改為delay(1000)試試************
  }
}
作者: 美琴的備胎    時間: 2023-2-21 22:40
watsonbu 發表于 2023-2-21 21:27
你的程序有個小問題,從頭到尾再找吧。

好兄弟,你這是缺了sd卡的庫,這是地址github.com/nhatuan84/esp32-micro-sdcard/blob/master/mySD.h
作者: 美琴的備胎    時間: 2023-2-21 23:00
人中狼 發表于 2023-2-21 22:28
aa[]定義錯誤,https://blog.csdn.net/qiaoermeng/article/details/88366250,可以看看這里的解釋

我程序改成這樣了

String aa[]={"1.bmp","2.bmp","3.bmp","4.bmp","5.bmp"};
*****
   for(bmp=0;bmp<5;bmp++){
   bmpFile = SD.open(aa[bmp].c_str(), FILE_READ);
******
最后輸出了下 :
Serial.println(aa[bmp].c_str());

這是輸出內容:
22:57:37.539 -> 5.bmp
22:57:38.886 -> 1.bmp
22:57:40.249 -> 2.bmp
22:57:41.615 -> 3.bmp
22:57:42.996 -> 4.bmp
22:57:44.372 -> 5.bmp
22:57:45.722 -> 1.bmp
22:57:47.085 -> 2.bmp
22:57:48.454 -> 3.bmp
22:57:49.842 -> 4.bmp
22:57:51.227 -> 5.bmp
22:57:52.575 -> 1.bmp
22:57:53.966 -> 2.bmp
22:57:55.316 -> 3.bmp
22:57:56.697 -> 4.bmp
22:57:58.031 -> 5.bmp
22:57:59.404 -> 1.bmp
22:58:00.797 -> 2.bmp
22:58:02.164 -> 3.bmp
確實已經寫入文件名了,但是顯示屏還是只顯示第一張圖片
作者: 人中狼    時間: 2023-2-21 23:12
這個不熟,不過看代碼SD.open(/*aa[bmp]*/"2.bmp", FILE_READ);,串口的輸出似乎少了雙引號,看現象就是pic[]的數據沒更新,也就是if (bmpFile)不成立
作者: watsonbu    時間: 2023-2-21 23:20
#include <SPI.h>
#include <mySD.h>
#include <Arduino_GFX_Library.h>
#include<String.h>
ext::File bmpFile;
unsigned short pic[0x5000]; // 128*160圖像0x5000像素
byte aByte;                 // 1字節數據
/*******重復播放圖片所需變量*******************************/
char* aa[5]={"1.bmp","2.bmp","3.bmp","4.bmp","5.bmp"};
unsigned int bmp;
/******************************************************/
Arduino_DataBus *bus = new Arduino_ESP32SPI(5 /* DC */, 7 /* CS */, 2 /* SCK */, 3 /* MOSI */);
Arduino_GFX *gfx = new Arduino_ST7735(bus, 4 /* RST */, 2 /* 旋轉屏幕180度 */, false);

void setup()
{
    Serial.begin(115200);
    //從SD卡讀取圖片
    SD.begin(6, 3, 10, 2);
}
void loop()
{
   for(bmp=0;bmp<5;bmp++){
   bmpFile = SD.open(aa[bmp], FILE_READ);
   if (bmpFile){
        //顯示圖片
        while (bmpFile.available())
        {
            //跳過前0x36個字節的頭
            for (int i = 0; i < 0x36; i++)
            {
                Serial.print(bmpFile.read(), HEX);
            }
            //把rgb24轉換成rgb16
            for (int i = 0; i < 0x5000; i++)
            {
                aByte = bmpFile.read();
                pic[i] = (aByte * 32 / 256) << 11;
                aByte = bmpFile.read();
                pic[i] |= (aByte * 64 / 256) << 5;
                aByte = bmpFile.read();
                pic[i] |= (aByte * 32 / 256);
            }
        }
        /***********************************/
       if (++bmp == 5)
       bmp = 0;          //計數復位
       /***********************************/
       bmpFile.close();
      
      // bmpFile.delete();//試試不行再加這句***********
    }
    //顯示圖片
    gfx->begin();
    gfx->fillScreen(WHITE);
    gfx->draw16bitRGBBitmap(0, 0, pic, 128, 160);
   
    delay(100);//前面修改完還不行 ,改為delay(1000)試試************
  }
}
作者: 美琴的備胎    時間: 2023-2-22 12:26
watsonbu 發表于 2023-2-21 23:20
#include
#include
#include

只加
/***********************************/
       if (++bmp == 5)
       bmp = 0;          //計數復位
/***********************************/
也不行
  bmpFile.delete(); 也不行,會報錯expected unqualified-id before 'delete'
作者: watsonbu    時間: 2023-2-22 12:41
“ext::File bmpFile;”
這里能改成指針?類似    ext::File  * bmpFile;
之后只加和不加兩種:
/***********************************/
       if (++bmp == 5)
       bmp = 0;          //計數復位
/***********************************/
試試?有Bug,就是如此令人不愉快
作者: 美琴的備胎    時間: 2023-2-22 12:56
watsonbu 發表于 2023-2-22 12:41
“ext::File bmpFile;”
這里能改成指針?類似    ext::File  * bmpFile;
之后只加和不加兩種:

試過了,會報錯,我都在考慮換個顯示庫了,有推薦嗎?
作者: watsonbu    時間: 2023-2-22 13:04
“unsigned short pic[0x5000]; // 128*160圖像0x5000像素”
這邊有這樣的問題,調試出錯說, pic[0x5000]的0x5000太大。

如果考慮這些之后不行,是不是要考慮 LINUX 調試ESP32了?但是配置完 LINUX 之后,真的不推薦。
看你的程序很清晰的,似乎還是沒有循環讀進來。很久以前也感同身受,慢慢折騰,也許得來全不費工夫。

還有一個可能解決問題的辦法,找一找 ESP32的供應商,他們有專門的售后軟件工程師,當時好像跟他們一起,才把問題解決掉。但是他們就是用的 LINUX 調試和燒錄的,搞好之后,不要用LINUX 燒錄,太慢了!直接用他們的串口軟件燒錄挺快的。
作者: 湯勇輝    時間: 2023-2-22 14:22
安信可軟件怎么編譯三元組
作者: 人中狼    時間: 2023-2-22 16:30
美琴的備胎 發表于 2023-2-21 23:00
我程序改成這樣了

String aa[]={"1.bmp","2.bmp","3.bmp","4.bmp","5.bmp"};

你這里應該是在bmpFile = SD.open(aa[bmp].c_str(), FILE_READ);后看bmpFile的值,現在串口輸出的只是數組aa[]的內容而已,并不是打開文件是否成功的結果
作者: watsonbu    時間: 2023-2-22 17:53
。。。。
SD.begin(6, 3, 10, 2);
}

void clear_buffer(){                         //緩存清除,返回-1,緩存清除。
while(Serial.read()>=0);
}
。。。。。
  gfx->draw16bitRGBBitmap(0, 0, pic, 128, 160);
    delay(1000);
    clear_buffeer();//調用清掉前一緩存
  }
}
作者: wfqxgw    時間: 2023-2-23 06:50
路過。順便問一下。Arduino_GFX_Library用這個庫后,編譯速度是不是 很慢。大概多長時間?
作者: 美琴的備胎    時間: 2023-2-23 14:32
watsonbu 發表于 2023-2-22 17:53
。。。。
SD.begin(6, 3, 10, 2);
}

    gfx->fillScreen(WHITE);
這一句就是刷新
作者: 美琴的備胎    時間: 2023-2-23 14:33
wfqxgw 發表于 2023-2-23 06:50
路過。順便問一下。Arduino_GFX_Library用這個庫后,編譯速度是不是 很慢。大概多長時間?

嗯…我不知道是不是因為這個庫的原因,我感覺esp32編譯都很慢
作者: watsonbu    時間: 2023-2-24 13:18
“esp32編譯都很慢”
是arduino慢,特別是大一點的程序,里面啰啰嗦嗦一堆
作者: 美琴的備胎    時間: 2023-2-24 19:34
問題解決了,用TFT_espi庫,源程序放下面:
//程序開始
/*
*@功能:ESP32讀取SD卡圖片顯示在1.14IPS屏幕上
*@作者:劉澤文
*@時間:2020/3/27
*/

//引用相關庫
#include <SD.h>
#include <FS.h>
#include <SPI.h>
#include <TFT_eSPI.h>
#include <JPEGDecoder.h>


#define DEBUG

#ifdef DEBUG
#define DebugPrintln(message) Serial.println(message)
#else
#define DebugPrintln(message)
#endif

#ifdef DEBUG
#define DebugPrint(message) Serial.print(message)
#else
#define DebugPrint(message)
#endif

TFT_eSPI tft = TFT_eSPI(128, 160); // Invoke custom library
SPIClass sdSPI(VSPI);
#define SD_MISO     10
#define SD_MOSI     3
#define SD_SCLK     2
#define SD_CS       9

void drawSdJpeg(const char *filename, int xpos, int ypos);
void jpegRender(int xpos, int ypos);
void jpegInfo();
void showTime(uint32_t msTime);
void SD_read_Time(uint32_t msTime);

void setup()
{
  Serial.begin(9600);
  DebugPrintln();

  tft.init();
  tft.setRotation(1);
  tft.fillScreen(TFT_WHITE);
  tft.setTextSize(1);
  tft.setTextColor(TFT_MAGENTA);
  tft.setCursor(0, 0);
  tft.setTextDatum(MC_DATUM);
  tft.setTextSize(1);
  tft.setSwapBytes(true);
  delay(500);

  if (TFT_BL > 0) { // TFT_BL has been set in the TFT_eSPI library in the User Setup file TTGO_T_Display.h
     pinMode(TFT_BL, OUTPUT); // Set backlight pin to output mode
     digitalWrite(TFT_BL, 1); // TFT_BACKLIGHT_ONTurn backlight on. TFT_BACKLIGHT_ON has been set in the TFT_eSPI library in the User Setup file TTGO_T_Display.h
   }
  
  //掛載文件系統
  sdSPI.begin(SD_SCLK, SD_MISO, SD_MOSI, SD_CS);
  if (!SD.begin(SD_CS, sdSPI))
  {
    DebugPrintln("存儲卡掛載失敗");
    return;
  }
  uint8_t cardType = SD.cardType();

  if (cardType == CARD_NONE)
  {
    DebugPrintln("未連接存儲卡");
    return;
  }
  else if (cardType == CARD_MMC)
  {
    DebugPrintln("掛載了MMC卡");
  }
  else if (cardType == CARD_SD)
  {
    DebugPrintln("掛載了SDSC卡");
  }
  else if (cardType == CARD_SDHC)
  {
    DebugPrintln("掛載了SDHC卡");
  }
  else
  {
    DebugPrintln("掛載了未知存儲卡");
  }

  //打印存儲卡信息
  Serial.printf("存儲卡總大小是: %lluMB \n", SD.cardSize() / (1024 * 1024)); // "/ (1024 * 1024)"可以換成">> 20"
  Serial.printf("文件系統總大小是: %lluB \n", SD.totalBytes());
  Serial.printf("文件系統已用大小是: %lluB \n", SD.usedBytes());
}

void loop() {
/*
  //測試壁紙
  for(int image_num = 1;image_num<=6;image_num++){
    char FileName[10];
    sprintf(FileName,"%d.jpg",image_num);
    drawSdJpeg(FileName, 0, 0);     // This draws a jpeg pulled off the SD Card
    delay(500);
  }
*/
  //播放badapple,共6540幀,每秒30幀
  for(int image_num = 1;image_num<=2;image_num+=1){
    char FileName[10];
    sprintf(FileName,"/%d.jpg",image_num);
    //drawSdJpeg(FileName, 0, 0);     // This draws a jpeg pulled off the SD Card
    drawSdJpeg(FileName, 0, 0);
    delay(100);
  }

}

void drawSdJpeg(const char *filename, int xpos, int ypos) {
  uint32_t readTime = millis();
  // Open the named file (the Jpeg decoder library will close it)
  File jpegFile = SD.open( filename, FILE_READ);  // or, file handle reference for SD library

  if ( !jpegFile ) {
    DebugPrint("ERROR: File \"");
    DebugPrint(jpegFile);
    DebugPrintln ("\" not found!");
    return;
  }

  DebugPrintln("===========================");
  DebugPrint("Drawing file: "); DebugPrintln(filename);
  DebugPrintln("===========================");

  // Use one of the following methods to initialise the decoder:
  boolean decoded = JpegDec.decodeSdFile(jpegFile);  // Pass the SD file handle to the decoder,
  //boolean decoded = JpegDec.decodeSdFile(filename);  // or pass the filename (String or character array)
  SD_read_Time(millis() - readTime);

  if (decoded) {
    // print information about the image to the serial port
    jpegInfo();
    // render the image onto the screen at given coordinates
    jpegRender(xpos, ypos);
  }
  else {
    DebugPrintln("Jpeg file format not supported!");
  }
}

//####################################################################################################
// Draw a JPEG on the TFT, images will be cropped on the right/bottom sides if they do not fit
//####################################################################################################
// This function assumes xpos,ypos is a valid screen coordinate. For convenience images that do not
// fit totally on the screen are cropped to the nearest MCU size and may leave right/bottom borders.
void jpegRender(int xpos, int ypos) {
  // record the current time so we can measure how long it takes to draw an image
  uint32_t drawTime = millis();

  //jpegInfo(); // Print information from the JPEG file (could comment this line out)

  uint16_t *pImg;
  uint16_t mcu_w = JpegDec.MCUWidth;
  uint16_t mcu_h = JpegDec.MCUHeight;
  uint32_t max_x = JpegDec.width;
  uint32_t max_y = JpegDec.height;

  bool swapBytes = tft.getSwapBytes();
  tft.setSwapBytes(true);
  
  // Jpeg images are draw as a set of image block (tiles) called Minimum Coding Units (MCUs)
  // Typically these MCUs are 16x16 pixel blocks
  // Determine the width and height of the right and bottom edge image blocks
  uint32_t min_w = (mcu_w<(max_x % mcu_w)?mcu_w:(max_x % mcu_w));
  uint32_t min_h = (mcu_h<(max_y % mcu_h)?mcu_h:(max_y % mcu_h));

  // save the current image block size
  uint32_t win_w = mcu_w;
  uint32_t win_h = mcu_h;

  // save the coordinate of the right and bottom edges to assist image cropping
  // to the screen size
  max_x += xpos;
  max_y += ypos;

  // Fetch data from the file, decode and display
  while (JpegDec.read()) {    // While there is more data in the file
    pImg = JpegDec.pImage ;   // Decode a MCU (Minimum Coding Unit, typically a 8x8 or 16x16 pixel block)

    // Calculate coordinates of top left corner of current MCU
    int mcu_x = JpegDec.MCUx * mcu_w + xpos;
    int mcu_y = JpegDec.MCUy * mcu_h + ypos;

    // check if the image block size needs to be changed for the right edge
    if (mcu_x + mcu_w <= max_x) win_w = mcu_w;
    else win_w = min_w;

    // check if the image block size needs to be changed for the bottom edge
    if (mcu_y + mcu_h <= max_y) win_h = mcu_h;
    else win_h = min_h;

    // copy pixels into a contiguous block
    if (win_w != mcu_w)
    {
      uint16_t *cImg;
      int p = 0;
      cImg = pImg + win_w;
      for (int h = 1; h < win_h; h++)
      {
        p += mcu_w;
        for (int w = 0; w < win_w; w++)
        {
          *cImg = *(pImg + w + p);
          cImg++;
        }
      }
    }

    // calculate how many pixels must be drawn
    uint32_t mcu_pixels = win_w * win_h;

    // draw image MCU block only if it will fit on the screen
    if (( mcu_x + win_w ) <= tft.width() && ( mcu_y + win_h ) <= tft.height())
      tft.pushImage(mcu_x, mcu_y, win_w, win_h, pImg);
    else if ( (mcu_y + win_h) >= tft.height())
      JpegDec.abort(); // Image has run off bottom of screen so abort decoding
  }

  tft.setSwapBytes(swapBytes);

  showTime(millis() - drawTime); //將圖片顯示到屏幕所用的時間(ms)
}

void jpegInfo() {
  DebugPrintln("JPEG image info");
  DebugPrintln("===============");
  DebugPrint("Width      :");
  DebugPrintln(JpegDec.width);
  DebugPrint("Height     :");
  DebugPrintln(JpegDec.height);
  DebugPrint("Components :");
  DebugPrintln(JpegDec.comps);
  DebugPrint("MCU / row  :");
  DebugPrintln(JpegDec.MCUSPerRow);
  DebugPrint("MCU / col  :");
  DebugPrintln(JpegDec.MCUSPerCol);
  DebugPrint("Scan type  :");
  DebugPrintln(JpegDec.scanType);
  DebugPrint("MCU width  :");
  DebugPrintln(JpegDec.MCUWidth);
  DebugPrint("MCU height :");
  DebugPrintln(JpegDec.MCUHeight);
  DebugPrintln("===============");
  DebugPrintln("");
}

void showTime(uint32_t msTime) {
  DebugPrint(F(" JPEG drawn in "));
  DebugPrint(msTime);
  DebugPrintln(F(" ms "));
}

void SD_read_Time(uint32_t msTime) {
  Serial.print(F(" SD JPEG read in "));
  Serial.print(msTime);
  Serial.println(F(" ms "));
}
//程序結束,可以看這篇博客,我借鑒這位大佬的https://blog.csdn.net/weixin_42880082/article/details/125939534
作者: 美琴的備胎    時間: 2023-2-24 22:37
美琴的備胎 發表于 2023-2-24 19:34
問題解決了,用TFT_espi庫,源程序放下面:
//程序開始
/*

此貼完結,謝謝大家
作者: hhdsdy    時間: 2023-2-25 00:09
有始有終,值得一贊,不過用Arduino_GFX_Library庫不能用的問題還是沒有解決,希望到時候有更完美的結果。




歡迎光臨 (http://www.zg4o1577.cn/bbs/) Powered by Discuz! X3.1
主站蜘蛛池模板: 中文字幕免费视频 | 精品视频一区二区三区 | 成人免费视频网站在线看 | 国产精品美女久久久久aⅴ国产馆 | 一区二区三区四区免费在线观看 | 精品一区二区三区在线观看 | 黑人巨大精品欧美一区二区免费 | 日日操夜夜干 | 一级片成人 | 成人欧美一区二区三区在线观看 | 日韩免费视频 | 国产精品a一区二区三区网址 | 亚洲一区成人 | avmans最新导航地址 | 欧美精品一区二区三区在线播放 | 欧美激情第一区 | 日本在线播放一区二区 | 亚洲电影第三页 | 日本一二区视频 | 天堂三级 | 亚洲精品成人网 | 99精品国产一区二区三区 | 亚洲成人免费视频在线观看 | 国产在线精品一区二区 | 视频在线一区 | 亚洲精品不卡 | 国产精产国品一二三产区视频 | 亚洲一区不卡在线 | 亚洲精品一区二区在线观看 | 久久99视频免费观看 | 国产综合欧美 | 99精品久久 | 亚洲永久免费观看 | 一区二区三区福利视频 | 激情五月婷婷丁香 | 黄色大片在线免费观看 | 午夜激情影院 | 一区二区日本 | 久草网免费 | 日韩影院一区 | 日本精品一区二区三区四区 |