本文介绍了AVFoundation元数据对象类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用AVFoundation使用以下代码读取条形码,但我不断收到以下错误。帮助为什么会非常感激。提前致谢!

I'm trying to use the AVFoundation to read barcodes with the below code, but I keep getting the error below. Help as to why would be much appreciated. Thanks in advance!

//Create camera view
        session = AVCaptureSession()
        var layer = self.cameraView.layer
        vidLayer = AVCaptureVideoPreviewLayer.layerWithSession(session) as AVCaptureVideoPreviewLayer
        vidLayer.frame = self.cameraView.bounds
        vidLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
        var device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
        var error:NSError? = nil
        var input:AVCaptureDeviceInput? = AVCaptureDeviceInput.deviceInputWithDevice(device, error: &error) as? AVCaptureDeviceInput
        var output = AVCaptureMetadataOutput();
        session.addOutput(output)
        output.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
        output.metadataObjectTypes = [AVMetadataObjectTypeUPCECode, AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode39Mod43Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code, AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeQRCode, AVMetadataObjectTypeAztecCode]
        if(input != nil){
            println("ADDED")
            session.addInput(input)
        }
        layer.addSublayer(vidLayer)
        session.startRunning()

2014-10-07 15:25:09.279 WegmannMobile[457:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[AVCaptureMetadataOutput setMetadataObjectTypes:] - unsupported type found.  Use -availableMetadataObjectTypes.'
*** First throw call stack:
(0x2d7edf83 0x38068ccf 0x2c72bc29 0x618f0 0x62060 0x300256df 0x3019b43f 0x300b8d63 0x300b8b6d 0x300b8b05 0x3000ad59 0x2fc8862b 0x2fc83e3b 0x2fc83ccd 0x2fc836df 0x2fc834ef 0x2fc7d21d 0x2d7b9255 0x2d7b6bf9 0x2d7b6f3b 0x2d721ebf 0x2d721ca3 0x3267b663 0x3006e14d 0x9eff4 0x9f030 0x38575ab7)
libc++abi.dylib: terminating with uncaught exception of type NSException


推荐答案

在设置元数据对象类型之前,尝试将输入和输出添加到会话中。

Try adding both the input and output to the session before setting the metadata object types.

当您没有将相机连接到会话, availableMetadataObjectTypes 将为空。

When you don't have the camera attached to the session yet, availableMetadataObjectTypes will be empty.

这篇关于AVFoundation元数据对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 19:58