How EFQRCode Handles Different Error Correction Levels: A Complete Guide
EFQRCode handles four standard QR code error correction levels (L, M, Q, H) by mapping them through the EFCorrectionLevel enum to the underlying QRCodeSwift engine, allowing developers to balance data capacity against damage recovery when generating codes.
The efprefix/efqrcode repository provides a Swift wrapper around QR code generation, with error correction being a critical configuration that determines how much physical damage a printed or displayed code can sustain while remaining readable. Understanding how EFQRCode implements these levels helps developers optimize their QR codes for different environmental conditions and data densities.
Understanding EFQRCode Error Correction Levels
The Four Standard Levels
EFQRCode implements the ISO/IEC 18004 standard for QR codes through the EFCorrectionLevel enum defined in Source/Type/EFCorrectionLevel.swift. The library supports four distinct recovery capacities:
- Level L (Low) – Approximately 7% of codewords can be restored
- Level M (Medium) – Approximately 15% of codewords can be restored
- Level Q (Quartile) – Approximately 25% of codewords can be restored
- Level H (High) – Approximately 30% of codewords can be restored
Higher levels increase redundancy but reduce the maximum amount of data that can be encoded in a fixed-size QR code.
Source Code Location
The error correction implementation resides primarily in two files:
Source/Type/EFCorrectionLevel.swift– Defines the enum and mapping logicSource/EFQRCode+Generator.swift– Consumes the level during QR code initialization
How EFQRCode Maps Correction Levels to the QR Engine
EFQRCode acts as a thin abstraction layer over the QRCodeSwift library. The EFCorrectionLevel enum provides a computed property qrErrorCorrectLevel that translates Swift-friendly enum cases into the underlying engine's expected format:
var qrErrorCorrectLevel: QRErrorCorrectLevel {
switch self {
case .h: return .H
case .l: return .L
case .m: return .M
case .q: return .Q
}
}
This mapping occurs in Source/Type/EFCorrectionLevel.swift and ensures that when you specify .h in your EFQRCode implementation, the QRCodeSwift engine receives the corresponding high-error-correction instruction.
Using Error Correction Levels in EFQRCode Generator
When initializing an EFQRCode.Generator, you pass the desired correction level through the errorCorrectLevel parameter. The initializer in Source/EFQRCode+Generator.swift forwards this value to the underlying QRCode constructor:
self.qrcode = try QRCode(
data,
errorCorrectLevel: errorCorrectLevel.qrErrorCorrectLevel,
withBorder: false,
needTypeTable: true
)
By default, EFQRCode uses .h (High) if you do not specify a level, providing maximum damage tolerance at the expense of data density. You can override this default when creating generators for scenarios where storage efficiency matters more than physical resilience.
Practical Examples
Generate a QR Code with Default High Error Correction
The following example creates a QR code using the default high correction level, suitable for environments where the code might be partially obscured or damaged:
let generator = try EFQRCode.Generator(
"https://example.com",
errorCorrectLevel: .h, // Optional – defaults to .h
style: .basic()
)
let pngData = try generator.toPNGData(width: 300)
Maximize Data Capacity with Low Error Correction
When encoding large amounts of data in a small physical space, use Level L to minimize redundancy and maximize storage:
let generator = try EFQRCode.Generator(
"Very long text string requiring maximum storage capacity...",
errorCorrectLevel: .l, // Low – 7% recovery, maximum data
style: .basic()
)
let image = try generator.toImage(width: 400)
Switch Correction Levels at Runtime
You can dynamically select correction levels based on application state or user preferences:
let levels: [EFCorrectionLevel] = [.l, .m, .q, .h]
for level in levels {
let generator = try EFQRCode.Generator(
"Level test: \(level)",
errorCorrectLevel: level,
style: .basic()
)
let data = try generator.toPNGData(width: 250)
// Compare output sizes or visual density
}
Summary
- EFQRCode supports four standard error correction levels (L, M, Q, H) through the
EFCorrectionLevelenum inSource/Type/EFCorrectionLevel.swift - The library maps these levels to
QRCodeSwiftengine values via theqrErrorCorrectLevelcomputed property - Default configuration uses Level H (30% recovery) unless explicitly overridden during
EFQRCode.Generatorinitialization - Higher correction levels reduce data capacity but increase physical damage tolerance, while lower levels maximize storage density
- All error correction encoding is delegated to the underlying
QRCodeSwiftlibrary after EFQRCode performs the initial enum translation
Frequently Asked Questions
What is the default error correction level in EFQRCode?
EFQRCode defaults to Level H (High) when you create a new Generator instance without specifying the errorCorrectLevel parameter. This provides approximately 30% recovery capacity, meaning up to 30% of the QR code can be damaged or obscured while remaining readable. If you need to encode more data in a smaller space, you must explicitly specify a lower level such as .l or .m.
How do I choose between the four error correction levels?
Select your error correction level based on the physical environment where the QR code will be displayed and the amount of data you need to encode. Use Level L (7%) for digital displays or clean print environments where maximum data capacity matters. Use Level M (15%) for standard print materials. Use Level Q (25%) for industrial or outdoor settings with moderate contamination risk. Use Level H (30%) for harsh environments, artistic QR codes with logo overlays, or situations where the code must remain readable despite significant damage.
Does EFQRCode perform the error correction calculations itself?
No, EFQRCode delegates all error correction encoding to the underlying QRCodeSwift library. EFQRCode's role is limited to providing a Swift-friendly API through the EFCorrectionLevel enum and mapping those values to QRCodeSwift's QRErrorCorrectLevel enum via the qrErrorCorrectLevel computed property. The actual Reed-Solomon error correction algorithms and redundancy calculations occur within the QRCodeSwift dependency, not in EFQRCode's own source files.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →