【问题标题】:Auto Focus and Auto Exposure in AVFoundation on Custom Camera Layer自定义相机层上的 AVFoundation 中的自动对焦和自动曝光
【发布时间】:2015-12-27 04:41:59
【问题描述】:

AVFoundation 自定义图层相机创建准确的自动对焦和曝光的最佳方法是什么?例如,目前我的相机预览图层是方形的,我会就像要指定到该帧绑定的相机焦点和曝光一样。如果可能的话,我在 Swift 2 中需要这个,如果没有,请写下你的答案,我可以自己转换它。

当前自动对焦和曝光:但正如您所见,这将在对焦时评估整个视图。

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    //Get Touch Point
    let Point = touches.first!.locationInView(self.capture)
    //Assign Auto Focus and Auto Exposour
    if let device = currentCameraInput {
        do {
            try! device.lockForConfiguration()
            if device.focusPointOfInterestSupported{
                //Add Focus on Point
                device.focusPointOfInterest = Point
                device.focusMode = AVCaptureFocusMode.AutoFocus
            }

            if device.exposurePointOfInterestSupported{
                //Add Exposure on Point
                device.exposurePointOfInterest = Point
                device.exposureMode = AVCaptureExposureMode.AutoExpose
            }
            device.unlockForConfiguration()
        }
    }
}

相机层:任何 1:1 比例的东西都应被视为焦点和曝光点,超出此范围的任何东西都不会被视为相机对焦的触摸事件。

【问题讨论】:

  • 你在使用 AVCaptureVideoPreviewLayer 吗?如果是这样:public func captureDevicePointOfInterestForPoint(pointInLayer: CGPoint) -&gt; CGPoint
  • @jlw 是的,我正在使用AVCaptureVideoPreviewLayer,但我没有看到该功能。 ://
  • @jlw 哇,我怎么会错过呢?请纠正你的答案,我会接受的。谢谢

标签: ios swift camera avfoundation swift2


【解决方案1】:
 public func captureDevicePointOfInterestForPoint(pointInLayer: CGPoint) -> CGPoint

将根据您的 AVCaptureVideoPreviewLayer 的设置为您提供设备关注点。请参阅docs

【讨论】:

    【解决方案2】:

    感谢 JLW,这是您在 Swift 2 中的操作方式。首先,我们需要设置 Tap 手势,您可以通过编程方式或 Storyboard 来执行此操作。

        //Add UITap Gesture Capture Frame for Focus and Exposure
        let captureTapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "AutoFocusGesture:")
        captureTapGesture.numberOfTapsRequired = 1
        captureTapGesture.numberOfTouchesRequired = 1
        self.captureFrame.addGestureRecognizer(captureTapGesture)
    

    根据captureTapGesture 中的选择器创建一个函数。

    /*=========================================
    * FOCUS & EXPOSOUR
    ==========================================*/
    var animateActivity: Bool!
    internal func AutoFocusGesture(RecognizeGesture: UITapGestureRecognizer){
        let touchPoint: CGPoint = RecognizeGesture.locationInView(self.captureFrame)
        //GET PREVIEW LAYER POINT
        let convertedPoint = self.previewLayer.captureDevicePointOfInterestForPoint(touchPoint)
    
        //Assign Auto Focus and Auto Exposour
        if let device = currentCameraInput {
            do {
                try! device.lockForConfiguration()
                if device.focusPointOfInterestSupported{
                    //Add Focus on Point
                    device.focusPointOfInterest = convertedPoint
                    device.focusMode = AVCaptureFocusMode.AutoFocus
                }
    
                if device.exposurePointOfInterestSupported{
                    //Add Exposure on Point
                    device.exposurePointOfInterest = convertedPoint
                    device.exposureMode = AVCaptureExposureMode.AutoExpose
                }
                device.unlockForConfiguration()
            }
        }
    }
    

    另外,如果您喜欢使用动画指示器,请在触摸事件时使用 touchPoint 并将其分配给动画层。

    //Assign Indicator Position
    touchIndicatorOutside.frame.origin.x = touchPoint.x - 10
    touchIndicatorOutside.frame.origin.y = touchPoint.y - 10
    

    【讨论】:

      猜你喜欢
      • 2018-09-07
      • 1970-01-01
      • 2012-05-06
      • 1970-01-01
      • 2015-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多