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

时间:2015-09-29 15:46:55

标签: ios swift camera avfoundation 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()
        }
    }
}

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

enter image description here

2 个答案:

答案 0 :(得分:8)

 public func captureDevicePointOfInterestForPoint(pointInLayer: CGPoint) -> CGPoint

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

答案 1 :(得分: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)

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
相关问题