.NET, Mobile, Xamarin

How to get iPhone X front camera depth data in Xamarin iOS?

How to get iPhone X front camera depth data in Xamarin iOS? This question has been raised in many places across the web. However I did not found any good description of this problem including Apple site. They have only quite generic description of required classes without tutorial. As one of the first step, we have to find the camera which will be used. As IR depth camera is in front, we need to choose dual camera (not duo, it is deprecated). In AVCaptureDeviceType, there is BuiltInTrueDepthCamera type but it does not fit for our purpose (it is misleading).

AVCaptureDeviceDiscoverySession discoverySession =
  AVCaptureDeviceDiscoverySession.Create( new AVCaptureDeviceType []
   { AVCaptureDeviceType.BuiltInDualCamera },

var camera = discoverySession.Devices.FirstOrDefault(x =>
   x.DeviceType == AVCaptureDeviceType.BuiltInDualCamera &&
   x.Position == AVCaptureDevicePosition.Front); 

Next step is AVCapturePhotoOuput configuration. AVCapturePhotoSettings has to be configured inlucing DepthDataDeliveryEnabled set to true. In the version which I implemented, I usedEmbedsDepthDataInPhoto = true (optional step). When you assign the configuration, a device (here assigned to camera variable) has to be locked and later unlocked. Otherwise it will not work.

Now we need to setup delegate which will handle saving the data on CapturePhoto invocation. The delegate should inherit from AVCapturePhotoCaptureDelegate and implement (override) the method DidFinishProcessingPhoto. One of its parameter is a type of AVCapturePhoto. This one is the most interesting for us as it contains a property DepthData. Be carefull when you will be trying to get the data from it. The primary purpose of the IR camera is face recognition, not measuring the distance, so when you will try to take a picture of a different object, quite often it may be null (no depth data). It may be caused by low distance to the object or taking a pic of monolithic area.

Now we have photo and depth data. The clue right now is to convert binary data int/float. Each value which you have to decode is on 4 bytes. And that’s it. You are ready to code and invent your own idea what to do with gathered data.

Nice video showing how depth camera is working:https://www.youtube.com/watch?v=g4m6StzUcOw.

Leave a Reply

Your email address will not be published. Required fields are marked *