How can i synchronize AVCaptureDevice setFocusModeLockedWithLensPosition call

837 views Asked by At

I want to synchronize setFocusModeLockedWithLensPosition, setWhiteBalanceModeLockedWithDeviceWhiteBalanceGains and setExposureModeCustomWithDuration calls.

Is there a logic order to call thoses functions ?

What i want to do is to start Running session when i am sure that focus, balance and exposure are properly set (i want to set values, not in automatic)

I have tried to lock the configuration, then call the 3 functions, then unlock, then startRunning on session. I put nil in the 3 completion handler parameters. What i see in this case is that my image preview is not pretty (kind of blue filter). I have to wait before having a good image quality. What i want is to display the image only when it is good. I want do be notified.

So i tried to cascade my 3 calls with completion handler. in some cases, the completion handler is not called. I suppose this is when i want to put my lens position to 0.4 and the current lens position is 0.4.

So i don't know which is the best method.

Thanks

1

There are 1 answers

2
Tuc3k On

You can set your camera options in completion handler like this. It will wait till focus has been set to set exposure and the same principle will work with white balance and exposure. You can read more about camera setting here.

var AVCGains:AVCaptureWhiteBalanceGains = AVCaptureWhiteBalanceGains()
AVCGains.redGain = 1.0;
AVCGains.greenGain = 1.0;
AVCGains.blueGain = 1.0;

self.camera?.focusMode = .locked
self.camera?.exposureMode = .locked
self.camera?.whiteBalanceMode = .locked

self.camera?.setFocusModeLockedWithLensPosition(focus_point, completionHandler: {(timestamp:CMTime) -> Void in   
        print("Focus applied")
        self.camera?.setExposureModeCustomWithDuration(CMTimeMake(1,10), iso: 100, completionHandler: {(timestamp:CMTime) -> Void in 
              print("Exposure applied")
              self.camera?.setWhiteBalanceModeLockedWithDeviceWhiteBalanceGains(AVCGains, completionHandler: {(timestamp:CMTime) -> Void in
                      print("White Balance applied")
                      // All settings have been applied, start running session 
              })
        })
})