1 QR Code generator library
2 =========================
8 This project aims to be the best, clearest QR Code generator library in multiple languages. The primary goals are flexible options and absolute correctness. Secondary goals are compact implementation size and good documentation comments.
10 Home page with live JavaScript demo, extensive descriptions, and competitor comparisons: [https://www.nayuki.io/page/qr-code-generator-library](https://www.nayuki.io/page/qr-code-generator-library)
18 * Available in 6 programming languages, all with nearly equal functionality: Java, TypeScript/JavaScript, Python, Rust, C++, C
19 * Significantly shorter code but more documentation comments compared to competing libraries
20 * Supports encoding all 40 versions (sizes) and all 4 error correction levels, as per the QR Code Model 2 standard
21 * Output formats: Raw modules/pixels of the QR symbol (all languages), SVG XML string (all languages except C), `BufferedImage` raster bitmap (Java only), HTML5 canvas (TypeScript/JavaScript only)
22 * Detects finder-like penalty patterns more accurately than other implementations
23 * Encodes numeric and special-alphanumeric text in less space than general text
24 * Open source code under the permissive MIT License
28 * User can specify minimum and maximum version numbers allowed, then library will automatically choose smallest version in the range that fits the data
29 * User can specify mask pattern manually, otherwise library will automatically evaluate all 8 masks and select the optimal one
30 * User can specify absolute error correction level, or allow the library to boost it if it doesn't increase the version number
31 * User can create a list of data segments manually and add ECI segments
33 Optional advanced features (Java only):
35 * Encodes Japanese Unicode text in kanji mode to save a lot of space compared to UTF-8 bytes
36 * Computes optimal segment mode switching for text with mixed numeric/alphanumeric/general/kanji parts
38 More information about QR Code technology and this library's design can be found on the project home page.
46 import java.awt.image.BufferedImage;
48 import java.util.List;
49 import javax.imageio.ImageIO;
50 import io.nayuki.qrcodegen.*;
53 QrCode qr0 = QrCode.encodeText("Hello, world!", QrCode.Ecc.MEDIUM);
54 BufferedImage img = qr0.toImage(4, 10);
55 ImageIO.write(img, "png", new File("qr-code.png"));
58 List<QrSegment> segs = QrSegment.makeSegments("3141592653589793238462643383");
59 QrCode qr1 = QrCode.encodeSegments(segs, QrCode.Ecc.HIGH, 5, 5, 2, false);
60 for (int y = 0; y < qr1.size; y++) {
61 for (int x = 0; x < qr1.size; x++) {
62 (... paint qr1.getModule(x, y) ...)
66 TypeScript/JavaScript languages:
68 // Name abbreviated for the sake of these examples here
69 var QRC = qrcodegen.QrCode;
72 var qr0 = QRC.encodeText("Hello, world!", QRC.Ecc.MEDIUM);
73 var svg = qr0.toSvgString(4);
76 var segs = qrcodegen.QrSegment.makeSegments("3141592653589793238462643383");
77 var qr1 = QRC.encodeSegments(segs, QRC.Ecc.HIGH, 5, 5, 2, false);
78 for (var y = 0; y < qr1.size; y++) {
79 for (var x = 0; x < qr1.size; x++) {
80 (... paint qr1.getModule(x, y) ...)
86 from qrcodegen import *
89 qr0 = QrCode.encode_text("Hello, world!", QrCode.Ecc.MEDIUM)
90 svg = qr0.to_svg_str(4)
93 segs = QrSegment.make_segments("3141592653589793238462643383")
94 qr1 = QrCode.encode_segments(segs, QrCode.Ecc.HIGH, 5, 5, 2, False)
95 for y in range(qr1.get_size()):
96 for x in range(qr1.get_size()):
97 (... paint qr1.get_module(x, y) ...)
103 #include "QrCode.hpp"
104 using namespace qrcodegen;
107 QrCode qr0 = QrCode::encodeText("Hello, world!", QrCode::Ecc::MEDIUM);
108 std::string svg = qr0.toSvgString(4);
111 std::vector<QrSegment> segs =
112 QrSegment::makeSegments("3141592653589793238462643383");
113 QrCode qr1 = QrCode::encodeSegments(
114 segs, QrCode::Ecc::HIGH, 5, 5, 2, false);
115 for (int y = 0; y < qr1.getSize(); y++) {
116 for (int x = 0; x < qr1.getSize(); x++) {
117 (... paint qr1.getModule(x, y) ...)
125 #include "qrcodegen.h"
128 uint8_t qr0[qrcodegen_BUFFER_LEN_MAX];
129 uint8_t tempBuffer[qrcodegen_BUFFER_LEN_MAX];
130 bool ok = qrcodegen_encodeText("Hello, world!",
131 tempBuffer, qr0, qrcodegen_Ecc_MEDIUM,
132 qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX,
133 qrcodegen_Mask_AUTO, true);
137 int size = qrcodegen_getSize(qr0);
138 for (int y = 0; y < size; y++) {
139 for (int x = 0; x < size; x++) {
140 (... paint qrcodegen_getModule(qr0, x, y) ...)
145 uint8_t dataAndTemp[qrcodegen_BUFFER_LEN_FOR_VERSION(7)]
146 = {0xE3, 0x81, 0x82};
147 uint8_t qr1[qrcodegen_BUFFER_LEN_FOR_VERSION(7)];
148 ok = qrcodegen_encodeBinary(dataAndTemp, 3, qr1,
149 qrcodegen_Ecc_HIGH, 2, 7, qrcodegen_Mask_4, false);
153 extern crate qrcodegen;
154 use qrcodegen::QrCode;
155 use qrcodegen::QrCodeEcc;
156 use qrcodegen::QrSegment;
159 let qr = QrCode::encode_text("Hello, world!",
160 QrCodeEcc::Medium).unwrap();
161 let svg = qr.to_svg_string(4);
164 let chrs: Vec<char> = "3141592653589793238462643383".chars().collect();
165 let segs = QrSegment::make_segments(&chrs);
166 let qr = QrCode::encode_segments_advanced(
167 &segs, QrCodeEcc::High, 5, 5, Some(Mask::new(2)), false).unwrap();
168 for y in 0 .. qr.size() {
169 for x in 0 .. qr.size() {
170 (... paint qr.get_module(x, y) ...)
178 Copyright © 2020 Project Nayuki. (MIT License)
179 [https://www.nayuki.io/page/qr-code-generator-library](https://www.nayuki.io/page/qr-code-generator-library)
181 Permission is hereby granted, free of charge, to any person obtaining a copy of
182 this software and associated documentation files (the "Software"), to deal in
183 the Software without restriction, including without limitation the rights to
184 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
185 the Software, and to permit persons to whom the Software is furnished to do so,
186 subject to the following conditions:
188 * The above copyright notice and this permission notice shall be included in
189 all copies or substantial portions of the Software.
191 * The Software is provided "as is", without warranty of any kind, express or
192 implied, including but not limited to the warranties of merchantability,
193 fitness for a particular purpose and noninfringement. In no event shall the
194 authors or copyright holders be liable for any claim, damages or other
195 liability, whether in an action of contract, tort or otherwise, arising from,
196 out of or in connection with the Software or the use or other dealings in the