iOS Camera focus value

后端 未结 2 1422
时光说笑
时光说笑 2021-01-16 11:52

Is there any way to get the focus value from iPhone camera with autofocus?

I want to use this data to calculate the distance from iPhone to an object in focus.

相关标签:
2条回答
  • 2021-01-16 12:13

    Obviously this is an old question, but as there is an option to get a "lense value" since iOS8 it should appear here.

    Since iOS8 you can get the focus value from the lense by key-value observing lensPosition. It is a property of the AVCaptureDevice class which is part of the AVFoundation framework.

    So somewhere in your camera class set the observer:

    // Assuming _device is an object of the `AVCaptureDevice` class
    [_device addObserver:self forKeyPath:@"lensPosition" options:NSKeyValueObservingOptionNew context:nil];
    

    And in the class you used as observer:

    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
        if ([keyPath isEqualToString:@"lensPosition"]) {
            NSLog(@"change: %@", change);
            NSLog(@"lens position: %f", [change[@"new"] floatValue]);
        }
    }
    

    The position of the lens will be displayed as a scalar value from 0 to 1. Also you can set the lens position manually. You can find out more about managing the lens position in the Apple Documentation.

    Finally as with all key-value observer don't forget to remove the observer.

    NOTE: The lens is a mechanical part in the device and focusing is done by moving the lens via a spring. So values differ depending on the device and situation.

    0 讨论(0)
  • 2021-01-16 12:36

    I do not think there is such a thing as a focus value.

    0 讨论(0)
提交回复
热议问题