Arduino -- 使用ESP32生成二维码并显示(带反显设置)

目录

一 前言

二 开发环境

Arduino IDE

芯片 ESP32-WROOM

三 所需库

QRcode

U8g2

四 实现过程

避坑

效果

关于反显二维码

接口

代码

完整项目


一 前言

最近需要实现一个使用二维码进行显示的项目,记录一下使用和实现过程,方便后面复习查看,也提供给大家进行查考

二 开发环境

Arduino IDE

芯片 ESP32-WROOM

 所需环境的搭建可以参考我之前发布的文章

 Arduino IDE 使用安装以及ESP32库的导入(离线)icon-default.png?t=N7T8https://blog.csdn.net/herui_2/article/details/135296814?spm=1001.2014.3001.5502

三 所需库

QRcode

Github 链接icon-default.png?t=N7T8https://github.com/ricmoo/QRCode

首先先下载库的压缩包

首先先解压我们的下载的压缩包,找到src文件夹,里面的文件就是我们需要用到的文件

 将这两个文件放到我们需要生成二维码项目的文件夹里面即可

目录如下图所示,我们的项目就可以直接使用二维码库了。

U8g2

U8g2库在我们的项目中直接安装即可了

四 实现过程

避坑

一定不要采用双拼色屏幕去做二维码!!

一定不要采用双拼色屏幕去做二维码!!

一定不要采用双拼色屏幕去做二维码!!

因为双拼屏幕中间有条杠,二维码显示不完全;

效果

关于反显二维码

我在网上没有找到相关资料,所以自己想了一下,我自己画一个白色背景不就行了哈哈,结果还是舒适的,主要加了这段代码,以及修改了二维码的黑白反向

u8g2.drawBox(0, 0, 128, 64);  //画箱

 代码如下

// 显示二维码
void QR_Code() {
  // 二维码
  QRCode qrcode;
  uint8_t qrcodeData[qrcode_getBufferSize(3)];
  qrcode_initText(&qrcode, qrcodeData, 3, ECC_LOW, "HX125458726");
  // start draw
  u8g2.firstPage();
  do {
    // get the draw starting point,128 and 64 is screen size
    uint8_t x0 = (128 - qrcode.size * 2) / 2;
    uint8_t y0 = (64 - qrcode.size * 2) / 2;
    // u8g2.setDrawColor(1); //
    u8g2.drawBox(0, 0, 128, 64);  //画箱
    // get QR code pixels in a loop
    for (uint8_t y = 0; y < qrcode.size; y++) {
      for (uint8_t x = 0; x < qrcode.size; x++) {
        // Check this point is black or white
        if (qrcode_getModule(&qrcode, x, y)) {
          u8g2.setColorIndex(0);
        } else {
          u8g2.setColorIndex(1);
        }
        // Double the QR code pixels
        u8g2.drawPixel(x0 + x * 2, y0 + y * 2);
        u8g2.drawPixel(x0 + 1 + x * 2, y0 + y * 2);
        u8g2.drawPixel(x0 + x * 2, y0 + 1 + y * 2);
        u8g2.drawPixel(x0 + 1 + x * 2, y0 + 1 + y * 2);
      }
    }

  } while (u8g2.nextPage());
}

接口

 OLED显示屏接口如下

代码

 代码编写如下

#include <U8g2lib.h>
// OLED
#include <WiFi.h>
// 二维码所需库
#include "qrcode.h"
// 显示屏 配置
#define SCL 22
#define SDA 21
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, SCL, SDA, /*reset=*/U8X8_PIN_NONE);
char str[50];  //拼接字符使用

void setup() {  
  Serial.begin(9600);  // 启动串口通讯
  //OELD
  u8g2.begin();
  u8g2.setFont(u8g2_font_ncenB08_tr);  //设定字体
  QR_Code(); // 生成

}
// 显示二维码
void QR_Code() {
  // 二维码
  QRCode qrcode;
  uint8_t qrcodeData[qrcode_getBufferSize(3)];
  qrcode_initText(&qrcode, qrcodeData, 3, ECC_LOW, "https://blog.csdn.net/herui_2?spm=1000.2115.3001.5343");
  // start draw
  u8g2.firstPage();
  do {
    // get the draw starting point,128 and 64 is screen size
    uint8_t x0 = (128 - qrcode.size * 2) / 2;
    uint8_t y0 = (64 - qrcode.size * 2) / 2;

    // get QR code pixels in a loop
    for (uint8_t y = 0; y < qrcode.size; y++) {
      for (uint8_t x = 0; x < qrcode.size; x++) {
        // Check this point is black or white
        if (qrcode_getModule(&qrcode, x, y)) {
          u8g2.setColorIndex(1);
        } else {
          u8g2.setColorIndex(0);
        }
        // Double the QR code pixels
        u8g2.drawPixel(x0 + x * 2, y0 + y * 2);
        u8g2.drawPixel(x0 + 1 + x * 2, y0 + y * 2);
        u8g2.drawPixel(x0 + x * 2, y0 + 1 + y * 2);
        u8g2.drawPixel(x0 + 1 + x * 2, y0 + 1 + y * 2);
      }
    }

  } while (u8g2.nextPage());
}

void loop() {


}

完整项目

链接:https://pan.baidu.com/s/1cfNZIBarf7X5ryMF7b8zbQ?pwd=XZY0
提取码:XZY0

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

阿柒学起来

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

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

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

打赏作者

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

抵扣说明:

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

余额充值