/ Readme.markdown
Readme.markdown
  1  QR Code generator library
  2  =========================
  3  
  4  
  5  Introduction
  6  ------------
  7  
  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.
  9  
 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)
 11  
 12  
 13  Features
 14  --------
 15  
 16  Core features:
 17  
 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
 25  
 26  Manual parameters:
 27  
 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
 32  
 33  Optional advanced features (Java only):
 34  
 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
 37  
 38  More information about QR Code technology and this library's design can be found on the project home page.
 39  
 40  
 41  Examples
 42  --------
 43  
 44  Java language:
 45  
 46      import java.awt.image.BufferedImage;
 47      import java.io.File;
 48      import java.util.List;
 49      import javax.imageio.ImageIO;
 50      import io.nayuki.qrcodegen.*;
 51      
 52      // Simple operation
 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"));
 56      
 57      // Manual operation
 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) ...)
 63          }
 64      }
 65  
 66  TypeScript/JavaScript languages:
 67  
 68      // Name abbreviated for the sake of these examples here
 69      var QRC = qrcodegen.QrCode;
 70      
 71      // Simple operation
 72      var qr0 = QRC.encodeText("Hello, world!", QRC.Ecc.MEDIUM);
 73      var svg = qr0.toSvgString(4);
 74      
 75      // Manual operation
 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) ...)
 81          }
 82      }
 83  
 84  Python language:
 85  
 86      from qrcodegen import *
 87      
 88      # Simple operation
 89      qr0 = QrCode.encode_text("Hello, world!", QrCode.Ecc.MEDIUM)
 90      svg = qr0.to_svg_str(4)
 91      
 92      # Manual operation
 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) ...)
 98  
 99  C++ language:
100  
101      #include <string>
102      #include <vector>
103      #include "QrCode.hpp"
104      using namespace qrcodegen;
105      
106      // Simple operation
107      QrCode qr0 = QrCode::encodeText("Hello, world!", QrCode::Ecc::MEDIUM);
108      std::string svg = qr0.toSvgString(4);
109      
110      // Manual operation
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) ...)
118          }
119      }
120  
121  C language:
122  
123      #include <stdbool.h>
124      #include <stdint.h>
125      #include "qrcodegen.h"
126      
127      // Text data
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);
134      if (!ok)
135          return;
136      
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) ...)
141          }
142      }
143      
144      // Binary data
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);
150  
151  Rust language:
152  
153      extern crate qrcodegen;
154      use qrcodegen::QrCode;
155      use qrcodegen::QrCodeEcc;
156      use qrcodegen::QrSegment;
157      
158      // Simple operation
159      let qr = QrCode::encode_text("Hello, world!",
160          QrCodeEcc::Medium).unwrap();
161      let svg = qr.to_svg_string(4);
162      
163      // Manual operation
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) ...)
171          }
172      }
173  
174  
175  License
176  -------
177  
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)
180  
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:
187  
188  * The above copyright notice and this permission notice shall be included in
189    all copies or substantial portions of the Software.
190  
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
197    Software.