swift | avfoundation | barcode | qrcode

Swift: Barcode & QR Code scanner

by Nikhil GohilJanuary 29, 2019 · 2 min read · Last Updated:

There are so many types of barcode and a qr code, iOS supports a qrcode and barcodes scanning by AVFoundation

Supported code types

Swift Project

1. Decide AVMetadataObject.ObjectType

There are multipel types of code we can scan using AVFoundation.

We can create a variable for AVMetadataObject.ObjectType and assign value to get the expected scan results.

1switch value {
2 case AVMetadataObject.ObjectType.upce.rawValue:
3 self.codes = [AVMetadataObject.ObjectType.upce]
4 case AVMetadataObject.ObjectType.code39.rawValue:
5 self.codes = [AVMetadataObject.ObjectType.code39]
6 case AVMetadataObject.ObjectType.code39Mod43.rawValue:
7 self.codes = [AVMetadataObject.ObjectType.code39Mod43]
8 case AVMetadataObject.ObjectType.code93.rawValue:
9 self.codes = [AVMetadataObject.ObjectType.code93]
10 case AVMetadataObject.ObjectType.code128.rawValue:
11 self.codes = [AVMetadataObject.ObjectType.code128]
12 case AVMetadataObject.ObjectType.ean8.rawValue:
13 self.codes = [AVMetadataObject.ObjectType.ean8]
14 case AVMetadataObject.ObjectType.ean13.rawValue:
15 self.codes = [AVMetadataObject.ObjectType.ean13]
16 case AVMetadataObject.ObjectType.aztec.rawValue:
17 self.codes = [AVMetadataObject.ObjectType.aztec]
18 case AVMetadataObject.ObjectType.pdf417.rawValue:
19 self.codes = [AVMetadataObject.ObjectType.pdf417]
20 case AVMetadataObject.ObjectType.itf14.rawValue:
21 self.codes = [AVMetadataObject.ObjectType.itf14]
22 case AVMetadataObject.ObjectType.dataMatrix.rawValue:
23 self.codes = [AVMetadataObject.ObjectType.dataMatrix]
24 case AVMetadataObject.ObjectType.qr.rawValue:
25 self.codes = [AVMetadataObject.ObjectType.qr]
26 case AVMetadataObject.ObjectType.interleaved2of5.rawValue:
27 self.codes = [AVMetadataObject.ObjectType.interleaved2of5]
28 default:
29 self.codes = [AVMetadataObject.ObjectType.upce,
30 AVMetadataObject.ObjectType.code39,
31 AVMetadataObject.ObjectType.code39Mod43,
32 AVMetadataObject.ObjectType.code93,
33 AVMetadataObject.ObjectType.code128,
34 AVMetadataObject.ObjectType.ean8,
35 AVMetadataObject.ObjectType.ean13,
36 AVMetadataObject.ObjectType.aztec,
37 AVMetadataObject.ObjectType.pdf417,
38 AVMetadataObject.ObjectType.itf14,
39 AVMetadataObject.ObjectType.dataMatrix,
40 AVMetadataObject.ObjectType.interleaved2of5,
41 AVMetadataObject.ObjectType.qr]
42 }

2. Create capture session

Now create capture session and assign codes value to metadataObjectTypes

1let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .back)
2
3 guard let captureDevice = deviceDiscoverySession.devices.first else {
4 print("Failed to get the camera device")
5 return
6 }
7
8 do {
9 let input = try AVCaptureDeviceInput(device: captureDevice)
10 captureSession.addInput(input)
11 let captureMetadataOutput = AVCaptureMetadataOutput()
12 captureSession.addOutput(captureMetadataOutput)
13
14 captureMetadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
15 captureMetadataOutput.metadataObjectTypes = codes
16
17 } catch {
18 // If any error occurs, simply print it out and don't continue any more.
19 print(error)
20 return
21 }

3. Start running session

Next step is to to start a running session using AVCaptureVideoPreviewLayer

1cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
2 cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
3 cameraPreviewLayer?.frame = view.layer.bounds
4 view.layer.addSublayer(cameraPreviewLayer!)
5
6 captureSession.startRunning()

https://github.com/nikhilgohil11/G-Scanner

Nikhil Gohil

Written by Nikhil Gohil
More than 500+ problems solved | Technology & programming based solutions, topics and blog 👨🏻‍💻
Buy me a coffee

Was this article helpful?

Your opinion matters

Please share your thought about this article

This page is open source. Noticed a typo? Or something unclear?
Improve this page on GitHub