Arduino Library – QR-code
Installatie van Arduino IDE libraries: Arduino info
Een QR-code is een bepaald type tweedimensionale streepjescode die in 1994 is ontwikkeld door Denso Wave, een dochteronderneming van het Japanse bedrijf Denso. De letters QR zijn een afkorting van Quick Response (“snel antwoord”).
De code werd ontwikkeld om snel decodeerbaar te zijn. QR-codes waren oorspronkelijk bedoeld voor het identificeren van auto-onderdelen, maar worden in Japan inmiddels algemeen gebruikt in tijdschriften, op bushaltes en op visitekaartjes. In Nederland is de code steeds vaker terug te vinden op posters en in tijdschriften.
Informatie (ENG):
QRCode
A simple library for generating QR codes in C, optimized for processing and memory constrained systems.
Features:
- Stack-based (no heap necessary; but you can use heap if you want)
- Low-memory foot print (relatively)
- Compile-time stripping of unecessary logic
- MIT License; do with this as you please
Installing
To install this library, download and save it to your Arduino libraries directory.
Rename the directory to QRCode (if downloaded from GitHub, the filename may be qrcode-master; library names may not contain the hyphen, so it must be renamed)
API
Generate a QR Code
1 2 3 4 5 6 7 |
<span class="pl-c">// The structure to manage the QR code</span> QRCode qrcode; <span class="pl-c">// Allocate a chunk of memory to store the QR code</span> <span class="pl-c1">uint8_t</span> qrcodeBytes[qrcode_getBufferSize()]; <span class="pl-en">qrcode_initText</span>(&qrcode, qrcodeBytes, <span class="pl-c1">3</span>, ECC_LOW, <span class="pl-s"><span class="pl-pds">"</span>HELLO WORLD<span class="pl-pds">"</span></span>); |
Draw a QR Code
How a QR code is used will vary greatly from project to project. For example:
- Display on an OLED screen (128×64 nicely supports 2 side-by-side version 3 QR codes)
- Print as a bitmap on a thermal printer
- Store as a BMP (or with a some extra work, possibly a PNG) on an SD card
The following example prints a QR code to the Serial Monitor (it likely will not be scannable, but is just for demonstration purposes).
1 2 3 4 5 6 7 8 9 10 |
<span class="pl-k">for</span> (uint8 y = <span class="pl-c1">0</span>; y < qrcode.size; y++) { <span class="pl-k">for</span> (uint8 x = <span class="pl-c1">0</span>; x < qrcode.<span class="pl-smi">size</span>; x++) { <span class="pl-k">if</span> (<span class="pl-c1">qrcode_getModule</span>(&qrcode, x, y) { Serial.<span class="pl-c1">print</span>(<span class="pl-s"><span class="pl-pds">"</span>**<span class="pl-pds">"</span></span>); } <span class="pl-k">else</span> { Serial.<span class="pl-c1">print</span>(<span class="pl-s"><span class="pl-pds">"</span> <span class="pl-pds">"</span></span>); } } Serial.<span class="pl-c1">print</span>(<span class="pl-s"><span class="pl-pds">"</span><span class="pl-cce">\n</span><span class="pl-pds">"</span></span>); } |
What is Version, Error Correction and Mode?
A QR code is composed of many little squares, called modules, which represent encoded data, with additional error correction (allowing partially damaged QR codes to still be read).
The version of a QR code is a number between 1 and 40 (inclusive), which indicates the size of the QR code. The width and height of a QR code are always equal (it is square) and are equal to 4 * version + 17
.
The level of error correction is a number between 0 and 3 (inclusive), or can be one of the symbolic names ECC_LOW, ECC_MEDIUM, ECC_QUARTILE and ECC_HIGH. Higher levels of error correction sacrifice data capacity, but allow a larger portion of the QR code to be damaged or unreadable.
The mode of a QR code is determined by the data being encoded. Each mode is encoded internally using a compact representation, so lower modes can contain more data.
- NUMERIC:Â numbers (
0-9
) - ALPHANUMERIC:Â uppercase letters (
A-Z
), numbers (0-9
), the space (Â
), dollar sign ($
), percent sign (%
), asterisk (*
), plus (+
), minus (-
), decimal point (.
), slash (/
) and colon (:
). - BYTE:Â any character
Data Capacities
Version | Size | Error Correction | Mode | ||
---|---|---|---|---|---|
Numeric | Alphanumeric | Byte | |||
1 | 21 x 21 | LOW | 41 | 25 | 17 |
MEDIUM | 34 | 20 | 14 | ||
QUARTILE | 27 | 16 | 11 | ||
HIGH | 17 | 10 | 7 | ||
2 | 25 x 25 | LOW | 77 | 47 | 32 |
MEDIUM | 63 | 38 | 26 | ||
QUARTILE | 48 | 29 | 20 | ||
HIGH | 34 | 20 | 14 | ||
3 | 29 x 29 | LOW | 127 | 77 | 53 |
MEDIUM | 101 | 61 | 42 | ||
QUARTILE | 77 | 47 | 32 | ||
HIGH | 58 | 35 | 24 | ||
4 | 33 x 33 | LOW | 187 | 114 | 78 |
MEDIUM | 149 | 90 | 62 | ||
QUARTILE | 111 | 67 | 46 | ||
HIGH | 82 | 50 | 34 | ||
5 | 37 x 37 | LOW | 255 | 154 | 106 |
MEDIUM | 202 | 122 | 84 | ||
QUARTILE | 144 | 87 | 60 | ||
HIGH | 106 | 64 | 44 | ||
6 | 41 x 41 | LOW | 322 | 195 | 134 |
MEDIUM | 255 | 154 | 106 | ||
QUARTILE | 178 | 108 | 74 | ||
HIGH | 139 | 84 | 58 | ||
7 | 45 x 45 | LOW | 370 | 224 | 154 |
MEDIUM | 293 | 178 | 122 | ||
QUARTILE | 207 | 125 | 86 | ||
HIGH | 154 | 93 | 64 | ||
8 | 49 x 49 | LOW | 461 | 279 | 192 |
MEDIUM | 365 | 221 | 152 | ||
QUARTILE | 259 | 157 | 108 | ||
HIGH | 202 | 122 | 84 | ||
9 | 53 x 53 | LOW | 552 | 335 | 230 |
MEDIUM | 432 | 262 | 180 | ||
QUARTILE | 312 | 189 | 130 | ||
HIGH | 235 | 143 | 98 | ||
10 | 57 x 57 | LOW | 652 | 395 | 271 |
MEDIUM | 513 | 311 | 213 | ||
QUARTILE | 364 | 221 | 151 | ||
HIGH | 288 | 174 | 119 | ||
11 | 61 x 61 | LOW | 772 | 468 | 321 |
MEDIUM | 604 | 366 | 251 | ||
QUARTILE | 427 | 259 | 177 | ||
HIGH | 331 | 200 | 137 | ||
12 | 65 x 65 | LOW | 883 | 535 | 367 |
MEDIUM | 691 | 419 | 287 | ||
QUARTILE | 489 | 296 | 203 | ||
HIGH | 374 | 227 | 155 | ||
13 | 69 x 69 | LOW | 1022 | 619 | 425 |
MEDIUM | 796 | 483 | 331 | ||
QUARTILE | 580 | 352 | 241 | ||
HIGH | 427 | 259 | 177 | ||
14 | 73 x 73 | LOW | 1101 | 667 | 458 |
MEDIUM | 871 | 528 | 362 | ||
QUARTILE | 621 | 376 | 258 | ||
HIGH | 468 | 283 | 194 | ||
15 | 77 x 77 | LOW | 1250 | 758 | 520 |
MEDIUM | 991 | 600 | 412 | ||
QUARTILE | 703 | 426 | 292 | ||
HIGH | 530 | 321 | 220 | ||
16 | 81 x 81 | LOW | 1408 | 854 | 586 |
MEDIUM | 1082 | 656 | 450 | ||
QUARTILE | 775 | 470 | 322 | ||
HIGH | 602 | 365 | 250 | ||
17 | 85 x 85 | LOW | 1548 | 938 | 644 |
MEDIUM | 1212 | 734 | 504 | ||
QUARTILE | 876 | 531 | 364 | ||
HIGH | 674 | 408 | 280 | ||
18 | 89 x 89 | LOW | 1725 | 1046 | 718 |
MEDIUM | 1346 | 816 | 560 | ||
QUARTILE | 948 | 574 | 394 | ||
HIGH | 746 | 452 | 310 | ||
19 | 93 x 93 | LOW | 1903 | 1153 | 792 |
MEDIUM | 1500 | 909 | 624 | ||
QUARTILE | 1063 | 644 | 442 | ||
HIGH | 813 | 493 | 338 | ||
20 | 97 x 97 | LOW | 2061 | 1249 | 858 |
MEDIUM | 1600 | 970 | 666 | ||
QUARTILE | 1159 | 702 | 482 | ||
HIGH | 919 | 557 | 382 | ||
21 | 101 x 101 | LOW | 2232 | 1352 | 929 |
MEDIUM | 1708 | 1035 | 711 | ||
QUARTILE | 1224 | 742 | 509 | ||
HIGH | 969 | 587 | 403 | ||
22 | 105 x 105 | LOW | 2409 | 1460 | 1003 |
MEDIUM | 1872 | 1134 | 779 | ||
QUARTILE | 1358 | 823 | 565 | ||
HIGH | 1056 | 640 | 439 | ||
23 | 109 x 109 | LOW | 2620 | 1588 | 1091 |
MEDIUM | 2059 | 1248 | 857 | ||
QUARTILE | 1468 | 890 | 611 | ||
HIGH | 1108 | 672 | 461 | ||
24 | 113 x 113 | LOW | 2812 | 1704 | 1171 |
MEDIUM | 2188 | 1326 | 911 | ||
QUARTILE | 1588 | 963 | 661 | ||
HIGH | 1228 | 744 | 511 | ||
25 | 117 x 117 | LOW | 3057 | 1853 | 1273 |
MEDIUM | 2395 | 1451 | 997 | ||
QUARTILE | 1718 | 1041 | 715 | ||
HIGH | 1286 | 779 | 535 | ||
26 | 121 x 121 | LOW | 3283 | 1990 | 1367 |
MEDIUM | 2544 | 1542 | 1059 | ||
QUARTILE | 1804 | 1094 | 751 | ||
HIGH | 1425 | 864 | 593 | ||
27 | 125 x 125 | LOW | 3517 | 2132 | 1465 |
MEDIUM | 2701 | 1637 | 1125 | ||
QUARTILE | 1933 | 1172 | 805 | ||
HIGH | 1501 | 910 | 625 | ||
28 | 129 x 129 | LOW | 3669 | 2223 | 1528 |
MEDIUM | 2857 | 1732 | 1190 | ||
QUARTILE | 2085 | 1263 | 868 | ||
HIGH | 1581 | 958 | 658 | ||
29 | 133 x 133 | LOW | 3909 | 2369 | 1628 |
MEDIUM | 3035 | 1839 | 1264 | ||
QUARTILE | 2181 | 1322 | 908 | ||
HIGH | 1677 | 1016 | 698 | ||
30 | 137 x 137 | LOW | 4158 | 2520 | 1732 |
MEDIUM | 3289 | 1994 | 1370 | ||
QUARTILE | 2358 | 1429 | 982 | ||
HIGH | 1782 | 1080 | 742 | ||
31 | 141 x 141 | LOW | 4417 | 2677 | 1840 |
MEDIUM | 3486 | 2113 | 1452 | ||
QUARTILE | 2473 | 1499 | 1030 | ||
HIGH | 1897 | 1150 | 790 | ||
32 | 145 x 145 | LOW | 4686 | 2840 | 1952 |
MEDIUM | 3693 | 2238 | 1538 | ||
QUARTILE | 2670 | 1618 | 1112 | ||
HIGH | 2022 | 1226 | 842 | ||
33 | 149 x 149 | LOW | 4965 | 3009 | 2068 |
MEDIUM | 3909 | 2369 | 1628 | ||
QUARTILE | 2805 | 1700 | 1168 | ||
HIGH | 2157 | 1307 | 898 | ||
34 | 153 x 153 | LOW | 5253 | 3183 | 2188 |
MEDIUM | 4134 | 2506 | 1722 | ||
QUARTILE | 2949 | 1787 | 1228 | ||
HIGH | 2301 | 1394 | 958 | ||
35 | 157 x 157 | LOW | 5529 | 3351 | 2303 |
MEDIUM | 4343 | 2632 | 1809 | ||
QUARTILE | 3081 | 1867 | 1283 | ||
HIGH | 2361 | 1431 | 983 | ||
36 | 161 x 161 | LOW | 5836 | 3537 | 2431 |
MEDIUM | 4588 | 2780 | 1911 | ||
QUARTILE | 3244 | 1966 | 1351 | ||
HIGH | 2524 | 1530 | 1051 | ||
37 | 165 x 165 | LOW | 6153 | 3729 | 2563 |
MEDIUM | 4775 | 2894 | 1989 | ||
QUARTILE | 3417 | 2071 | 1423 | ||
HIGH | 2625 | 1591 | 1093 | ||
38 | 169 x 169 | LOW | 6479 | 3927 | 2699 |
MEDIUM | 5039 | 3054 | 2099 | ||
QUARTILE | 3599 | 2181 | 1499 | ||
HIGH | 2735 | 1658 | 1139 | ||
39 | 173 x 173 | LOW | 6743 | 4087 | 2809 |
MEDIUM | 5313 | 3220 | 2213 | ||
QUARTILE | 3791 | 2298 | 1579 | ||
HIGH | 2927 | 1774 | 1219 | ||
40 | 177 x 177 | LOW | 7089 | 4296 | 2953 |
MEDIUM | 5596 | 3391 | 2331 | ||
QUARTILE | 3993 | 2420 | 1663 | ||
HIGH | 3057 | 1852 | 1273 |
Download @ https://github.com/ricmoo/QRCode
DL: QRCode-0.0.1
Arduino voorbeeld:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include "qrcode.h" void setup() { Serial.begin(9600); // Start time uint32_t dt = millis(); // Create the QR code QRCode qrcode; uint8_t qrcodeData[qrcode_getBufferSize(3)]; qrcode_initText(&qrcode, qrcodeData, 3, 0, "HALLO WERELD!"); // Delta time dt = millis() - dt; Serial.print("QR Code Generation Time: "); Serial.print(dt); Serial.print("\n"); for (uint8_t y = 0; y < qrcode.size; y++) { // Each horizontal module for (uint8_t x = 0; x < qrcode.size; x++) { Serial.print(qrcode_getModule(&qrcode, x, y) ? "1": "0"); } Serial.print("\n"); } } void loop() { delay(50); } |
Op de console zie je nu nullen en enen, de enen zijn een zwart (segment)blokje, de nullen een witte
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
QR Code Generation Time: 881 11111110111101110111001111111 10000010000111110010101000001 10111010010101100000101011101 10111010001100110011001011101 10111010001111010000101011101 10000010010101100000101000001 11111110101010101010101111111 00000000110010100111100000000 11011010010011111001001000001 00101101101000100010000011001 10111111010010100111110110100 00011000100000110101111011001 10011110111001100110011111100 01010000111010000101100110000 00110110110000110101100010101 11000100100011101110100100111 10101011110110010100101110010 10000100010100000110101010001 11101111011011101110111011101 11011100000101011000010110100 11111011110101000010111110010 00000000110011001101100011101 11111110001101011000101011000 10000010011111001010100011011 10111010110111011101111111100 10111010101001101010100010100 10111010011111001011001110011 10000010111100010000110001101 11111110110000101111011111100 |
Op internet is ook een voorbeeld te vinden om een QR-code op een TFT scherm te zetten:
https://github.com/LastxTemplar/arduino_qr_code_generator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
int powerTimer; /********************** | SCREEN | **********************/ #include <TFT.h> // Arduino LCD library #include <SPI.h> #define cs 10 #define dc 9 #define rst 8 TFT TFTscreen = TFT(cs, dc, rst); /********************** | DS3231 | **********************/ #include <DS3231.h> DS3231 rtc(SDA, SCL); // Pins A4 and A5 String strDate; String strTime; char dateCharArray[11]; // dd.mm.yyyy char timeCharArray[7]; // xx:xx:xx /********************** | QR Code | **********************/ #include "qrcode.h" QRCode qrcode; void setup() { Serial.begin(115200); powerTimer = 10; /********************** | 5V SOURCE | **********************/ pinMode(4, OUTPUT); // 5V digitalWrite(4, HIGH); /********************** | RELAY / LED | **********************/ pinMode(7, OUTPUT); // Relay pinMode(2, OUTPUT); // LED digitalWrite(7, LOW); digitalWrite(2, HIGH); /********************** | SCREEN | **********************/ TFTscreen.begin(); // Rotate the screen TFTscreen.setRotation(2); // clear the screen with a white background TFTscreen.background(255,255,255); TFTscreen.setTextSize(2); TFTscreen.stroke(0,0,0); TFTscreen.fill(0,0,0); /********************** | DS3231 | **********************/ rtc.begin(); // The following lines can be uncommented to set the date and time // You need to do this just once, the battery on the DS3231 will help remember the setting. // ----- // rtc.setDOW(WEDNESDAY); // Set Day-of-Week to SUNDAY // rtc.setTime(23, 14, 0); // Set the time to 12:00:00 (24hr format) // rtc.setDate(18, 04, 2018); // Set the date to 18 April 2018 // Set Initial Time strDate = String(rtc.getDateStr()); strTime = String(rtc.getTimeStr()); strDate.toCharArray(dateCharArray, 11); strTime.toCharArray(timeCharArray, 6); TFTscreen.text(dateCharArray,0,0); TFTscreen.text(timeCharArray,0,20); /********************** | QR Code | **********************/ // Print First Time uint8_t qrcodeData[qrcode_getBufferSize(3)]; qrcode_initText(&qrcode, qrcodeData, 3, 0, dateCharArray); // your text in last parameter, e.g. "Hello World" for (uint8_t y = 0; y < qrcode.size; y++) { // Each horizontal module for (uint8_t x = 0; x < qrcode.size; x++) { if(qrcode_getModule(&qrcode, x, y)) { TFTscreen.rect(5+(x*4), 40+(y*4), 4, 4); } } } } void loop() { strDate = String(rtc.getDateStr()); strTime = String(rtc.getTimeStr()); if(powerTimer==0) { digitalWrite(2, LOW); // TURN OFF LED digitalWrite(7, HIGH); // POWER OFF ARDUINO } // Overwriting the date and time on screen but dont touch the qr code if(strTime.substring(6)=="00" && powerTimer>0) { // Remove previous value TFTscreen.stroke(255,255,255); TFTscreen.text(dateCharArray,0,0); TFTscreen.text(timeCharArray,0,20); strDate.toCharArray(dateCharArray, 11); strTime.toCharArray(timeCharArray, 6); // Set new value TFTscreen.stroke(0,0,0); TFTscreen.text(dateCharArray,0,0); TFTscreen.text(timeCharArray,0,20); } powerTimer = powerTimer - 1; delay (1000); } |