• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * QR Code generator demo (C++)
3  *
4  * Run this command-line program with no arguments. The program computes a bunch of demonstration
5  * QR Codes and prints them to the console. Also, the SVG code for one QR Code is printed as a sample.
6  *
7  * Copyright (c) Project Nayuki. (MIT License)
8  * https://www.nayuki.io/page/qr-code-generator-library
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a copy of
11  * this software and associated documentation files (the "Software"), to deal in
12  * the Software without restriction, including without limitation the rights to
13  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
14  * the Software, and to permit persons to whom the Software is furnished to do so,
15  * subject to the following conditions:
16  * - The above copyright notice and this permission notice shall be included in
17  *   all copies or substantial portions of the Software.
18  * - The Software is provided "as is", without warranty of any kind, express or
19  *   implied, including but not limited to the warranties of merchantability,
20  *   fitness for a particular purpose and noninfringement. In no event shall the
21  *   authors or copyright holders be liable for any claim, damages or other
22  *   liability, whether in an action of contract, tort or otherwise, arising from,
23  *   out of or in connection with the Software or the use or other dealings in the
24  *   Software.
25  */
26 
27 #include <cstdint>
28 #include <cstdlib>
29 #include <cstring>
30 #include <iostream>
31 #include <string>
32 #include <vector>
33 #include "QrCode.hpp"
34 
35 using std::uint8_t;
36 using qrcodegen::QrCode;
37 using qrcodegen::QrSegment;
38 
39 
40 // Function prototypes
41 static void doBasicDemo();
42 static void doVarietyDemo();
43 static void doSegmentDemo();
44 static void doMaskDemo();
45 static void printQr(const QrCode &qr);
46 
47 
48 // The main application program.
main()49 int main() {
50 	doBasicDemo();
51 	doVarietyDemo();
52 	doSegmentDemo();
53 	doMaskDemo();
54 	return EXIT_SUCCESS;
55 }
56 
57 
58 
59 /*---- Demo suite ----*/
60 
61 // Creates a single QR Code, then prints it to the console.
doBasicDemo()62 static void doBasicDemo() {
63 	const char *text = "Hello, world!";              // User-supplied text
64 	const QrCode::Ecc errCorLvl = QrCode::Ecc::LOW;  // Error correction level
65 
66 	// Make and print the QR Code symbol
67 	const QrCode qr = QrCode::encodeText(text, errCorLvl);
68 	printQr(qr);
69 	std::cout << qr.toSvgString(4) << std::endl;
70 }
71 
72 
73 // Creates a variety of QR Codes that exercise different features of the library, and prints each one to the console.
doVarietyDemo()74 static void doVarietyDemo() {
75 	// Numeric mode encoding (3.33 bits per digit)
76 	const QrCode qr0 = QrCode::encodeText("314159265358979323846264338327950288419716939937510", QrCode::Ecc::MEDIUM);
77 	printQr(qr0);
78 
79 	// Alphanumeric mode encoding (5.5 bits per character)
80 	const QrCode qr1 = QrCode::encodeText("DOLLAR-AMOUNT:$39.87 PERCENTAGE:100.00% OPERATIONS:+-*/", QrCode::Ecc::HIGH);
81 	printQr(qr1);
82 
83 	// Unicode text as UTF-8
84 	const QrCode qr2 = QrCode::encodeText("\xE3\x81\x93\xE3\x82\x93\xE3\x81\xAB\xE3\x81\xA1wa\xE3\x80\x81"
85 		"\xE4\xB8\x96\xE7\x95\x8C\xEF\xBC\x81\x20\xCE\xB1\xCE\xB2\xCE\xB3\xCE\xB4", QrCode::Ecc::QUARTILE);
86 	printQr(qr2);
87 
88 	// Moderately large QR Code using longer text (from Lewis Carroll's Alice in Wonderland)
89 	const QrCode qr3 = QrCode::encodeText(
90 		"Alice was beginning to get very tired of sitting by her sister on the bank, "
91 		"and of having nothing to do: once or twice she had peeped into the book her sister was reading, "
92 		"but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice "
93 		"'without pictures or conversations?' So she was considering in her own mind (as well as she could, "
94 		"for the hot day made her feel very sleepy and stupid), whether the pleasure of making a "
95 		"daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly "
96 		"a White Rabbit with pink eyes ran close by her.", QrCode::Ecc::HIGH);
97 	printQr(qr3);
98 }
99 
100 
101 // Creates QR Codes with manually specified segments for better compactness.
doSegmentDemo()102 static void doSegmentDemo() {
103 	// Illustration "silver"
104 	const char *silver0 = "THE SQUARE ROOT OF 2 IS 1.";
105 	const char *silver1 = "41421356237309504880168872420969807856967187537694807317667973799";
106 	const QrCode qr0 = QrCode::encodeText(
107 		(std::string(silver0) + silver1).c_str(),
108 		QrCode::Ecc::LOW);
109 	printQr(qr0);
110 
111 	const QrCode qr1 = QrCode::encodeSegments(
112 		{QrSegment::makeAlphanumeric(silver0), QrSegment::makeNumeric(silver1)},
113 		QrCode::Ecc::LOW);
114 	printQr(qr1);
115 
116 	// Illustration "golden"
117 	const char *golden0 = "Golden ratio \xCF\x86 = 1.";
118 	const char *golden1 = "6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374";
119 	const char *golden2 = "......";
120 	const QrCode qr2 = QrCode::encodeText(
121 		(std::string(golden0) + golden1 + golden2).c_str(),
122 		QrCode::Ecc::LOW);
123 	printQr(qr2);
124 
125 	std::vector<uint8_t> bytes(golden0, golden0 + std::strlen(golden0));
126 	const QrCode qr3 = QrCode::encodeSegments(
127 		{QrSegment::makeBytes(bytes), QrSegment::makeNumeric(golden1), QrSegment::makeAlphanumeric(golden2)},
128 		QrCode::Ecc::LOW);
129 	printQr(qr3);
130 
131 	// Illustration "Madoka": kanji, kana, Cyrillic, full-width Latin, Greek characters
132 	const char *madoka =  // Encoded in UTF-8
133 		"\xE3\x80\x8C\xE9\xAD\x94\xE6\xB3\x95\xE5"
134 		"\xB0\x91\xE5\xA5\xB3\xE3\x81\xBE\xE3\x81"
135 		"\xA9\xE3\x81\x8B\xE2\x98\x86\xE3\x83\x9E"
136 		"\xE3\x82\xAE\xE3\x82\xAB\xE3\x80\x8D\xE3"
137 		"\x81\xA3\xE3\x81\xA6\xE3\x80\x81\xE3\x80"
138 		"\x80\xD0\x98\xD0\x90\xD0\x98\xE3\x80\x80"
139 		"\xEF\xBD\x84\xEF\xBD\x85\xEF\xBD\x93\xEF"
140 		"\xBD\x95\xE3\x80\x80\xCE\xBA\xCE\xB1\xEF"
141 		"\xBC\x9F";
142 	const QrCode qr4 = QrCode::encodeText(madoka, QrCode::Ecc::LOW);
143 	printQr(qr4);
144 
145 	const std::vector<int> kanjiChars{  // Kanji mode encoding (13 bits per character)
146 		0x0035, 0x1002, 0x0FC0, 0x0AED, 0x0AD7,
147 		0x015C, 0x0147, 0x0129, 0x0059, 0x01BD,
148 		0x018D, 0x018A, 0x0036, 0x0141, 0x0144,
149 		0x0001, 0x0000, 0x0249, 0x0240, 0x0249,
150 		0x0000, 0x0104, 0x0105, 0x0113, 0x0115,
151 		0x0000, 0x0208, 0x01FF, 0x0008,
152 	};
153 	qrcodegen::BitBuffer bb;
154 	for (int c : kanjiChars)
155 		bb.appendBits(static_cast<std::uint32_t>(c), 13);
156 	const QrCode qr5 = QrCode::encodeSegments(
157 		{QrSegment(QrSegment::Mode::KANJI, static_cast<int>(kanjiChars.size()), bb)},
158 		QrCode::Ecc::LOW);
159 	printQr(qr5);
160 }
161 
162 
163 // Creates QR Codes with the same size and contents but different mask patterns.
doMaskDemo()164 static void doMaskDemo() {
165 	// Project Nayuki URL
166 	std::vector<QrSegment> segs0 = QrSegment::makeSegments("https://www.nayuki.io/");
167 	printQr(QrCode::encodeSegments(segs0, QrCode::Ecc::HIGH, QrCode::MIN_VERSION, QrCode::MAX_VERSION, -1, true));  // Automatic mask
168 	printQr(QrCode::encodeSegments(segs0, QrCode::Ecc::HIGH, QrCode::MIN_VERSION, QrCode::MAX_VERSION, 3, true));  // Force mask 3
169 
170 	// Chinese text as UTF-8
171 	std::vector<QrSegment> segs1 = QrSegment::makeSegments(
172 		"\xE7\xB6\xAD\xE5\x9F\xBA\xE7\x99\xBE\xE7\xA7\x91\xEF\xBC\x88\x57\x69\x6B\x69\x70"
173 		"\x65\x64\x69\x61\xEF\xBC\x8C\xE8\x81\x86\xE8\x81\xBD\x69\x2F\xCB\x8C\x77\xC9\xAA"
174 		"\x6B\xE1\xB5\xBB\xCB\x88\x70\x69\xCB\x90\x64\x69\x2E\xC9\x99\x2F\xEF\xBC\x89\xE6"
175 		"\x98\xAF\xE4\xB8\x80\xE5\x80\x8B\xE8\x87\xAA\xE7\x94\xB1\xE5\x85\xA7\xE5\xAE\xB9"
176 		"\xE3\x80\x81\xE5\x85\xAC\xE9\x96\x8B\xE7\xB7\xA8\xE8\xBC\xAF\xE4\xB8\x94\xE5\xA4"
177 		"\x9A\xE8\xAA\x9E\xE8\xA8\x80\xE7\x9A\x84\xE7\xB6\xB2\xE8\xB7\xAF\xE7\x99\xBE\xE7"
178 		"\xA7\x91\xE5\x85\xA8\xE6\x9B\xB8\xE5\x8D\x94\xE4\xBD\x9C\xE8\xA8\x88\xE7\x95\xAB");
179 	printQr(QrCode::encodeSegments(segs1, QrCode::Ecc::MEDIUM, QrCode::MIN_VERSION, QrCode::MAX_VERSION, 0, true));  // Force mask 0
180 	printQr(QrCode::encodeSegments(segs1, QrCode::Ecc::MEDIUM, QrCode::MIN_VERSION, QrCode::MAX_VERSION, 1, true));  // Force mask 1
181 	printQr(QrCode::encodeSegments(segs1, QrCode::Ecc::MEDIUM, QrCode::MIN_VERSION, QrCode::MAX_VERSION, 5, true));  // Force mask 5
182 	printQr(QrCode::encodeSegments(segs1, QrCode::Ecc::MEDIUM, QrCode::MIN_VERSION, QrCode::MAX_VERSION, 7, true));  // Force mask 7
183 }
184 
185 
186 
187 /*---- Utilities ----*/
188 
189 // Prints the given QrCode object to the console.
printQr(const QrCode & qr)190 static void printQr(const QrCode &qr) {
191 	int border = 4;
192 	for (int y = -border; y < qr.getSize() + border; y++) {
193 		for (int x = -border; x < qr.getSize() + border; x++) {
194 			std::cout << (qr.getModule(x, y) ? "##" : "  ");
195 		}
196 		std::cout << std::endl;
197 	}
198 	std::cout << std::endl;
199 }
200