EFQRCode Generation and Recognition: Main Entry Points Explained
The main entry points for EFQRCode generation and recognition are the nested classes EFQRCode.Generator and EFQRCode.Recognizer, defined in Source/EFQRCode+Generator.swift and Source/EFQRCode+Recognizer.swift respectively.
EFQRCode is a lightweight Swift library for generating and recognizing QR codes with extensive styling options. Unlike monolithic libraries that expose dozens of top-level functions, EFQRCode adopts a minimalist namespace pattern where an empty EFQRCode class serves as the container for two specialized entry points.
Understanding the EFQRCode Namespace Architecture
The library’s architecture centers on a deliberate separation between the namespace container and the functional implementations.
The Empty Namespace Class
In Source/EFQRCode.swift, the library defines a bare EFQRCode class:
public class EFQRCode { }
This class contains no logic, methods, or properties. Its sole purpose is to provide a static namespace that prevents naming collisions and creates a clean import experience (import EFQRCode).
Extension-Based Organization
All functionality is added via Swift extensions on this empty class. The library splits generation and recognition into separate files using the + naming convention common in Swift projects:
Source/EFQRCode+Generator.swift— Containspublic extension EFQRCode { class Generator { … } }Source/EFQRCode+Recognizer.swift— Containspublic extension EFQRCode { class Recognizer { … } }
This structure ensures that the main entry points for EFQRCode generation and recognition are discoverable under the EFQRCode prefix while maintaining clean separation of concerns.
EFQRCode Generation Entry Point
QR code creation flows through the EFQRCode.Generator class, which encapsulates the entire generation pipeline from raw data to styled output.
The Generator Class Location and Structure
The generator is defined in Source/EFQRCode+Generator.swift as a public class nested inside the EFQRCode extension:
public extension EFQRCode {
public class Generator {
public let qrcode: QRCode
public let style: EFQRCodeStyleBase
// ...
}
}
The class holds two critical references: a qrcode property containing the underlying QRCode object (from the QRCodeSwift dependency) and a style property implementing the EFQRCodeStyleBase protocol.
Key Initializers and Methods
The generator provides multiple convenience initializers to accommodate different input types:
init(_ text: String, encoding:, errorCorrectLevel:, style:)— For string content with specified encodinginit(_ data: Data, errorCorrectLevel:, style:)— For raw binary datainit(_ qrcode: QRCode, style:)— For existing QR code objects
Each initializer validates the input and constructs the internal QRCode instance with the specified error correction level.
Output Methods
Once initialized, the generator offers several export methods defined in the same file:
toImage(width:)— Returns aCGImageof the specified widthtoPNGData(width:)— Returns PNG binary datatoSVGString(width:)— Returns an SVG string representation
These methods delegate to the style implementation to render the QR matrix into the desired format, making EFQRCode.Generator the definitive entry point for all EFQRCode generation operations.
EFQRCode Recognition Entry Point
For decoding QR codes from images, the library exposes EFQRCode.Recognizer, which wraps Core Image’s detection capabilities.
The Recognizer Class Structure
Located in Source/EFQRCode+Recognizer.swift, the recognizer is implemented as:
public extension EFQRCode {
public class Recognizer {
public init(image: CGImage)
public func recognize() -> [String]
// ...
}
}
The class requires a CGImage at initialization and maintains internal state to cache detection results.
Recognition Workflow
The recognition process follows a simple two-step pattern:
- Initialization — Pass a
CGImagetoEFQRCode.Recognizer(image:) - Extraction — Call
recognize()to return an array of decoded strings
Under the hood, the implementation uses Core Image’s CIDetector with the CIDetectorTypeQRCode type to locate and decode QR codes within the image bounds. The results are cached, allowing repeated calls to recognize() without re-processing the image.
This design makes EFQRCode.Recognizer the primary entry point for all EFQRCode recognition tasks, providing a clean Swift wrapper around Core Image’s functionality.
Practical Implementation Examples
The following examples demonstrate the complete workflow for both generation and recognition using the main entry points.
Generating a Styled QR Code
import EFQRCode
import CoreGraphics
// Create a styled generator
do {
let style = EFQRCodeStyle.bubble
let generator = try EFQRCode.Generator(
"https://github.com/efprefix/efqrcode",
style: style
)
// Export as PNG
let pngData = try generator.toPNGData(width: 400)
try pngData.write(to: URL(fileURLWithPath: "/tmp/qrcode.png"))
} catch {
print("Generation failed: \(error)")
}
Recognizing QR Codes from an Image
import EFQRCode
import CoreGraphics
// Load an image (example uses NSImage, UIImage works similarly)
if let cgImage = NSImage(named: "sampleQR")?.cgImage(forProposedRect: nil, context: nil, hints: nil) {
// Initialize the recognizer
let recognizer = EFQRCode.Recognizer(image: cgImage)
// Extract contents
let decoded = recognizer.recognize()
print("Found \(decoded.count) QR code(s):", decoded)
}
Summary
- EFQRCode serves as an empty namespace class defined in
Source/EFQRCode.swift, providing the organizational structure for the library. - EFQRCode.Generator in
Source/EFQRCode+Generator.swiftis the main entry point for QR code generation, offering initializers for text and data input along with export methods for images, PNG data, and SVG strings. - EFQRCode.Recognizer in
Source/EFQRCode+Recognizer.swiftis the main entry point for QR code recognition, wrapping Core Image’sCIDetectorto extract string payloads fromCGImageinputs. - Both classes are accessed through the
EFQRCodestatic namespace, creating a clean, discoverable API surface for Swift developers.
Frequently Asked Questions
Where is the EFQRCode Generator class defined?
The EFQRCode.Generator class is defined in Source/EFQRCode+Generator.swift as a public class nested inside a public extension EFQRCode block. This file contains all generation logic, including initializers for text and data input, style configuration, and output methods like toPNGData(width:) and toSVGString(width:).
How does EFQRCode recognize QR codes from images?
EFQRCode recognizes QR codes through the EFQRCode.Recognizer class located in Source/EFQRCode+Recognizer.swift. The class initializes with a CGImage and uses Core Image’s CIDetector with the CIDetectorTypeQRCode type to detect and decode QR codes. The recognize() method returns an array of strings containing the decoded payloads.
What is the purpose of the empty EFQRCode class?
The empty EFQRCode class in Source/EFQRCode.swift serves as a namespace container. It contains no properties or methods of its own, but provides the organizational structure for the library’s public API. By defining Generator and Recognizer as nested classes within extensions on EFQRCode, the library creates a clean, discoverable interface where users access functionality through EFQRCode.Generator and EFQRCode.Recognizer.
Can I use EFQRCode to generate QR codes with custom styles?
Yes, the EFQRCode.Generator class supports extensive styling through the EFQRCodeStyleBase protocol. When initializing a generator, you can pass a style implementation such as EFQRCodeStyle.bubble or create custom styles by conforming to EFQRCodeStyleBase. The generator stores the style in its public let style: EFQRCodeStyleBase property and applies it when rendering output through methods like toImage(width:) or toSVGString(width:).
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 →