How to capture depth data from camera in iOS 11 and Swift 4?

后端 未结 4 777
一个人的身影
一个人的身影 2020-12-09 06:36

I\'m trying to get depth data from the camera in iOS 11 with AVDepthData, tho when I setup a photoOutput with the AVCapturePhotoCaptureDelegate the photo.depthData is nil.

4条回答
  •  暖寄归人
    2020-12-09 07:05

    First, you need to use the dual camera, otherwise you won't get any depth data.

    let device = AVCaptureDevice.default(.builtInDualCamera, for: .video, position: .back)
    

    And keep a reference to your queue

    let dataOutputQueue = DispatchQueue(label: "data queue", qos: .userInitiated, attributes: [], autoreleaseFrequency: .workItem)
    

    You'll also probably want to synchronize the video and depth data

    var outputSynchronizer: AVCaptureDataOutputSynchronizer?
    

    Then you can synchronize the two outputs in your viewDidLoad() method like this

    if sessionOutput?.isDepthDataDeliverySupported {
        sessionOutput?.isDepthDataDeliveryEnabled = true
        depthDataOutput?.connection(with: .depthData)!.isEnabled = true
        depthDataOutput?.isFilteringEnabled = true
        outputSynchronizer = AVCaptureDataOutputSynchronizer(dataOutputs: [sessionOutput!, depthDataOutput!])
        outputSynchronizer!.setDelegate(self, queue: self.dataOutputQueue)
    }
    

    I would recommend watching WWDC session 507 - they also provide a full sample app that does exactly what you want.

    https://developer.apple.com/videos/play/wwdc2017/507/

提交回复
热议问题