Using the CoreImage framework, one can easily generate Bar/QR Codes within an iOS app with very few lines of code. Using the CoreImage filter, specifically the 'CICode128BarcodeGenerator' for Bar code and ‘CIQRCodeGenerator‘ for QR Code filter, we can get a CIImage that can be convert to UIImage for usage. Please visit Core Image Filter Reference for more information.
Following are supported CICategoryGenerator:
- CIAztecCodeGenerator
- CICheckerboardGenerator
- CICode128BarcodeGenerator
- CIConstantColorGenerator
- CILenticularHaloGenerator
- CIPDF417BarcodeGenerator
- CIQRCodeGenerator
- CIRandomGenerator
- CIStarShineGenerator
- CIStripesGenerator
- CISunbeamsGenerator
func generateBarCode(_ string: String) -> UIImage {
if !string.isEmpty {
let data = string.data(using: String.Encoding.ascii)
let filter = CIFilter(name: "CICode128BarcodeGenerator")
// Check the KVC for the selected code generator
filter.setValue(data, forKey: "inputMessage")
let transform = CGAffineTransform(scaleX: 10, y: 10)
let output = filter.outputImage?.transformed(by: transform)
return UIImage(ciImage: output!)
} else {
return UIImage()
}
}
func generateQRCode(_ string: String) -> UIImage {
if !string.isEmpty {
let data = string.data(using: String.Encoding.ascii)
let filter = CIFilter(name: "CIQRCodeGenerator")
// Check the KVC for the selected code generator
filter.setValue(data, forKey: "inputMessage")
let transform = CGAffineTransform(scaleX: 10, y: 10)
let output = filter.outputImage?.transformed(by: transform)
return UIImage(ciImage: output!)
} else {
return UIImage()
}
}
Using AVFoundation framework we can scan all kinds of market leading bar code formats. Please visit AVMetadataObject.ObjectType for all supported formats on iOS.
Following are supported scanning formats:
- AVMetadataObject.ObjectType.aztec
- AVMetadataObject.ObjectType.code128
- AVMetadataObject.ObjectType.code39
- AVMetadataObject.ObjectType.code39Mod43
- AVMetadataObject.ObjectType.code93
- AVMetadataObject.ObjectType.dataMatrix
- AVMetadataObject.ObjectType.ean13
- AVMetadataObject.ObjectType.ean8
- AVMetadataObject.ObjectType.face
- AVMetadataObject.ObjectType.interleaved2of5
- AVMetadataObject.ObjectType.itf14
- AVMetadataObject.ObjectType.pdf417
- AVMetadataObject.ObjectType.qr
- AVMetadataObject.ObjectType.upce