# esp32 电子墨水屏 **Repository Path**: xiao-si-qi/esp32-ePaper ## Basic Information - **Project Name**: esp32 电子墨水屏 - **Description**: 使用ESP32驱动7.5寸电子墨水屏 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 12 - **Forks**: 7 - **Created**: 2022-01-12 - **Last Updated**: 2025-10-02 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # ESP32电子墨水屏 ## 简介 通过ESP32连接WIFI从服务器获取转换好的图片字节数据,显示在在墨水屏上。 硬件是咸鱼购入,店家提供的资料: [7.5寸墨水屏天气站资料](https://gitee.com/corogoo/epaper-7.5-weather-station/blob/master/README.md) [屏幕资料](https://www.waveshare.net/wiki/7.5inch_e-Paper_HAT_(B)) ## 实现原理 由于本人不擅长c/c++,故将图片转换算法由java实现,java首先将彩色图片每个像素的RGB值转换为0-255的灰度像素存储到二维数组中,再转换为三色墨水屏支持的色深为2的图像。 通过Java处理图片,可以自由在图片上叠加文字,实现动态内容。 ## 传输图像格式 两个二进制位表示一个像素: 00表示黑色 ,11表示白色,10或01表示红色,故一个字节可表示4个像素,本项目的墨水屏的分辨率为384x640,所以表示一张图像需要长度为61440的byte数组。 传输的图片分辨率也应固定为384x640,像素不对将影响显示效果。 ## java图片转换示例代码 ```java @RestController @RequestMapping("/img") public class EPaperController { //转换明暗阈值 final static Integer LUM=160; @GetMapping("/getImg") public byte[] getMenu() throws IOException { return getImgData(); } /** * 将图片转为灰度模式 * @param filePath 图片路径 * @return int[][] */ public int[][] toGrayscaleImage(String filePath) { File file = new File(filePath); int[][] result = null; if (!file.exists()) { return result; } try { BufferedImage bufImg = ImageIO.read(file); int height = bufImg.getHeight(); int width = bufImg.getWidth(); result = new int[height][width]; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { int r = (bufImg.getRGB(i, j) & 0xFF0000) >> 16; int g = (bufImg.getRGB(i, j) & 0x00FF00) >> 8; int b = (bufImg.getRGB(i, j) & 0x0000FF); int v = (int) ((0.2125 * r) + (0.7154 * g) + (0.0721 * b)); result[j][i] = v; } } } catch (IOException e) { e.printStackTrace(); } return result; } /** * 将灰度图像转换为水墨屏支持的图像 * @return byte[] */ public byte[] getImgData(){ byte[] bytes = new byte[61440]; int[][] image = toGrayscaleImage("C:\\Users\\xiaos\\Desktop\\6e203344947b3e532eaf017590ee3416.png"); int index=0; for (int i = 0; i < image.length; i++) { for (int j = 0; j < image[i].length; j += 4) { int xs; if (image[i][j + 3] > LUM) { xs = 0xC0; } else { xs = 0x00; } xs = xs >> 2; if (image[i][j + 2] > LUM) { xs |= 0xC0; } else { xs |= 0x00; } xs = xs >> 2; if (image[i][j + 1] > LUM) { xs |= 0xC0; } else { xs |= 0x00; } xs = xs >> 2; if (image[i][j] > LUM) { xs |= 0xC0; } else { xs |= 0x00; } bytes[index++]= (byte) xs; } } return bytes; } } ``` ## 显示效果 ![图片1](img/1.jpg) ![图片2](img/2.jpg)