2016-05-24 15 views
1

フォーカスを合わせるためにタップを実装していますが、異なる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]; 
      } 
     } 
    } 
} 

答えて

1

は、タップはフォーカスモード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 
     } 
    } 
} 

カメラアプリと同じように機能します。私は非常に古い答えからこの答えを得ましたが、ここでそれを参照するために見つけることができませんでした。

+0

何場合:単純にそうよう.continuousAutoFocusに切り替える - あなたのメソッドが呼び出されると

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

:あなたがしなければならないのは、isSubjectAreaChangeMonitoringEnabledtrueであることを言うと、このようAVCaptureDeviceSubjectAreaDidChangeNotificationに登録することですそれを連続AFに設定しておきます。通常のAFと連続AFの間で変化を維持する必要がありますか? –

+0

@ VaddadiKartickその後、フォーカスをタップすると変更されます – Bluewings

+0

申し訳ありませんが、私は理解していません。 –

1

システム自体がイメージの重大な変化を検出できます。

do { 
    try captureDevice.lockForConfiguration() 
} catch let error as NSError { 
    print("error: \(error.localizedDescription)") 
    return 
} 
captureDevice.focusMode = .continuousAutoFocus 
captureDevice.unlockForConfiguration() 
+0

focusPointOfInterestを最初に設定したとき、私はそれを連続AE/AFに設定しました。さもなければ、場面が変わったとき、私は時々通知を得ることはない。または、電話をしっかりと持っている5秒後にそれを入手してください。 –

関連する問題