【问题标题】:iPhone : camera autofocus observer?iPhone:相机自动对焦观察者?
【发布时间】:2012-02-24 09:37:09
【问题描述】:

我想知道是否可以在 iPhone 应用程序中接收有关自动对焦的通知?

IE,是否存在一种在自动对焦开始、结束、成功或失败时收到通知的方法...?

如果有,这个通知名称是什么?

【问题讨论】:

    标签: iphone camera notifications observer-pattern autofocus


    【解决方案1】:

    您可以通过观察AVCaptureDeviceInput.device.isAdjustingFocus 属性,使用现代 Swift 键值观察 api 在聚焦开始和结束时获取回调。在下面的示例中,AVCaptureDeviceInput 的实例称为captureDeviceInput

    例子:

    self.focusObservation = observe(\.captureDeviceInput.device.isAdjustingFocus, options: .new) { _, change in
        guard let isAdjustingFocus = change.newValue else { return }
    
        print("isAdjustingFocus = \(isAdjustingFocus)")
    
    }
    

    【讨论】:

      【解决方案2】:

      斯威夫特 3

      AVCaptureDevice 实例上设置焦点模式:

      do {
           try videoCaptureDevice.lockForConfiguration()
           videoCaptureDevice.focusMode = .continuousAutoFocus
           videoCaptureDevice.unlockForConfiguration()
      } catch {}
      

      添加观察者:

      videoCaptureDevice.addObserver(self, forKeyPath: "adjustingFocus", options: [.new], context: nil)
      

      覆盖observeValue

      override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
      
          guard let key = keyPath, let changes = change else { 
              return
          }
      
          if key == "adjustingFocus" {
      
              let newValue = changes[.newKey]
              print("adjustingFocus \(newValue)")
          }
      }
      

      【讨论】:

        【解决方案3】:

        我为我的案例找到了自动对焦开始/结束时间的解决方案。它只是处理 KVO(Key-Value Observing)。

        在我的 UIViewController 中:

        // callback
        - (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] ];
                NSLog(@"Is adjusting focus? %@", adjustingFocus ? @"YES" : @"NO" );
                NSLog(@"Change dictionary: %@", change);
            }
        }
        
        // register observer
        - (void)viewWillAppear:(BOOL)animated{
            AVCaptureDevice *camDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
            int flags = NSKeyValueObservingOptionNew; 
            [camDevice addObserver:self forKeyPath:@"adjustingFocus" options:flags context:nil];
        
            (...)   
        }
        
        // unregister observer
        - (void)viewWillDisappear:(BOOL)animated{
            AVCaptureDevice *camDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
            [camDevice removeObserver:self forKeyPath:@"adjustingFocus"];
        
            (...)
        }
        

        文档:

        【讨论】:

        • 这并不能告诉你自动对焦是否失败。即使adjustingFocus变为false,也不一定意味着相机对焦。
        • 此方法在 iPhone 6/6Plus/6S/6S Plus 级别的设备上也失败,因为在不同的自动对焦模式下调整对焦不准确。
        • ISO 的密钥值是多少?
        • 感谢 Sean Langley 提供这些有用的信息。 Apple 文档:“注意:在使用对比度检测自动对焦的旧设备上,自动对焦期间 AVCaptureDeviceadjustingFocus 的值会发生变化。在具有焦点像素的设备上,焦点变化更小且更频繁,因此该属性的值不会改变在对焦期间。相反,观察 lensPosition 属性以查看镜头移动。"
        猜你喜欢
        • 2011-12-25
        • 1970-01-01
        • 1970-01-01
        • 2012-07-17
        • 1970-01-01
        • 2013-08-27
        • 2015-02-17
        • 2017-08-17
        • 2012-10-06
        相关资源
        最近更新 更多