2016-04-15 04:26:37 +00:00
|
|
|
/*
|
|
|
|
* QR Code generator demo (C++)
|
|
|
|
*
|
|
|
|
* Run this command-line program with no arguments. The program computes a bunch of demonstration
|
|
|
|
* QR Codes and prints them to the console. Also, the SVG code for one QR Code is printed as a sample.
|
|
|
|
*
|
2017-04-24 20:41:54 +00:00
|
|
|
* Copyright (c) Project Nayuki. (MIT License)
|
2016-04-15 04:26:37 +00:00
|
|
|
* https://www.nayuki.io/page/qr-code-generator-library
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
* this software and associated documentation files (the "Software"), to deal in
|
|
|
|
* the Software without restriction, including without limitation the rights to
|
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
* - The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
* - The Software is provided "as is", without warranty of any kind, express or
|
|
|
|
* implied, including but not limited to the warranties of merchantability,
|
|
|
|
* fitness for a particular purpose and noninfringement. In no event shall the
|
|
|
|
* authors or copyright holders be liable for any claim, damages or other
|
|
|
|
* liability, whether in an action of contract, tort or otherwise, arising from,
|
|
|
|
* out of or in connection with the Software or the use or other dealings in the
|
|
|
|
* Software.
|
|
|
|
*/
|
|
|
|
|
2016-05-24 18:50:22 +00:00
|
|
|
#include <cstdint>
|
2017-02-25 19:24:56 +00:00
|
|
|
#include <cstdlib>
|
2016-04-15 04:26:37 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
2016-05-24 18:50:22 +00:00
|
|
|
#include <vector>
|
2017-08-17 21:29:15 +00:00
|
|
|
#include "BitBuffer.hpp"
|
2016-04-15 04:26:37 +00:00
|
|
|
#include "QrCode.hpp"
|
|
|
|
|
2017-05-05 20:57:13 +00:00
|
|
|
using std::uint8_t;
|
2017-04-21 05:38:44 +00:00
|
|
|
using qrcodegen::QrCode;
|
|
|
|
using qrcodegen::QrSegment;
|
|
|
|
|
2016-04-15 04:26:37 +00:00
|
|
|
|
|
|
|
// Function prototypes
|
|
|
|
static void doBasicDemo();
|
|
|
|
static void doVarietyDemo();
|
|
|
|
static void doSegmentDemo();
|
2017-04-21 05:38:44 +00:00
|
|
|
static void printQr(const QrCode &qr);
|
2016-04-15 04:26:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-04-20 20:27:41 +00:00
|
|
|
// The main application program.
|
2017-02-25 19:24:56 +00:00
|
|
|
int main() {
|
2016-04-15 04:26:37 +00:00
|
|
|
doBasicDemo();
|
|
|
|
doVarietyDemo();
|
|
|
|
doSegmentDemo();
|
2017-02-25 19:24:56 +00:00
|
|
|
return EXIT_SUCCESS;
|
2016-04-15 04:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Creates a single QR Code, then prints it to the console.
|
|
|
|
static void doBasicDemo() {
|
|
|
|
const char *text = "Hello, world!"; // User-supplied text
|
2017-04-21 05:38:44 +00:00
|
|
|
const QrCode::Ecc &errCorLvl = QrCode::Ecc::LOW; // Error correction level
|
2016-04-15 04:26:37 +00:00
|
|
|
|
|
|
|
// Make and print the QR Code symbol
|
2017-04-21 05:38:44 +00:00
|
|
|
const QrCode qr = QrCode::encodeText(text, errCorLvl);
|
2016-04-15 04:26:37 +00:00
|
|
|
std::cout << qr.toSvgString(4) << std::endl;
|
|
|
|
printQr(qr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Creates a variety of QR Codes that exercise different features of the library, and prints each one to the console.
|
|
|
|
static void doVarietyDemo() {
|
|
|
|
// Project Nayuki URL
|
2017-04-21 05:38:44 +00:00
|
|
|
const QrCode qr0 = QrCode::encodeText("https://www.nayuki.io/", QrCode::Ecc::HIGH);
|
|
|
|
printQr(QrCode(qr0, 3)); // Change mask, forcing to mask #3
|
2016-04-15 04:26:37 +00:00
|
|
|
|
|
|
|
// Numeric mode encoding (3.33 bits per digit)
|
2017-04-21 05:38:44 +00:00
|
|
|
const QrCode qr1 = QrCode::encodeText("314159265358979323846264338327950288419716939937510", QrCode::Ecc::MEDIUM);
|
2016-04-15 04:26:37 +00:00
|
|
|
printQr(qr1);
|
|
|
|
|
|
|
|
// Alphanumeric mode encoding (5.5 bits per character)
|
2017-04-21 05:38:44 +00:00
|
|
|
const QrCode qr2 = QrCode::encodeText("DOLLAR-AMOUNT:$39.87 PERCENTAGE:100.00% OPERATIONS:+-*/", QrCode::Ecc::HIGH);
|
2016-04-15 04:26:37 +00:00
|
|
|
printQr(qr2);
|
|
|
|
|
|
|
|
// Unicode text as UTF-8, and different masks
|
2017-04-21 05:38:44 +00:00
|
|
|
const QrCode qr3 = QrCode::encodeText("\xE3\x81\x93\xE3\x82\x93\xE3\x81\xAB\xE3\x81\xA1wa\xE3\x80\x81\xE4\xB8\x96\xE7\x95\x8C\xEF\xBC\x81\x20\xCE\xB1\xCE\xB2\xCE\xB3\xCE\xB4", QrCode::Ecc::QUARTILE);
|
|
|
|
printQr(QrCode(qr3, 0));
|
|
|
|
printQr(QrCode(qr3, 1));
|
|
|
|
printQr(QrCode(qr3, 5));
|
|
|
|
printQr(QrCode(qr3, 7));
|
2016-04-15 04:26:37 +00:00
|
|
|
|
|
|
|
// Moderately large QR Code using longer text (from Lewis Carroll's Alice in Wonderland)
|
2017-04-21 05:38:44 +00:00
|
|
|
const QrCode qr4 = QrCode::encodeText(
|
2016-04-20 20:50:42 +00:00
|
|
|
"Alice was beginning to get very tired of sitting by her sister on the bank, "
|
2016-04-15 04:26:37 +00:00
|
|
|
"and of having nothing to do: once or twice she had peeped into the book her sister was reading, "
|
|
|
|
"but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice "
|
|
|
|
"'without pictures or conversations?' So she was considering in her own mind (as well as she could, "
|
|
|
|
"for the hot day made her feel very sleepy and stupid), whether the pleasure of making a "
|
|
|
|
"daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly "
|
2017-04-21 05:38:44 +00:00
|
|
|
"a White Rabbit with pink eyes ran close by her.", QrCode::Ecc::HIGH);
|
2016-04-15 04:26:37 +00:00
|
|
|
printQr(qr4);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Creates QR Codes with manually specified segments for better compactness.
|
|
|
|
static void doSegmentDemo() {
|
|
|
|
// Illustration "silver"
|
|
|
|
const char *silver0 = "THE SQUARE ROOT OF 2 IS 1.";
|
|
|
|
const char *silver1 = "41421356237309504880168872420969807856967187537694807317667973799";
|
2017-04-21 05:38:44 +00:00
|
|
|
const QrCode qr0 = QrCode::encodeText(
|
2016-04-15 04:26:37 +00:00
|
|
|
(std::string(silver0) + silver1).c_str(),
|
2017-04-21 05:38:44 +00:00
|
|
|
QrCode::Ecc::LOW);
|
2016-04-15 04:26:37 +00:00
|
|
|
printQr(qr0);
|
|
|
|
|
2017-04-21 05:38:44 +00:00
|
|
|
std::vector<QrSegment> segs;
|
|
|
|
segs.push_back(QrSegment::makeAlphanumeric(silver0));
|
|
|
|
segs.push_back(QrSegment::makeNumeric(silver1));
|
|
|
|
const QrCode qr1 = QrCode::encodeSegments(segs, QrCode::Ecc::LOW);
|
2016-04-15 04:26:37 +00:00
|
|
|
printQr(qr1);
|
|
|
|
|
|
|
|
// Illustration "golden"
|
|
|
|
const char *golden0 = "Golden ratio \xCF\x86 = 1.";
|
|
|
|
const char *golden1 = "6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374";
|
|
|
|
const char *golden2 = "......";
|
2017-04-21 05:38:44 +00:00
|
|
|
const QrCode qr2 = QrCode::encodeText(
|
2016-04-15 04:26:37 +00:00
|
|
|
(std::string(golden0) + golden1 + golden2).c_str(),
|
2017-04-21 05:38:44 +00:00
|
|
|
QrCode::Ecc::LOW);
|
2016-04-15 04:26:37 +00:00
|
|
|
printQr(qr2);
|
|
|
|
|
|
|
|
segs.clear();
|
|
|
|
std::vector<uint8_t> bytes;
|
|
|
|
for (const char *temp = golden0; *temp != '\0'; temp++)
|
|
|
|
bytes.push_back(static_cast<uint8_t>(*temp));
|
2017-04-21 05:38:44 +00:00
|
|
|
segs.push_back(QrSegment::makeBytes(bytes));
|
|
|
|
segs.push_back(QrSegment::makeNumeric(golden1));
|
|
|
|
segs.push_back(QrSegment::makeAlphanumeric(golden2));
|
|
|
|
const QrCode qr3 = QrCode::encodeSegments(segs, QrCode::Ecc::LOW);
|
2016-04-15 04:26:37 +00:00
|
|
|
printQr(qr3);
|
2017-04-21 16:59:13 +00:00
|
|
|
|
|
|
|
// Illustration "Madoka": kanji, kana, Greek, Cyrillic, full-width Latin characters
|
|
|
|
const char *madoka = "\xE3\x80\x8C\xE9\xAD\x94\xE6\xB3\x95\xE5\xB0\x91\xE5\xA5\xB3\xE3\x81\xBE\xE3\x81\xA9\xE3\x81\x8B\xE2\x98\x86\xE3\x83\x9E\xE3\x82\xAE\xE3\x82\xAB\xE3\x80\x8D\xE3\x81\xA3\xE3\x81\xA6\xE3\x80\x81\xE3\x80\x80\xD0\x98\xD0\x90\xD0\x98\xE3\x80\x80\xEF\xBD\x84\xEF\xBD\x85\xEF\xBD\x93\xEF\xBD\x95\xE3\x80\x80\xCE\xBA\xCE\xB1\xEF\xBC\x9F";
|
|
|
|
const QrCode qr4 = QrCode::encodeText(madoka, QrCode::Ecc::LOW);
|
|
|
|
printQr(qr4);
|
|
|
|
|
2017-08-17 21:29:15 +00:00
|
|
|
const std::vector<int> packedKanjiData{ // Kanji mode encoding (13 bits per character)
|
|
|
|
0x0035, 0x1002, 0x0FC0, 0x0AED, 0x0AD7,
|
|
|
|
0x015C, 0x0147, 0x0129, 0x0059, 0x01BD,
|
|
|
|
0x018D, 0x018A, 0x0036, 0x0141, 0x0144,
|
|
|
|
0x0001, 0x0000, 0x0249, 0x0240, 0x0249,
|
|
|
|
0x0000, 0x0104, 0x0105, 0x0113, 0x0115,
|
|
|
|
0x0000, 0x0208, 0x01FF, 0x0008,
|
2017-04-21 16:59:13 +00:00
|
|
|
};
|
2017-08-17 21:29:15 +00:00
|
|
|
qrcodegen::BitBuffer bb;
|
|
|
|
for (int c : packedKanjiData)
|
|
|
|
bb.appendBits(c, 13);
|
2017-04-21 16:59:13 +00:00
|
|
|
segs.clear();
|
2017-08-17 21:29:15 +00:00
|
|
|
segs.push_back(QrSegment(QrSegment::Mode::KANJI, packedKanjiData.size(), bb));
|
2017-04-21 16:59:13 +00:00
|
|
|
const QrCode qr5 = QrCode::encodeSegments(segs, QrCode::Ecc::LOW);
|
|
|
|
printQr(qr5);
|
2016-04-15 04:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Prints the given QR Code to the console.
|
2017-04-21 05:38:44 +00:00
|
|
|
static void printQr(const QrCode &qr) {
|
2016-04-15 04:26:37 +00:00
|
|
|
int border = 4;
|
|
|
|
for (int y = -border; y < qr.size + border; y++) {
|
|
|
|
for (int x = -border; x < qr.size + border; x++) {
|
|
|
|
std::cout << (qr.getModule(x, y) == 1 ? "##" : " ");
|
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
|
|
|
}
|