未调用AVCaptureMetadataOutputObjectsDelegate方法

时间:2018-01-29 21:50:23

标签: ios swift swift4 avcapturesession

我有一个ViewController,可以连续扫描QR码并实现AVCaptureMetadataOutputObjectsDelegate来检索元数据输出。

class ScanViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {

    var captureSession = AVCaptureSession()
    var videoPreviewLayer: AVCaptureVideoPreviewLayer?

    override func viewDidLoad() {
        super.viewDidLoad()

        // Get the back-facing camera for capturing videos
        let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInDualCamera], mediaType: AVMediaTypeVideo, position: .back)

        guard let captureDevice = deviceDiscoverySession?.devices.first else {
            print("Failed to get the camera device")
            return
        }

        do {
            // Get an instance of the AVCaptureDeviceInput class using the previous device object.
            let input = try AVCaptureDeviceInput(device: captureDevice)

            // Set the input device on the capture session.
            captureSession.addInput(input)

            // Initialize a AVCaptureMetadataOutput object and set it as the output device to the capture session.
            let captureMetadataOutput = AVCaptureMetadataOutput()
            captureSession.addOutput(captureMetadataOutput)

            // Set delegate and use the default dispatch queue to execute the call back
            captureMetadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
            captureMetadataOutput.metadataObjectTypes = [AVMetadataObjectTypeQRCode]

        } catch {
            // If any error occurs, simply print it out and don't continue any more.
            print(error)
            return
        }

        // Initialize the video preview layer and add it as a sublayer to the viewPreview view's layer.
        videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
        videoPreviewLayer?.frame = view.layer.bounds
        view.layer.addSublayer(videoPreviewLayer!)

        // Start video capture.
        captureSession.startRunning()
    }

    func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
        print("in function of extension")
        // Check if the metadataObjects array is not nil and it contains at least one object.
        if metadataObjects.count == 0 {
            print("nada qr code")
            return
        }

        // Get the metadata object.
        let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject

        if metadataObj.type == AVMetadataObjectTypeQRCode {
            if metadataObj.stringValue != nil {
                print(metadataObj.stringValue)
            }
        }
    }
}

但由于某种原因,委托回调没有被调用。在the tutorial I'm following中,它运行得很好。

希望有人可以提供任何帮助。任何能让我走上正轨的东西都会非常有帮助。非常感谢提前。

3 个答案:

答案 0 :(得分:1)

不运行的原因是RAISE NOTICE '%', firstrecord.id;在技术上是AV框架的内置函数,但仅适用于Swift 4.在Swift 3.0和3.2中,正​​确的功能是:

metadataOutput(...)

应该运行!

答案 1 :(得分:0)

您发布的扩展程序代码不能为我编译。我必须在方法声明中添加public

public func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {

我正在使用Swift 4。

答案 2 :(得分:0)

如下更改代码。它可能有用,至少对我有用。我正在使用Swift 4.2

captureMetadataOutput.metadataObjectTypes = captureDeviceOutput.availableMetadataObjectTypes