标题: QRcode二维码生成显示 [打印本页]

作者: roc2    时间: 2018-7-31 16:39
标题: QRcode二维码生成显示
在我们生活中随处可见二维码,我们在零知板上使用软件库来生成并显示二维码。
硬件:零知-标准板、OLED(最好用像素更大的LCD)
连线:连线请参照OLED模块使用教程进行。

程序源码如下:
  1. /**
  2. *  QRCode 零知板-二维码生成与显示
  3. * 2018年7月31日16:23:41
  4. *        by 零知实验室
  5. *
  6. */

  7. #include <qrcode.h>
  8. #include <Adafruit_SSD1306.h>

  9. #define OLED_RESET 4
  10. Adafruit_SSD1306 display(OLED_RESET);

  11. void setup() {
  12.     Serial.begin(9600);
  13.         
  14.         display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  15.         display.clearDisplay();

  16.     // Start time
  17.     uint32_t dt = millis();
  18.   
  19.     // 根据字符串生成二维码
  20.     QRCode qrcode;
  21.     uint8_t qrcodeData[qrcode_getBufferSize(3)];
  22.     qrcode_initText(&qrcode, qrcodeData, 3, 0, "http://www.lingzhilab.com");
  23.   
  24.     // Delta time
  25.     dt = millis() - dt;
  26.     Serial.print("QR Code Generation Time: ");
  27.     Serial.print(dt);
  28.     Serial.print("\n");

  29.     // Top quiet zone
  30.     Serial.print("\n\n\n\n");
  31.         

  32.         //显示到OLED上
  33.     for (uint8_t y = 0; y < qrcode.size; y++) {

  34.         // Left quiet zone
  35.         Serial.print("        ");

  36.         // Each horizontal module
  37.         for (uint8_t x = 0; x < qrcode.size; x++) {

  38.             // Print each module (UTF-8 \u2588 is a solid block)
  39.             //Serial.print(qrcode_getModule(&qrcode, x, y) ? "\u2588\u2588": "  ");
  40.                         
  41.                         if(qrcode_getModule(&qrcode,x,y)){
  42.                                 display.drawPixel(x, y, WHITE);
  43.                         }else{
  44.                                 display.drawPixel(x, y, BLACK);
  45.                         }

  46.         }

  47.         Serial.print("\n");
  48.     }
  49.         
  50.         display.display();

  51.     // Bottom quiet zone
  52.     Serial.print("\n\n\n\n");
  53. }

  54. void loop() {

  55. }
复制代码

最后在OLED上可以看到我们生成的二维码:







欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1