-->

Is there any way to programmatically set the camer

2020-05-26 12:31发布

问题:

I am creating an app that locks the camera focus for video recording purposes. I want to lock the focus to infinity without having the user manually adjust the focus. Is this possible? Thanks!

回答1:

That is the way to disable focus, it locks it for all the time:

// Find a suitable capture device
    AVCaptureDevice *cameraDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    // SETUP FOCUS MODE
    if ([cameraDevice lockForConfiguration:nil]) {

        [cameraDevice setFocusMode:AVCaptureFocusModeLocked];

        [cameraDevice unlockForConfiguration];
    }
    else{
        NSLog(@"error while configuring focusMode");
    }

What do you mean "lock the focus to infinity"?



回答2:

Sadly, no. You can set the camera into focus-locked mode, as Artem said; put into autofocus mode (focus, then lock), or continuous autofocus mode, but you can't give the camera a specific distance to focus at.

The best I've been able to come up with (for exposure and white balance control, which are similarly limited) is to have the user point the camera at an appropriate scene (in your case, something far away) and have him/her push a lock button.

Details on the capture device API, such as it is: https://developer.apple.com/library/mac/#documentation/AVFoundation/Reference/AVCaptureDevice_Class/Reference/Reference.html



回答3:

AVCaptureDevice has function setFocusModeLockedWithLensPosition:completionHandler:

You can use it to set 1.0 in order to achieve "infinity" distance

    func focusTo(value : Float) {
      if let device = captureDevice {
      if(device.lockForConfiguration(nil)) {
         device.setFocusModeLockedWithLensPosition(value, completionHandler: { (time) -> Void in
            //
        })
        device.unlockForConfiguration()
      }
    }

Update: According to Apple documentation 1.0 does not represent focus at infinity.