自定义相机层上 AVFoundation 中的自动对焦和自动曝光

Auto Focus and Auto Exposure in AVFoundation on Custom Camera Layer

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()
        }
    }
}

Camera Layer: 比率在 1:1 内的任何东西都应被视为焦点和曝光点,超出此范围的任何东西甚至都不会被视为触摸事件用于相机对焦。

 public func captureDevicePointOfInterestForPoint(pointInLayer: CGPoint) -> CGPoint

将根据您的 AVCaptureVideoPreviewLayer 设置为您提供设备关注的点。见 docs.

感谢 JLW,这是您在 Swift 中的操作方法 2. 首先,我们需要设置点击手势,您可以通过编程方式或 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