/QRCodeKit

QRCodeKit is a library for capturing and generating QR code in Swift.

Primary LanguageSwiftMIT LicenseMIT

QRCodeKit

Carthage compatible Version Platform

QRCodeKit is a library for capturing and generating QR code in Swift.

Usage

Capturing

View controller

QRCodeKit provides view controller that captures QR code automatically and delivers that information via delegate method. In addition, it can display video that is captured by camera.

// Present capturing view controller.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let captureViewController = segue.destinationViewController as? QRCodeCaptureViewController {
        captureViewController.delegate = self
    }
}

// Delegate will be called when QR code is recognized.
func qrCodeCaptureCamera(captureCamera: QRCodeCaptureCamera, didCaptureQRCodeMetadataObjects QRCodeMetadataObjects: [AVMetadataMachineReadableCodeObject]) {
    guard let qrCodeObject = QRCodeMetadataObjects.last else { return }
    
    print(qrCodeObject.stringValue)
}

Capture camera class

If you use this class directory, you can more finely control the session or input devices. And the above capture view controller has this class.

// An error will be propagating from initializer of AVCaptureDeviceInput.
let camera = try! QRCodeCaptureCamera(delegate: self)

// Delegate will be called when QR code is recognized
func qrCodeCaptureCamera(captureCamera: QRCodeCaptureCamera, didCaptureQRCodeMetadataObjects QRCodeMetadataObjects: [AVMetadataMachineReadableCodeObject]) {
    guard let qrCodeObject = QRCodeMetadataObjects.last else { return }
    
    print(qrCodeObject.stringValue)
}

// Whether to deliver the same capture results to the delegate. The default value is false.
camera.allowsSameValueCapturing = true

// This class has instance of AVCaptureVideoPreviewLayer
self.view.layer.addSublayer(camera.previewLayer)

// The same as AVCaptureSession#startRunning()
// In addition, focus and exposure are reset, 
// and delivery to the delegate of the capture results will be enabled again.
camera.startSessionRunning()

// The same as AVCaptureSession#stopRunning()
camera.stopSessionRunning()

// An error will be propagating from initializer of AVCaptureDeviceInput.
try! camera.changeCaptureDevice(...)
try! camera.changeCaptureSession(...)

// A session to use in this class has been managed within this queue.
dispatch_async(camera.sessionQueue) {
	configure the session.
}

Generating

Prepare for UIImageView (If �necessary)

It prepares for UIImageView (layer) because size of QR code image generated by the system is small.

let imageView = UIImageView()
imageView.layer.magnificationFilter = kCAFilterNearest

Generate Asynchronously

QRCodeGenerator.generateQRCodeAsynchronously(stringValue: text) { image: UIImage? in
	self.imageView.image = image
}

Generate Synchronously

let generator = QRCodeGenerator(stringValue: text)
imageView.image = generator.image

Specify error correction level

Both generating methods can specify QR code error correction level (Error Correction Feature) by passing "QRCodeErrorCorrectionLevel" enum. The default value is ".H"

QRCodeGenerator.generateQRCodeAsynchronously(stringValue: text, correctionLevel: .M)
QRCodeGenerator(stringValue: text, correctionLevel: .Q)

Requirements

  • Swift 2.0
  • iOS 8.0 or later

Installation

  • Add github "hokuron/QRCodeKit" to Cartfile.
  • Run carthage update.
  • Add the followings to Podfile:
use_frameworks!
pod 'QRCodeKit'
  • Run pod install.

License

QRCodeKit is available under the MIT License.