junet junet - 2 months ago 42
Swift Question

Code for capturing QR Code doesn't seem working well in Xcode8 and Swift3

I'm trying to capture QR code using

AVCaptureMetadataOutputObjectsDelegate
in Swift3 and iOS 10 beta device.

But
captureOutput
function is not called when QR code is actually captured by device's camera.

Does anyone have a solution for this? If so, please let me know how to fix and work properly.




Thanks,

import UIKit
import AVFoundation

class ViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {

override func viewDidLoad() {
super.viewDidLoad()

let mySession: AVCaptureSession! = AVCaptureSession()
let devices = AVCaptureDevice.devices()
var myDevice: AVCaptureDevice!

for device in devices! {
if((device as AnyObject).position == AVCaptureDevicePosition.back){
myDevice = device as! AVCaptureDevice
}
}

let myVideoInput = try! AVCaptureDeviceInput.init(device: myDevice)

if mySession.canAddInput(myVideoInput) {
mySession.addInput(myVideoInput)
}

let myMetadataOutput: AVCaptureMetadataOutput! = AVCaptureMetadataOutput()

if mySession.canAddOutput(myMetadataOutput) {
mySession.addOutput(myMetadataOutput)
myMetadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
myMetadataOutput.metadataObjectTypes = [AVMetadataObjectTypeQRCode]
}

let myVideoLayer = AVCaptureVideoPreviewLayer.init(session: mySession)
myVideoLayer?.frame = self.view.bounds
myVideoLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
self.view.layer.addSublayer(myVideoLayer!)

mySession.startRunning()
}


// MARK: - AVCaptureMetadataOutputObjectsDelegate

// This function is not called despite capturing QR code
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, from connection: AVCaptureConnection!) {
if metadataObjects.count > 0 {
let qrData: AVMetadataMachineReadableCodeObject = metadataObjects[0] as! AVMetadataMachineReadableCodeObject
print("\(qrData.type)")
print("\(qrData.stringValue)")

}
}

}

Answer

The method header to implement AVCaptureMetadataOutputObjectsDelegate in Swift 3 has become like this:

func captureOutput(_ captureOutput: AVCaptureOutput!,
                   didOutputMetadataObjects metadataObjects: [Any]!,
                   from connection: AVCaptureConnection!) {

[AnyObject] -> [Any], I haven't checked other parts of your code, but at least, you need to fix this.