代码之家  ›  专栏  ›  技术社区  ›  Emre Önder

iOS 9.0的AVCaptureDevice发现会话

  •  0
  • Emre Önder  · 技术社区  · 6 年前

    我正在开发一个二维码和矩阵码阅读器应用程序。我得到了 AVCaptureDeviceInput AvCaptureDevice.DiscoverySession . 我的问题是它只有在iOS 10.0之后才可用。我怎样才能得到它的回退版本?

    // Get the back-facing camera for capturing videos
    if #available(iOS 10.0, *) {
        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
        }
    
    } else {
        // Fallback on earlier versions
    }
    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)
    
        //TODO: Decide the data types!
        captureMetadataOutput.metadataObjectTypes = [AVMetadataObject.ObjectType.dataMatrix]
        //captureMetadataOutput.metadataObjectTypes = [AVMetadataObject.ObjectType.qr]
    
    } catch {
        // If any error occurs, simply print it out and don't continue any more.
        print(error)
        return
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Nguyễn Đức    6 年前

    如果你想得到 AVCaptureDevice ,您可以使用

    let cameras = AVCaptureDevice.devices(for: AVMediaType.video)
    

    在里面 iOS 9