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

什么是为AVFoundation自定义层摄像头创build准确的自动对焦和曝光的最佳方法是什么?例如,目前我的相机预览图层是方形的,我希望相机的焦点和曝光被指定到该帧的边界。 如果可能的话,我需要这个在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的比例都应该被认为是焦点和曝光点,而这个界限之外的东西甚至不会被视为相机焦点的触摸事件。

在这里输入图像说明

  public func captureDevicePointOfInterestForPoint(pointInLayer: CGPoint) -> CGPoint 

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

感谢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的select器中创build一个函数库。

 /*========================================= * 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() } } } 

另外,如果您喜欢使用animation指示符,请在触摸事件时使用touchPoint,并将其指定给您的animation图层。

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