点按即可对焦,同时保持连续自动对焦

时间:2016-05-24 02:14:40

标签: ios avcapturesession avcapturedevice

我正在实施点按以集中精力,并对如何使用不同的AVCaptureFocusModes感到困惑。这样做:

[device setFocusPointOfInterest:focusPoint];
[device setFocusMode:AVCaptureFocusModeAutoFocus];

导致成功聚焦,但由于我锁定焦距,移动相机将永远失去焦点。相反,如果我这样做:

[device setFocusPointOfInterest:focusPoint];
[device setFocusMode:AVCaptureFocusModeContinousAutoFocus];

自动对焦引擎似乎忽略了我的兴趣点,只关注最好看的东西。相机应用成功地关注您的兴趣点,同时在移动相机时保持连续自动对焦,这是如何完成的?

这是我现在的完整代码:

- (void)setFocusPointOfInterest:(CGPoint)point
{
    Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
    if (captureDeviceClass != nil) {
        AVCaptureDevice *device = [captureDeviceClass defaultDeviceWithMediaType:AVMediaTypeVideo];
        if([device isFocusPointOfInterestSupported] &&
           [device isFocusModeSupported:AVCaptureFocusModeAutoFocus]) {
            CGRect screenRect = [[UIScreen mainScreen] bounds];
            double screenWidth = screenRect.size.width;
            double screenHeight = screenRect.size.height;
            double focus_x = point.x/screenWidth;
            double focus_y = point.y/screenHeight;
            CGPoint focusPoint = CGPointMake(focus_x,focus_y);
            if([device lockForConfiguration:nil]) {
                [device setFocusPointOfInterest:focusPoint];
                [device setFocusMode:AVCaptureFocusModeAutoFocus];
                [device setExposurePointOfInterest:focusPoint];
                if ([device isExposureModeSupported:AVCaptureExposureModeAutoExpose]){
                    [device setExposureMode:AVCaptureExposureModeAutoExpose];
                }
                [device unlockForConfiguration];
            }
        }
    }
}

2 个答案:

答案 0 :(得分:5)

系统本身可以检测到图像中的重大变化。您所要做的就是说lazyisSubjectAreaChangeMonitoringEnabled,然后注册true,如下所示:

AVCaptureDeviceSubjectAreaDidChangeNotification

调用方法后 - 只需切换到captureDevice.isSubjectAreaChangeMonitoringEnabled = true NotificationCenter.default.addObserver(self, selector: #selector(subjectAreaDidChange), name: NSNotification.Name.AVCaptureDeviceSubjectAreaDidChange, object: nil) 即可:

.continuousAutoFocus

答案 1 :(得分:2)

允许点击以对焦模式AVCaptureFocusModeAutoFocus进行对焦,一旦相机完成对焦于您设定的点,请尝试将对焦模式设定回AVCaptureFocusModeContinuousAutoFocus。这有点棘手,因为聚焦需要一些时间。在相机应用程序中,当焦点对准时,您将看到预览的小效果。

关键是,您可以弄清楚并且焦点会随着键值观察而变化,一旦您知道焦点是通过自动对焦完成的,您可以将其重置为连续自动对焦。

当你开始相机会话时,将一个键值观察者添加到AVCaptureDevice

[device addObserver:self forKeyPath:@"adjustingFocus" options:NSKeyValueObservingOptionNew context:nil];

当键值更改为新值时,您将知道设备是否正在聚焦。当它没有改变焦点到连续自动对焦时。

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if( [keyPath isEqualToString:@"adjustingFocus"] ){
        BOOL adjustingFocus = [ [change objectForKey:NSKeyValueChangeNewKey] isEqualToNumber:[NSNumber numberWithInt:1]];
        if (!adjustingFocus) {
                // Reset the focus mode to AVCaptureFocusModeContinuousAutoFocus here
        }
    }
}

现在它将完全像相机应用程序。我从很老的答案得到了这个答案,但是在这里找不到它。