Search code examples
iosswiftavcapturedevice

Capture frames from both cameras (TelePhoto and WideAngle) in the same time


It's possible to get frame from TelephotoCamera, WideAngleCamera or automatically using DualCamera:

//auto
let device = AVCaptureDevice.default(.builtInDualCamera, for: .video, position: .back)
//telephoto
let device = AVCaptureDevice.default(.builtInTelephotoCamera, for: .video, position: .back)
//wideAngle
let device = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back)

But how to get frames from TelephotoCamera and WideAngleCamera in the same time (using devices with dual cameras).


Edit1
Tried to add 2 inputs to capture session:

captureSession?.addInput(input)
captureSession?.addInput(input2)

NSInvalidArgumentException: Multiple audio/video AVCaptureInputs are not currently supported

Edit2
Tried to enable isDualCameraDualPhotoDeliveryEnabled flag for capture photo output:

capturePhotoOutput?.isDualCameraDualPhotoDeliveryEnabled = true

NSInvalidArgumentException: Dual Camera dual photo delivery is not supported in this configuration

But
isDualCameraDualPhotoDeliverySupported gives false on iPhone 8 Plus
could it be the reason of my problem?


Still no luck.


Solution

  • Yes, it's possible.
    Just pick the right configuration.


    Capture device should be builtInDualCamera:

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

    Configure capture photo output after configuring capture session:

    captureSession = AVCaptureSession()
    captureSession?.sessionPreset = AVCaptureSession.Preset.photo
    captureSession?.addInput(input)
    captureSession?.addOutput(capturePhotoOutput!)
    
    capturePhotoOutput?.isHighResolutionCaptureEnabled = true
    

    capturePhotoOutput?.isDualCameraDualPhotoDeliveryEnabled = true


    Configure photo settings before capture the photo:

    let photoSettings = AVCapturePhotoSettings()
    photoSettings.isAutoStillImageStabilizationEnabled = true
    photoSettings.isHighResolutionPhotoEnabled = true
    photoSettings.isAutoDualCameraFusionEnabled = false
    

    photoSettings.isDualCameraDualPhotoDeliveryEnabled = true


    Implement AVCapturePhotoCaptureDelegate, and override next method:

    public func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?)
    

    You will receive 2 photoOutput callbacks!