Skip to content

CorvidLabs/swift-qr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

10 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

SwiftQR

Swift 6.0+ Platforms MIT License

A pure Swift QR code generator.

QR Code Demo
QR code generated by SwiftQR

Features

  • Pure Swift - Works on macOS and Linux
  • All QR versions - Supports versions 1-40 (21x21 to 177x177 modules)
  • Multiple encoding modes - Numeric, alphanumeric, and byte
  • Error correction - All 4 levels (L, M, Q, H)
  • SVG output - Scalable vector graphics with customizable colors and sizing
  • PNG output - Cross-platform PNG generation via swift-png
  • Built on swift-ascii - Uses PixelGrid for rendering

Installation

Add to your Package.swift:

dependencies: [
    .package(url: "https://github.com/CorvidLabs/swift-qr", from: "0.1.0")
]

Library Usage

Basic Usage

import SwiftQR

let qr = try QRCode.encode("https://example.com", errorCorrection: .medium)

// Generate SVG
let svg = qr.svg()

// Generate PNG
let pngBytes = qr.png()

Custom SVG Options

let qr = try QRCode.encode("Hello, World!", errorCorrection: .high)
let svg = qr.svg(
    moduleSize: 10,      // Size of each module in pixels
    quietZone: 4,        // Quiet zone in modules
    foregroundColor: "#000000",
    backgroundColor: "#FFFFFF"
)

Error Correction Levels

// Low - ~7% recovery, smallest QR code
let qrL = try QRCode.encode(text, errorCorrection: .low)

// Medium - ~15% recovery (default)
let qrM = try QRCode.encode(text, errorCorrection: .medium)

// Quartile - ~25% recovery
let qrQ = try QRCode.encode(text, errorCorrection: .quartile)

// High - ~30% recovery, largest QR code
let qrH = try QRCode.encode(text, errorCorrection: .high)

Accessing Module Data

let qr = try QRCode.encode("Test", errorCorrection: .low)

print("Version: \(qr.version.number)")  // 1-40
print("Size: \(qr.size)x\(qr.size)")    // e.g., 21x21

// Check individual modules
let isDark = qr.module(at: 0, y: 0)

// Get all modules
for (x, y, isDark) in qr.modules() {
    // Process each module
}

Encoding Modes

The encoder automatically selects the optimal mode:

Mode Characters Efficiency
Numeric 0-9 3.3 bits/char
Alphanumeric 0-9, A-Z, space, $%*+-./: 5.5 bits/char
Byte Any UTF-8 8 bits/char
// Numeric mode (most efficient for numbers)
let qr1 = try QRCode.encode("12345678901234")

// Alphanumeric mode (uppercase only)
let qr2 = try QRCode.encode("HELLO WORLD")

// Byte mode (any text)
let qr3 = try QRCode.encode("Hello, World!")

CLI Usage

swift run qr-gen > output.svg

Modify Sources/qr-gen/main.swift to customize the output.

API Reference

QRCode

public struct QRCode: Sendable {
    public let version: QRVersion
    public let errorCorrectionLevel: QRErrorCorrectionLevel
    public let size: Int

    public static func encode(
        _ text: String,
        errorCorrection: QRErrorCorrectionLevel = .medium
    ) throws -> QRCode

    public func module(at x: Int, y: Int) -> Bool
    public func modules() -> [(x: Int, y: Int, isDark: Bool)]

    public func svg(
        moduleSize: Int = 10,
        quietZone: Int = 4,
        foregroundColor: String = "#000000",
        backgroundColor: String = "#FFFFFF"
    ) -> String

    public func png(
        moduleSize: Int = 10,
        quietZone: Int = 4
    ) -> [UInt8]
}

QRErrorCorrectionLevel

public enum QRErrorCorrectionLevel: String, Sendable, CaseIterable {
    case low = "L"       // ~7% recovery
    case medium = "M"    // ~15% recovery
    case quartile = "Q"  // ~25% recovery
    case high = "H"      // ~30% recovery
}

QRVersion

public struct QRVersion: Sendable {
    public let number: Int   // 1-40
    public var size: Int     // 21-177 (17 + 4*version)
}

QREncodingMode

public enum QREncodingMode: Sendable {
    case numeric
    case alphanumeric
    case byte

    public static func optimal(for text: String) -> QREncodingMode
}

License

MIT

About

πŸ“± Generate QR codes in pure Swift; no UIKit, no AppKit, no problem

Topics

Resources

License

Stars

Watchers

Forks

Contributors

Languages