ESP32-CAM获取的图像显示在OLED上面

ESP32CAM图像显示在1.3寸240*240的彩屏上面
ESP32CAM使用串口将图像传到上位机里面显示

 把ESP32-cam获取的数据显示在0.96inch的OLED上面如下图:

在这里插入图片描述在这里插入图片描述

IIC - SSD1306
ESP32-cam IIC 引脚:
SCL:PIN14
SDA:PIN15

#include "esp_camera.h"
#include "SSD1306.h"

// clk:14; sda:15
SSD1306 display(0x3c, 15, 14);

#define SCREEN_WIDTH 128 // OLED显示宽度,以像素为单位
#define SCREEN_HEIGHT 64 

#define PWDN_GPIO_NUM     32
#define RESET_GPIO_NUM    -1
#define XCLK_GPIO_NUM      0
#define SIOD_GPIO_NUM     26
#define SIOC_GPIO_NUM     27
#define Y9_GPIO_NUM       35
#define Y8_GPIO_NUM       34
#define Y7_GPIO_NUM       39
#define Y6_GPIO_NUM       36
#define Y5_GPIO_NUM       21
#define Y4_GPIO_NUM       19
#define Y3_GPIO_NUM       18
#define Y2_GPIO_NUM        5
#define VSYNC_GPIO_NUM    25
#define HREF_GPIO_NUM     23
#define PCLK_GPIO_NUM     22

static camera_config_t camera_config = {
	.pin_pwdn = PWDN_GPIO_NUM,
	.pin_reset = RESET_GPIO_NUM,
	.pin_xclk = XCLK_GPIO_NUM, 
	.pin_sscb_sda = SIOD_GPIO_NUM,  
	.pin_sscb_scl = SIOC_GPIO_NUM,
	.pin_d7 = Y9_GPIO_NUM,
	.pin_d6 = Y8_GPIO_NUM,
	.pin_d5 = Y7_GPIO_NUM, 
	.pin_d4 = Y6_GPIO_NUM,
	.pin_d3 = Y5_GPIO_NUM,
	.pin_d2 = Y4_GPIO_NUM,  
	.pin_d1 = Y3_GPIO_NUM,
	.pin_d0 = Y2_GPIO_NUM,
	.pin_vsync = VSYNC_GPIO_NUM,
	.pin_href = HREF_GPIO_NUM,
	.pin_pclk = PCLK_GPIO_NUM,
	
	.xclk_freq_hz = 20000000,
	.ledc_timer = LEDC_TIMER_0,
	.ledc_channel = LEDC_CHANNEL_0,
	
	.pixel_format = PIXFORMAT_GRAYSCALE,
	.frame_size = FRAMESIZE_QQVGA2,
	.jpeg_quality = 12,
	.fb_count = 1,
};

byte x,y;
uint8_t oldpixel, newpixel;
int quanterror;

esp_err_t camera_init()
{
	//initialize the camera
	esp_err_t err = esp_camera_init(&camera_config);
	if (err != ESP_OK) 
	{
		Serial.print("Camera Init Failed");
		return err;
	}
	sensor_t * s = esp_camera_sensor_get();
	//initial sensors are flipped vertically and colors are a bit saturated
	if (s->id.PID == OV2640_PID) 
	{
		s->set_vflip(s, 1);//flip it back
		s->set_brightness(s, 1);//up the blightness just a bit
		s->set_contrast(s, 1);
	}
	Serial.print("Camera Init OK");
	return ESP_OK;
}

void my_camera_show(void)
{
	camera_fb_t *fb = esp_camera_fb_get();
	int i = 0;
	for(y = 0; y < SCREEN_HEIGHT; y++)
	{
		for(x = 0;x < SCREEN_WIDTH; x++)
		{
			oldpixel = fb->buf[i]; //保持原始的灰度值
			newpixel = (255*(oldpixel >> 7)); //门槛值128
			fb->buf[i] = newpixel; //缓冲区现在是单通道,0或255
			// floyd-steignburg抖动算法
			quanterror = oldpixel - newpixel; //像素之间的误差
			//将此错误分发给相邻像素:
			//右
			if(x < SCREEN_WIDTH-1)//边界检查...
			{
				fb->buf[(x + 1)+(y * SCREEN_WIDTH)] += ((quanterror * 7)>> 4);
			}
			// 左下
			if((x > 1) && (y < SCREEN_HEIGHT-1))//边界检查...
			{
				fb->buf [(x-1)+((y + 1)* SCREEN_WIDTH)] == ((quanterror * 3)>> 4); 
			}
			//下
			if(y < 63)//边界检查...
			{
				fb->buf [(x)+((y + 1)* SCREEN_WIDTH)] == ((quanterror * 5)>> 4);
			}
			// 右下
			if((x < SCREEN_WIDTH-1) && (y < SCREEN_HEIGHT-1))//边界检查...
			{
				fb->buf [(x + 1) + ((y + 1)* SCREEN_WIDTH)] == (quanterror >> 4);
			}
			// 画这个像素
			switch(fb->buf[i]%2)
			{
			case 0:
				display.setColor(BLACK);
				break;
			case 1:
				display.setColor(WHITE);
				break;
			case 2:
				display.setColor(INVERSE);
				break;
			}
			display.setPixel(x, y);
			i++;
		}
	}
	display.display();
	esp_camera_fb_return(fb);
}


void setup() 
{
	Serial.begin(115200);
	display.init();
	camera_init();
	Serial.setDebugOutput(true);
	Serial.println();
	Serial.print("sys is 1...");
	Serial.print("sys is running!");
}

void loop() 
{
   my_camera_show();
}
  • 28
    点赞
  • 180
    收藏
    觉得还不错? 一键收藏
  • 23
    评论
评论 23
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值