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

Far*_*had 7 camera avfoundation ios swift swift2

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()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

相机层: 1:1比例的任何内容都应被视为焦点和曝光点,此范围之外的任何内容甚至不会被视为相机焦点的触摸事件.

在此输入图像描述

jlw*_*jlw 8

 public func captureDevicePointOfInterestForPoint(pointInLayer: CGPoint) -> CGPoint
Run Code Online (Sandbox Code Playgroud)

将根据AVCaptureVideoPreviewLayer的设置为您提供设备关注点.查看文档.


Far*_*had 7

感谢JLW,你在Swift 2中是如何做到的.首先,我们需要设置Tap手势,你可以通过编程或故事板来完成.

    //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)
Run Code Online (Sandbox Code Playgroud)

在我们的选择器中创建一个函数库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()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

此外,如果您想使用动画指示器,请在触摸事件时使用touchPoint并将其指定给动画图层.

//Assign Indicator Position
touchIndicatorOutside.frame.origin.x = touchPoint.x - 10
touchIndicatorOutside.frame.origin.y = touchPoint.y - 10
Run Code Online (Sandbox Code Playgroud)