• 方案介紹
    • 一 前言
    • 二 開發(fā)環(huán)境
    • 三 所需庫
    • 四 實現(xiàn)過程
  • 附件下載
  • 推薦器件
  • 相關(guān)推薦
申請入駐 產(chǎn)業(yè)圖譜

Arduino-使用ESP32生成二維碼并顯示(帶反顯設(shè)置)

2024/03/20
5665
加入交流群
掃碼加入
獲取工程師必備禮包
參與熱點資訊討論

Arduino-使用ESP32生成二維碼并顯示(帶反顯設(shè)置).docx

共1個文件

一 前言

最近需要實現(xiàn)一個使用二維碼進行顯示的項目,記錄一下使用和實現(xiàn)過程,方便后面復(fù)習(xí)查看,也提供給大家進行查考

二 開發(fā)環(huán)境

Arduino IDE

芯片 ESP32-WROOM

?所需環(huán)境的搭建可以參考我之前發(fā)布的文章

Arduino IDE 使用安裝以及ESP32庫的導(dǎo)入(離線)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庫在我們的項目中直接安裝即可了

四 實現(xiàn)過程

避坑

一定不要采用雙拼色屏幕去做二維碼??!

一定不要采用雙拼色屏幕去做二維碼??!

一定不要采用雙拼色屏幕去做二維碼?。?/p>

因為雙拼屏幕中間有條杠,二維碼顯示不完全;

效果

關(guān)于反顯二維碼

我在網(wǎng)上沒有找到相關(guān)資料,所以自己想了一下,我自己畫一個白色背景不就行了哈哈,結(jié)果還是舒適的,主要加了這段代碼,以及修改了二維碼的黑白反向

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);  //設(shè)定字體
  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

  • Arduino-使用ESP32生成二維碼并顯示(帶反顯設(shè)置).docx
    下載

推薦器件

更多器件
器件型號 數(shù)量 器件廠商 器件描述 數(shù)據(jù)手冊 ECAD模型 風(fēng)險等級 參考價格 更多信息
AT89C51CC03UA-RDTUM 1 Atmel Corporation Microcontroller, 8-Bit, FLASH, 8051 CPU, 60MHz, CMOS, PQFP64, GREEN, VQFP-64

ECAD模型

下載ECAD模型
$9.5 查看
ATXMEGA128A4U-MHR 1 Atmel Corporation RISC Microcontroller, 16-Bit, FLASH, AVR RISC CPU, 32MHz, CMOS, PQCC44, 7 X 7 MM, 1 MM HEIGHT, 0.50 MM PITCH, GREEN, PLASTIC, MO-220VKKD-3, VQFN-44
$4.48 查看
STM32F745IGT6 1 STMicroelectronics High-performance and DSP with FPU, Arm Cortex-M7 MCU with 1 Mbyte of Flash memory, 216 MHz CPU, Art Accelerator, L1 cache, SDRAM

ECAD模型

下載ECAD模型
$16.29 查看

相關(guān)推薦

方案定制

去合作
方案開發(fā)定制化,2000+方案商即時響應(yīng)!

方案定制,程序設(shè)計方案、單片機程序設(shè)計與講解、APP定制開發(fā)。本公眾號致力于向讀者傳遞關(guān)于程序設(shè)計和開發(fā)的相關(guān)知識,并分享一些關(guān)于軟件開發(fā)的最佳實踐。如果您有什么問題或建議,請隨時聯(lián)系我們。我們將竭誠為您服務(wù)