esp12f + tft 显示图片问题

这篇博客介绍了在使用 TFT_eSPI 和 TJpg_Decoder 库显示 JPEG 图片时遇到 ESP8266 强制复位的故障。作者通过调试发现 TJpg_Decoder 库仅负责解码,不直接处理显示,并提供了一个回调函数 tft_output() 来将解码后的位图推送到屏幕。通过在 setup() 函数中注册该回调,成功解决了复位问题。代码示例展示了如何完整显示 JPEG 图片并测量渲染时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述

 #include <TFT_eSPI.h>             
#include <TJpg_Decoder.h>

在使用这两库时候,发现图片虽然显示后,但会强制esp8266复位,一直没找到原因,经过一次一次测试,发现TJpg_Decoder仅仅是一个tjpg图片解码的库,并不能之间显示图片,而且库中提供了一个回调函数,来调用屏幕显示位图的函数。

解决

注意这个函数

bool tft_output(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t *bitmap)
{
  if (y >= tft.height())
    return 0;
  tft.pushImage(x, y, w, h, bitmap);
  // Return 1 to decode next block
  return 1;
}

在setup中添加这个回调函数即可
下面就是全部代码:
只需要将图片换成自己的就可以完整的显示一个图片。

#include <Arduino.h>
#include <TFT_eSPI.h>                 // Include the graphics library (this includes the sprite functions)
#include <TJpg_Decoder.h>
#include<EEPROM.h>
TFT_eSPI    tft = TFT_eSPI();         // Create object "tft"
TFT_eSprite img = TFT_eSprite(&tft);  // Create Sprite object "img" with pointer to "tft" object

#include "../include/jpeg/jpeg1.h"
#define LCD_BC 5  // 这个是我的硬件调节背光的IO,大家可以自己忽略
// TFT屏幕输出函数
bool tft_output(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t *bitmap)
{
  if (y >= tft.height())
    return 0;
  tft.pushImage(x, y, w, h, bitmap);
  // Return 1 to decode next block
  return 1;
}


void setup()
{
  pinMode(LCD_BC,OUTPUT);// 这个是我的硬件调节背光的IO,大家可以自己忽略
  analogWrite(LCD_BC,0);// 这个是我的硬件调节背光的IO,大家可以自己忽略
  Serial.begin(115200);
  tft.begin();
  tft.fillScreen(TFT_BLUE);

  // The jpeg image can be scaled by a factor of 1, 2, 4, or 8
  TJpgDec.setJpgScale(1);
  TJpgDec.setSwapBytes(true);
  TJpgDec.setCallback(tft_output);


}

void loop()
{
  // Time recorded for test purposes
  uint32_t t = millis();

  // Get the width and height in pixels of the jpeg if you wish
  uint16_t w = 0, h = 0;
  TJpgDec.getJpgSize(&w, &h, panda, sizeof(panda));
  Serial.print("Width = "); Serial.print(w); Serial.print(", height = "); Serial.println(h);

  // Draw the image, top left at 0,0
  TJpgDec.drawJpg(0, 0, panda, sizeof(panda));

  // How much time did rendering take (ESP8266 80MHz 473ms, 160MHz 266ms, ESP32 SPI 116ms)
  t = millis() - t;
  Serial.print(t); Serial.println(" ms");

  // Wait before drawing again
  delay(2000);
}

tft.pushImage 是用于在TFT显示屏上显示图像的一个函数,通常在嵌入式系统中使用,如Arduino平台。该函数的主要作用是将图像数据从内存推送到TFT显示屏上,从而在屏幕上显示图像。以下是该函数的一些关键点: 1. **参数**tft.pushImage 函数通常需要几个参数,包括图像的起始坐标(x, y),图像的宽度和高度,以及指向图像数据的指针。 2. **图像数据**:图像数据通常以字节数组的形式存储,每个像素的颜色信息需要按照特定的格式进行编码。 3. **颜色格式**:不同的TFT显示屏可能支持不同的颜色格式,如16位颜色(RGB565)或24位颜色(RGB888)。在调用tft.pushImage之前,需要确保图像数据与显示屏的颜色格式匹配。 4. **性能**:由于图像数据量可能较大,直接推送大量数据到显示屏可能会影响性能。因此,通常需要对图像数据进行优化,如使用压缩格式或分块推送。 以下是一个简单的示例代码,展示了如何使用tft.pushImage函数在TFT显示屏上显示一幅图像: ```cpp #include <TFT.h> // 假设使用一个通用的TFT库 #define TFT_CS 10 #define TFT_RST 9 #define TFT_DC 8 TFT tft = TFT(TFT_CS, TFT_DC, TFT_RST); void setup() { tft.begin(); tft.fillScreen(TFT_BLACK); // 假设我们有一幅宽度为100,高度为100,颜色格式为RGB565的图像 uint16_t image[100 * 100] = { /* 图像数据 */ }; // 显示图像,起始坐标为(20, 20) tft.pushImage(20, 20, 100, 100, image); } void loop() { // 主循环中不需要做任何事情 } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

おもいね

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值