Is there any way to programmatically set the camer

2020-05-26 11:58发布

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!

3条回答
冷血范
2楼-- · 2020-05-26 12:24

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.

查看更多
We Are One
3楼-- · 2020-05-26 12:27

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"?

查看更多
闹够了就滚
4楼-- · 2020-05-26 12:40

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

查看更多
登录 后发表回答