【问题标题】:Detect when camera is auto-focusing检测相机何时自动对焦
【发布时间】:2014-07-13 04:30:21
【问题描述】:

我正在开发视频应用程序。我想在相机自动对焦时丢弃视频帧。在自动对焦期间,捕获的图像变得模糊,并且该帧的图像处理变得很差,但是一旦完成自动对焦,图像处理就会变得非常好。任何机构给我解决方案?

【问题讨论】:

标签: ios ios7 video-capture autofocus


【解决方案1】:

adjustingFocus 属性。
指示设备当前是否正在调整其焦点设置。 (只读)

*注意:您可以使用 Key-value 观察来观察此属性值的变化。

iOS 4.0 及更高版本


https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVCaptureDevice_Class/Reference/Reference.html#//apple_ref/occ/instp/AVCaptureDevice/adjustingFocus

【讨论】:

    【解决方案2】:

    以下是 Swift 3.x 中的示例代码。

    首先应在相机初始化时将观察者添加到选定的捕获设备。

    captureDevice.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 changedValue = changes[.newKey]
            if (changedValue! as! Bool){ 
                // camera is auto-focussing
            }else{         
                // camera is not auto-focussing
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      Swift 4+ 示例

      class ViewController: UIViewController, AVCapturePhotoCaptureDelegate {
          //@objc var captureDevice: AVCaptureDevice?
      
              override func viewWillAppear(_ animated: Bool) {
                  super.viewWillAppear(animated)
                  self.addObservers()
              }
      
             func addObservers() {
                   self.addObserver(self, forKeyPath: "captureDevice.adjustingFocus", options: .new, context: nil)
      
             }
      
             func removeObservers() {
                self.removeObserver(self, forKeyPath: "captureDevice.adjustingFocus")
             }
      
      
            override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
      
               if keyPath == "captureDevice.adjustingFocus" {
                   print("============== adjustingFocus: \(self.captureDevice?.lensPosition)") 
              }
      
          } //End of class
      

      【讨论】:

        【解决方案4】:

        观察adjustingFocus 对我不起作用。总是没有。我发现了这一点。

        请注意,当使用传统的对比度检测自动对焦时,AVCaptureDeviceadjustingFocus 属性会在对焦进行时翻转为 YES,并在完成后翻转回 NO。使用相位检测自动对焦时,adjustingFocus 属性不会翻转为 YES,因为相位检测方法倾向于更频繁地对焦,但对焦量很小,有时甚至难以察觉。您可以观察 AVCaptureDevice lensPosition 属性来查看由相位检测 AF 驱动的镜头移动。

        来自Apple

        我还没有尝试,稍后我会尝试更新。

        编辑。我试过了,确认是对的。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-08-27
          • 1970-01-01
          • 2019-07-13
          • 2014-09-10
          • 2012-10-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多