Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QR Code reader & generator in Swift

Tags:

swift

Currently in Objective-C, i use Zbar(http://zbar.sourceforge.net/) to generate & read QR Code. Now, i want to move on Swift development only, is there any 'how to' or library about generate and read QR Code in Swift ?

like image 607
tesmojones Avatar asked Sep 24 '14 02:09

tesmojones


People also ask

Can I scan a QR code without an app?

To scan a QR Code with Google Screen Search, you don't need an app. You can use the following steps to scan a QR Code: Point your camera at the QR Code. Hold down the “Home” button and swipe up to reveal the options at the bottom.

How do I view a QR code?

On your compatible Android phone or tablet, open the built-in camera app. Point the camera at the QR code. Tap the banner that appears on your Android phone or tablet. Follow the instructions on the screen to finish signing in.

Is there a QR scanner on my phone?

Android 9 and Android 10 have an in-built QR code reader courtesy of Google Lens. Consumers have to open their camera app and point it at the QR Code and see a URL pop-up.

Which QR code readers are free?

QR Reader for Android is one best QR code scanner apps for android in 2022. This app has a quick and responsive reading capacity as well, and you can also download it for free. It includes different features as well, such as scanning QR codes in real-time using your Android camera.


1 Answers

Beginning with iOS 7, iOS devices can read QR codes. However, there is no built-in way to generate a QR code. Here's a quick and dirty QR code reader.

First, import AVFoundation and add AVCaptureMetadataOutputObjectsDelegate

Next, setup the capture session:

func captureQRCode() {
    let session = AVCaptureSession()
    let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

    let input = AVCaptureDeviceInput.deviceInputWithDevice(device, error: nil) as AVCaptureDeviceInput
    session.addInput(input)

    let output = AVCaptureMetadataOutput()
    output.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
    session.addOutput(output)
    output.metadataObjectTypes = [AVMetadataObjectTypeQRCode]

    let previewLayer = AVCaptureVideoPreviewLayer(session: session)
    let bounds = self.view.layer.bounds
    previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
    previewLayer.bounds = bounds
    previewLayer.position = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds))

    self.view.layer.addSublayer(previewLayer)
    session.startRunning()
}

Finally, handle the delegate

func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {
    for item in metadataObjects {
        if let metadataObject = item as? AVMetadataMachineReadableCodeObject {
            if metadataObject.type == AVMetadataObjectTypeQRCode {
                println("QR Code: \(metadataObject.stringValue)")
            }
        }
    }
}
like image 81
Ron Fessler Avatar answered Nov 03 '22 02:11

Ron Fessler