/CodeGeneratorAndScanner

Bar code and QR code generator and scanner built in Swift

Primary LanguageSwift

CodeGeneratorAndScanner

How to generate Bar code or QR code on iOS using native iOS APIs?

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:

Bar Code Example

  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()
        }
  }

QR Code Example

  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()
        }
  }
    

How to scan Bar code or QR code on iOS using native iOS APIs?

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: