• 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 <climits>
28 #include <cstdint>
29 #include <cstdlib>
30 #include <cstring>
31 #include <iostream>
32 #include <sstream>
33 #include <string>
34 #include <vector>
35 #include "qrcodegen.hpp"
36 
37 using std::uint8_t;
38 using qrcodegen::QrCode;
39 using qrcodegen::QrSegment;
40 
41 
42 // Function prototypes
43 static void doBasicDemo();
44 static void doVarietyDemo();
45 static void doSegmentDemo();
46 static void doMaskDemo();
47 static std::string toSvgString(const QrCode &qr, int border);
48 static void printQr(const QrCode &qr);
49 
50 
51 // The main application program.
main()52 int main() {
53 	doBasicDemo();
54 	doVarietyDemo();
55 	doSegmentDemo();
56 	doMaskDemo();
57 	return EXIT_SUCCESS;
58 }
59 
60 
61 
62 /*---- Demo suite ----*/
63 
64 // Creates a single QR Code, then prints it to the console.
doBasicDemo()65 static void doBasicDemo() {
66 	const char *text = "Hello, world!";              // User-supplied text
67 	const QrCode::Ecc errCorLvl = QrCode::Ecc::LOW;  // Error correction level
68 
69 	// Make and print the QR Code symbol
70 	const QrCode qr = QrCode::encodeText(text, errCorLvl);
71 	printQr(qr);
72 	std::cout << toSvgString(qr, 4) << std::endl;
73 }
74 
75 
76 // Creates a variety of QR Codes that exercise different features of the library, and prints each one to the console.
doVarietyDemo()77 static void doVarietyDemo() {
78 	// Numeric mode encoding (3.33 bits per digit)
79 	const QrCode qr0 = QrCode::encodeText("314159265358979323846264338327950288419716939937510", QrCode::Ecc::MEDIUM);
80 	printQr(qr0);
81 
82 	// Alphanumeric mode encoding (5.5 bits per character)
83 	const QrCode qr1 = QrCode::encodeText("DOLLAR-AMOUNT:$39.87 PERCENTAGE:100.00% OPERATIONS:+-*/", QrCode::Ecc::HIGH);
84 	printQr(qr1);
85 
86 	// Unicode text as UTF-8
87 	const QrCode qr2 = QrCode::encodeText("\xE3\x81\x93\xE3\x82\x93\xE3\x81\xAB\xE3\x81\xA1wa\xE3\x80\x81"
88 		"\xE4\xB8\x96\xE7\x95\x8C\xEF\xBC\x81\x20\xCE\xB1\xCE\xB2\xCE\xB3\xCE\xB4", QrCode::Ecc::QUARTILE);
89 	printQr(qr2);
90 
91 	// Moderately large QR Code using longer text (from Lewis Carroll's Alice in Wonderland)
92 	const QrCode qr3 = QrCode::encodeText(
93 		"Alice was beginning to get very tired of sitting by her sister on the bank, "
94 		"and of having nothing to do: once or twice she had peeped into the book her sister was reading, "
95 		"but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice "
96 		"'without pictures or conversations?' So she was considering in her own mind (as well as she could, "
97 		"for the hot day made her feel very sleepy and stupid), whether the pleasure of making a "
98 		"daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly "
99 		"a White Rabbit with pink eyes ran close by her.", QrCode::Ecc::HIGH);
100 	printQr(qr3);
101 }
102 
103 
104 // Creates QR Codes with manually specified segments for better compactness.
doSegmentDemo()105 static void doSegmentDemo() {
106 	// Illustration "silver"
107 	const char *silver0 = "THE SQUARE ROOT OF 2 IS 1.";
108 	const char *silver1 = "41421356237309504880168872420969807856967187537694807317667973799";
109 	const QrCode qr0 = QrCode::encodeText(
110 		(std::string(silver0) + silver1).c_str(),
111 		QrCode::Ecc::LOW);
112 	printQr(qr0);
113 
114 	const QrCode qr1 = QrCode::encodeSegments(
115 		{QrSegment::makeAlphanumeric(silver0), QrSegment::makeNumeric(silver1)},
116 		QrCode::Ecc::LOW);
117 	printQr(qr1);
118 
119 	// Illustration "golden"
120 	const char *golden0 = "Golden ratio \xCF\x86 = 1.";
121 	const char *golden1 = "6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374";
122 	const char *golden2 = "......";
123 	const QrCode qr2 = QrCode::encodeText(
124 		(std::string(golden0) + golden1 + golden2).c_str(),
125 		QrCode::Ecc::LOW);
126 	printQr(qr2);
127 
128 	std::vector<uint8_t> bytes(golden0, golden0 + std::strlen(golden0));
129 	const QrCode qr3 = QrCode::encodeSegments(
130 		{QrSegment::makeBytes(bytes), QrSegment::makeNumeric(golden1), QrSegment::makeAlphanumeric(golden2)},
131 		QrCode::Ecc::LOW);
132 	printQr(qr3);
133 
134 	// Illustration "Madoka": kanji, kana, Cyrillic, full-width Latin, Greek characters
135 	const char *madoka =  // Encoded in UTF-8
136 		"\xE3\x80\x8C\xE9\xAD\x94\xE6\xB3\x95\xE5"
137 		"\xB0\x91\xE5\xA5\xB3\xE3\x81\xBE\xE3\x81"
138 		"\xA9\xE3\x81\x8B\xE2\x98\x86\xE3\x83\x9E"
139 		"\xE3\x82\xAE\xE3\x82\xAB\xE3\x80\x8D\xE3"
140 		"\x81\xA3\xE3\x81\xA6\xE3\x80\x81\xE3\x80"
141 		"\x80\xD0\x98\xD0\x90\xD0\x98\xE3\x80\x80"
142 		"\xEF\xBD\x84\xEF\xBD\x85\xEF\xBD\x93\xEF"
143 		"\xBD\x95\xE3\x80\x80\xCE\xBA\xCE\xB1\xEF"
144 		"\xBC\x9F";
145 	const QrCode qr4 = QrCode::encodeText(madoka, QrCode::Ecc::LOW);
146 	printQr(qr4);
147 
148 	const std::vector<int> kanjiChars{  // Kanji mode encoding (13 bits per character)
149 		0x0035, 0x1002, 0x0FC0, 0x0AED, 0x0AD7,
150 		0x015C, 0x0147, 0x0129, 0x0059, 0x01BD,
151 		0x018D, 0x018A, 0x0036, 0x0141, 0x0144,
152 		0x0001, 0x0000, 0x0249, 0x0240, 0x0249,
153 		0x0000, 0x0104, 0x0105, 0x0113, 0x0115,
154 		0x0000, 0x0208, 0x01FF, 0x0008,
155 	};
156 	qrcodegen::BitBuffer bb;
157 	for (int c : kanjiChars)
158 		bb.appendBits(static_cast<std::uint32_t>(c), 13);
159 	const QrCode qr5 = QrCode::encodeSegments(
160 		{QrSegment(QrSegment::Mode::KANJI, static_cast<int>(kanjiChars.size()), bb)},
161 		QrCode::Ecc::LOW);
162 	printQr(qr5);
163 }
164 
165 
166 // Creates QR Codes with the same size and contents but different mask patterns.
doMaskDemo()167 static void doMaskDemo() {
168 	// Project Nayuki URL
169 	std::vector<QrSegment> segs0 = QrSegment::makeSegments("https://www.nayuki.io/");
170 	printQr(QrCode::encodeSegments(segs0, QrCode::Ecc::HIGH, QrCode::MIN_VERSION, QrCode::MAX_VERSION, -1, true));  // Automatic mask
171 	printQr(QrCode::encodeSegments(segs0, QrCode::Ecc::HIGH, QrCode::MIN_VERSION, QrCode::MAX_VERSION, 3, true));  // Force mask 3
172 
173 	// Chinese text as UTF-8
174 	std::vector<QrSegment> segs1 = QrSegment::makeSegments(
175 		"\xE7\xB6\xAD\xE5\x9F\xBA\xE7\x99\xBE\xE7\xA7\x91\xEF\xBC\x88\x57\x69\x6B\x69\x70"
176 		"\x65\x64\x69\x61\xEF\xBC\x8C\xE8\x81\x86\xE8\x81\xBD\x69\x2F\xCB\x8C\x77\xC9\xAA"
177 		"\x6B\xE1\xB5\xBB\xCB\x88\x70\x69\xCB\x90\x64\x69\x2E\xC9\x99\x2F\xEF\xBC\x89\xE6"
178 		"\x98\xAF\xE4\xB8\x80\xE5\x80\x8B\xE8\x87\xAA\xE7\x94\xB1\xE5\x85\xA7\xE5\xAE\xB9"
179 		"\xE3\x80\x81\xE5\x85\xAC\xE9\x96\x8B\xE7\xB7\xA8\xE8\xBC\xAF\xE4\xB8\x94\xE5\xA4"
180 		"\x9A\xE8\xAA\x9E\xE8\xA8\x80\xE7\x9A\x84\xE7\xB6\xB2\xE8\xB7\xAF\xE7\x99\xBE\xE7"
181 		"\xA7\x91\xE5\x85\xA8\xE6\x9B\xB8\xE5\x8D\x94\xE4\xBD\x9C\xE8\xA8\x88\xE7\x95\xAB");
182 	printQr(QrCode::encodeSegments(segs1, QrCode::Ecc::MEDIUM, QrCode::MIN_VERSION, QrCode::MAX_VERSION, 0, true));  // Force mask 0
183 	printQr(QrCode::encodeSegments(segs1, QrCode::Ecc::MEDIUM, QrCode::MIN_VERSION, QrCode::MAX_VERSION, 1, true));  // Force mask 1
184 	printQr(QrCode::encodeSegments(segs1, QrCode::Ecc::MEDIUM, QrCode::MIN_VERSION, QrCode::MAX_VERSION, 5, true));  // Force mask 5
185 	printQr(QrCode::encodeSegments(segs1, QrCode::Ecc::MEDIUM, QrCode::MIN_VERSION, QrCode::MAX_VERSION, 7, true));  // Force mask 7
186 }
187 
188 
189 
190 /*---- Utilities ----*/
191 
192 // Returns a string of SVG code for an image depicting the given QR Code, with the given number
193 // of border modules. The string always uses Unix newlines (\n), regardless of the platform.
toSvgString(const QrCode & qr,int border)194 static std::string toSvgString(const QrCode &qr, int border) {
195 	if (border < 0)
196 		throw std::domain_error("Border must be non-negative");
197 	if (border > INT_MAX / 2 || border * 2 > INT_MAX - qr.getSize())
198 		throw std::overflow_error("Border too large");
199 
200 	std::ostringstream sb;
201 	sb << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
202 	sb << "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n";
203 	sb << "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" viewBox=\"0 0 ";
204 	sb << (qr.getSize() + border * 2) << " " << (qr.getSize() + border * 2) << "\" stroke=\"none\">\n";
205 	sb << "\t<rect width=\"100%\" height=\"100%\" fill=\"#FFFFFF\"/>\n";
206 	sb << "\t<path d=\"";
207 	for (int y = 0; y < qr.getSize(); y++) {
208 		for (int x = 0; x < qr.getSize(); x++) {
209 			if (qr.getModule(x, y)) {
210 				if (x != 0 || y != 0)
211 					sb << " ";
212 				sb << "M" << (x + border) << "," << (y + border) << "h1v1h-1z";
213 			}
214 		}
215 	}
216 	sb << "\" fill=\"#000000\"/>\n";
217 	sb << "</svg>\n";
218 	return sb.str();
219 }
220 
221 
222 // Prints the given QrCode object to the console.
printQr(const QrCode & qr)223 static void printQr(const QrCode &qr) {
224 	int border = 4;
225 	for (int y = -border; y < qr.getSize() + border; y++) {
226 		for (int x = -border; x < qr.getSize() + border; x++) {
227 			std::cout << (qr.getModule(x, y) ? "##" : "  ");
228 		}
229 		std::cout << std::endl;
230 	}
231 	std::cout << std::endl;
232 }
233