#include // TFT 显示屏库 TFT_eSPI tft = TFT_eSPI(); // 初始化 TFT 实例 const int screenWidth = 240; // 根据你的屏幕分辨率设置 const int screenHeight = 320; const int byteWidth = (screenWidth + 7) / 8; // 每行需要的字节数 // 数组用于存储二值化后的屏幕数据 uint8_t binaryImage[screenHeight][byteWidth]; // 将屏幕数据读取并二值化,存储到数组中 void binaryScreenToArray() { for (int y = 0; y < screenHeight; y++) { uint8_t byteValue = 0; for (int x = 0; x < screenWidth; x++) { // 读取像素颜色 uint16_t color = tft.readPixel(x, y); // 将颜色二值化(假设TFT_WHITE为白色,其它为黑色) if (color == TFT_WHITE) { byteValue |= (1 << (7 - (x % 8))); // 设置当前位为1(白色) } // 每8个像素存储为一个字节 if (x % 8 == 7 || x == screenWidth - 1) { binaryImage[y][x / 8] = byteValue; byteValue = 0; } } } } // 将二值化后的屏幕数据通过串口发送到电脑,值之间用逗号隔开 void sendArrayToPC() { Serial.println("Start sending binary screen data:"); for (int y = 0; y < screenHeight; y++) { for (int x = 0; x < byteWidth; x++) { Serial.print(binaryImage[y][x], HEX); // 以十六进制格式发送每个字节 // 如果不是最后一个字节,则添加逗号 if (x < byteWidth - 1) { Serial.print(","); // 用逗号分隔 } } Serial.println(); // 每行结束后换行 } Serial.println("Binary screen data sent."); } void setup() { Serial.begin(115200); // 初始化串口通信 tft.init(); // 初始化屏幕 tft.setRotation(1); // 根据需要设置屏幕的旋转 tft.fillScreen(TFT_BLACK); // 将屏幕设置为黑色 // 绘制一些内容(测试用) tft.fillCircle(120, 160, 50, TFT_WHITE); // 在屏幕中间绘制一个白色的圆圈 // 读取并二值化屏幕内容 binaryScreenToArray(); // 将二值化后的数组通过串口传输到电脑 sendArrayToPC(); } void loop() { // 主要功能在 setup 中执行,loop 中不做任何操作 }