代码之家  ›  专栏  ›  技术社区  ›  Ido Naveh

找到不支持的类型-AvMetadataObject

  •  1
  • Ido Naveh  · 技术社区  · 8 年前

    我正试图在iOS应用程序中实现条形码扫描,但出于某种原因,我无法打开相机。我正在使用苹果的AVFoundation框架进行扫描。

    这是我启动相机和实现扫描仪的代码:

    // Get the back-facing camera for capturing videos
    let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, 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 = [AVMetadataObject.ObjectType.qr]
    
        // Initialize the video preview layer and add it as a sublayer to the viewPreview view's layer.
        videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession!)
        videoPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
        videoPreviewLayer?.frame = view.layer.bounds
        view.layer.addSublayer(videoPreviewLayer!)
    
        // Start video capture.
        captureSession?.startRunning()
    
        // Initialize QR Code Frame to highlight the QR code
        qrCodeFrameView = UIView()
    
        if let qrCodeFrameView = qrCodeFrameView {
            qrCodeFrameView.layer.borderColor = UIColor.green.cgColor
            qrCodeFrameView.layer.borderWidth = 2
            view.addSubview(qrCodeFrameView)
            view.bringSubview(toFront: qrCodeFrameView)
        }
    } catch {
        // If any error occurs, simply print it out and don't continue any more.
        print(error)
        return
    }
    

    屏幕加载后,立即将以下消息打印到控制台:

    *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[AVCaptureMetadataOutput setMetadataObjectTypes:] Unsupported type found - use -availableMetadataObjectTypes'
    *** First throw call stack:
    (0x1824d2d8c 0x18168c5ec 0x18803eb44 0x1013f035c 0x1013f12ec 0x18c23e64c 0x18c35f870 0x18c244700 0x18c37a1a8 0x18c2c19e0 0x18c2b6890 0x18c2b51d0 0x18ca96d1c 0x18ca992c8 0x18ca92368 0x18247b404 0x18247ac2c 0x18247879c 0x182398da8 0x18437d020 0x18c3b5758 0x1013da1d8 0x181e29fc0)
    libc++abi.dylib: terminating with uncaught exception of type NSException
    (lldb) 
    

    我在堆栈溢出或任何其他网站上都找不到任何解决方案。

    你能帮我修一下吗?

    1 回复  |  直到 7 年前
        1
  •  3
  •   Bhavya Kaushal    7 年前

    所以我有同样的错误,我通过做一些小的改变来解决它。希望这有帮助(如果你还没有解决!).

        let input = try AVCaptureDeviceInput(device: captureDevice)
    
        //Make sure you have initialized the captureSession object
            captureSession = AVCaptureSession()
            captureSession?.addInput(input)
    
            videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession!)
            videoPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
            videoPreviewLayer?.frame = view.layer.bounds
            view.layer.addSublayer(videoPreviewLayer!)
    
            let captureMetadataOutput = AVCaptureMetadataOutput()
            captureSession?.addOutput(captureMetadataOutput)
    
    
           // Main Change
            captureMetadataOutput.metadataObjectTypes = captureMetadataOutput.availableMetadataObjectTypes
                //[AVMetadataObject.ObjectType.qr]
    
            captureMetadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
    
    
            captureSession?.startRunning()