qrcodegen QR Code Generator Library

repository·master·Indexed 27 days ago

https://github.com/nayuki/qr-code-generator

A high-quality, safety-focused QR Code generator library supporting the QR Code Model 2 standard. It is available in C, C++, Java, Python, Rust, and TypeScript/JavaScript. The library supports all 40 versions and 4 error correction levels, providing raw modules/pixels for custom rendering. The C implementation is specifically designed for environments without heap allocation, avoiding malloc() to ensure suitability for embedded systems.

Tokens
8.5K
Snippets
21
Records
42
Agent score
89%

What's inside qrcodegen

  1. Overview of the C QR Code generator library

    master

    This library is a C implementation of a QR Code generator designed for correctness, flexibility, and safety.

    Key Features:

    • No Heap Allocation: The library avoids malloc(), relying instead on caller-provided buffers and fixed-size stack allocations. This makes it suitable for embedded systems.
    • Standard Compliance: Supports all 40 versions (sizes) and all 4 error correction levels of the QR Code Model 2 standard.
    • Output Format: Produces raw modules/pixels of the QR symbol.
    • Flexible Encoding: Supports numeric, special-alphanumeric, and general text encoding, as well as binary data.
    • Manual Control: Allows users to specify version ranges, mask patterns, error correction levels, and data segments/ECI segments.
    • Safety: Coded to prevent memory corruption, integer overflow, and undefined behavior.
  2. Overview of C++ QR Code Generator features

    master

    The C++ implementation of the QR Code generator library provides:

    • Standard Compliance: Supports all 40 versions (sizes) and all 4 error correction levels per the QR Code Model 2 standard.
    • Flexible Encoding: Automatically chooses the smallest version within a user-specified range and can automatically select the optimal mask pattern (out of 8 possible masks).
    • Optimized Data: Encodes numeric and special-alphanumeric text more efficiently than general text.
    • Output: Provides raw modules/pixels of the QR symbol.
    • Safety: Designed to prevent memory corruption, integer overflow, and undefined behavior.
  3. Overview of QR Code generator library features

    master

    The library provides a high-fidelity implementation of the QR Code Model 2 standard with the following capabilities:

    • Multi-language support: Available in Java, TypeScript/JavaScript, Python, Rust, C++, and C.
    • Standard Compliance: Supports all 40 versions (sizes) and all 4 error correction levels.
    • Flexible Encoding: Automatically chooses the smallest version within a user-specified range and selects the optimal mask pattern (out of 8 possible masks).
    • Manual Control: Users can manually specify minimum/maximum versions, mask patterns, absolute error correction levels, and custom data segments (including ECI segments).
    • Output: Provides raw modules/pixels for custom rendering.
    • Advanced Java Features: Includes support for Japanese Unicode text in kanji mode and optimal segment mode switching for mixed text types.
  4. Use the qrcodegen-no-heap Rust library

    master
    The qrcodegen-no-heap library is a QR Code generator designed for environments where heap allocation is not allowed. It avoids std::vec::Vec and instead relies on buffers provided by the caller and fixed-size stack allocations. It supports all 40 QR versions and all 4 error correction levels (QR Code Model 2 standard).
  5. Encode data segments manually

    master

    For more control, you can create data segments using qrcodegen.QrSegment.makeSegments and then encode them using qrcodegen.QrCode.encodeSegments.

    encodeSegments parameters:

    • segments: The list of data segments.
    • errorCorrectionLevel: The desired qrcodegen.QrCode.Ecc level.
    • minVersion: Minimum QR version (1-40).
    • maxVersion: Maximum QR version (1-40).
    • maskPattern: Manual mask pattern (0-7), or -1 to let the library choose.
    • useEnhancedErrorCorrection: Boolean to allow the library to boost error correction if it doesn't increase the version number.
    const QRC = qrcodegen.QrCode;
    const segs = qrcodegen.QrSegment.makeSegments("3141592653589793238462643383");
    const qr1 = QRC.encodeSegments(segs, QRC.Ecc.HIGH, 5, 5, 2, false);
  6. Generate a QR Code with manual parameters in Java

    master

    For more control, you can manually create data segments using QrSegment.makeSegments and then use QrCode.encodeSegments. This allows you to specify version ranges, mask patterns, and error correction levels.

    Note: While the example below is in Java, other language ports (TypeScript, Python, Rust, C++, C) follow essentially the same API naming and behavior.

    import java.util.List;
    import io.nayuki.qrcodegen.*;
    
    // Manual operation
    List<QrSegment> segs = QrSegment.makeSegments("3141592653589793238462643383");
    QrCode qr1 = QrCode.encodeSegments(segs, QrCode.Ecc.HIGH, 5, 5, 2, false);
    
    // Accessing modules
    for (int y = 0; y < qr1.size; y++) {
        for (int x = 0; x < qr1.size; x++) {
            boolean isBlack = qr1.getModule(x, y);
            // (... paint qr1.getModule(x, y) ...)
        }
    }
  7. Generate a QR Code with advanced manual parameters

    master

    For fine-grained control, use QrCode::encode_segments_advanced. This allows you to specify data segments, error correction levels, version ranges, and mask patterns.

    Parameters for encode_segments_advanced:

    • segs: A slice of QrSegment objects.
    • ecc: The QrCodeEcc level.
    • min_version: The minimum Version allowed.
    • max_version: The maximum Version allowed.
    • mask: An Option<Mask> (use Some(Mask::new(n)) to specify a mask, or None to let the library choose the optimal one).
    • boost_ecc: A boolean indicating whether the library is allowed to increase the error correction level if it doesn't increase the version number.
    extern crate qrcodegen;
    use qrcodegen::Mask;
    use qrcodegen::QrCode;
    use qrcodegen::QrCodeEcc;
    use qrcodegen::QrSegment;
    use qrcodegen::Version;
    
    // Manual operation
    let text: &str = "3141592653589793238462643383";
    let segs = QrSegment::make_segments(text);
    let qr = QrCode::encode_segments_advanced(&segs,
        QrCodeEcc::High, Version::new(5), Version::new(5),
        Some(Mask::new(2)), false).unwrap();
    
    // Accessing modules (pixels)
    for y in 0 .. qr.size() {
        for x in 0 .. qr.size() {
            let is_black = qr.get_module(x, y);
            // (... paint qr.get_module(x, y) ...)
        }
    }
  8. Generate a simple QR Code in Rust

    master

    To generate a QR code with default settings using a single text string, use QrCode::encode_text. You must specify the error correction level using QrCodeEcc.

    Available error correction levels include QrCodeEcc::Low, QrCodeEcc::Medium, QrCodeEcc::Quartile, and QrCodeEcc::High.

    extern crate qrcodegen;
    use qrcodegen::QrCode;
    use qrcodegen::QrCodeEcc;
    
    // Simple operation
    let qr = QrCode::encode_text("Hello, world!",
        QrCodeEcc::Medium).unwrap();
  9. Generate a QR Code from text in Java

    master

    Use QrCode.encodeText(text, ecc) to generate a QR code from a string. You can specify the Error Correction Code (ECC) level using QrCode.Ecc constants (e.g., MEDIUM, HIGH). The output is a QrCode object containing the raw modules.

    import io.nayuki.qrcodegen.*;
    
    // Simple operation
    QrCode qr0 = QrCode.encodeText("Hello, world!", QrCode.Ecc.MEDIUM);
  10. Generate a simple QR Code from text

    master

    Use QrCode.encodeText(text, ecc) to generate a QR code from a string. You must provide the text and an error correction level (QrCode.Ecc).

    To visualize the output, you can iterate over the modules using qr.getModule(x, y) or use a helper like toImage (as shown in the example) to convert the raw modules into a BufferedImage.

    import io.nayuki.fastqrcodegen.*;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import javax.imageio.ImageIO;
    
    // Simple operation
    QrCode qr0 = QrCode.encodeText("Hello, world!", QrCode.Ecc.MEDIUM);
    BufferedImage img = toImage(qr0, 4, 10);  // See QrCodeGeneratorDemo
    ImageIO.write(img, "png", new File("qr-code.png"));
  11. Generate a QR Code using simple operation

    master

    Use QrCode.encode_text to quickly generate a QR code from a string. You can specify the Error Correction Code (ECC) level using QrCode.Ecc. To convert the resulting QR code to an SVG string, use to_svg_str(qr_code, border_size).

    from qrcodegen import *
    
    # Simple operation
    qr0 = QrCode.encode_text("Hello, world!", QrCode.Ecc.MEDIUM)
    svg = to_svg_str(qr0, 4)