点按以保持连续自动对焦

Dan*_*son 3 ios avcapturesession avcapturedevice

我正在实施水龙头以集中精力,并为如何使用不同而感到困惑AVCaptureFocusModes。这样做:

[device setFocusPointOfInterest:focusPoint];
[device setFocusMode:AVCaptureFocusModeAutoFocus];
Run Code Online (Sandbox Code Playgroud)

可以成功对焦,但是由于锁定焦距,移动相机将永远失去对焦。相反,如果我这样做:

[device setFocusPointOfInterest:focusPoint];
[device setFocusMode:AVCaptureFocusModeContinousAutoFocus];
Run Code Online (Sandbox Code Playgroud)

自动对焦引擎似乎消除了我的兴趣,只专注于看起来最好的东西。相机应用程序成功聚焦在您的兴趣点上,同时在移动相机时也保持连续自动对焦,这是怎么做的?

到目前为止,这是我的完整代码:

- (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];
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Sob*_*man 5

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

captureDevice.isSubjectAreaChangeMonitoringEnabled = true
NotificationCenter.default.addObserver(self,
                                       selector: #selector(subjectAreaDidChange),
                                       name: NSNotification.Name.AVCaptureDeviceSubjectAreaDidChange,
                                       object: nil)
Run Code Online (Sandbox Code Playgroud)

一旦您的方法被调用-只需.continuousAutoFocus像这样切换:

do {
    try captureDevice.lockForConfiguration()
} catch let error as NSError {
    print("error: \(error.localizedDescription)")
    return
}
captureDevice.focusMode = .continuousAutoFocus
captureDevice.unlockForConfiguration()
Run Code Online (Sandbox Code Playgroud)