2015-09-29 160 views
7

什麼是創建一個精確的自動對焦和AVFoundation定製層相機曝光的最好辦法?比如,目前我的攝像頭預覽層是方形的,我想要將相機對焦和曝光指定爲該邊框。如果可能的話,我需要這個在Swift 2中,如果不是,請寫下你的答案,我可以自己將其轉換。自動對焦和自定義相機層AVFoundation自動曝光

當前自動對焦和曝光:但正如您所看到的,這將在對焦時評估整個視圖。

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

您使用的是AVCaptureVideoPreviewLayer嗎?如果是這樣的話:'公共func captureDevicePointOfInterestForPoint(pointInLayer:CGPoint) - > CGPoint' – jlw

+0

@jlw是的我使用'AVCaptureVideoPreviewLayer',但我沒有看到這個功能。 :/ – Xrait

+1

https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVCaptureVideoPreviewLayer_Class/#//apple_ref/occ/instm/AVCaptureVideoPreviewLayer/captureDevicePointOfInterestForPoint: – jlw

回答

8
public func captureDevicePointOfInterestForPoint(pointInLayer: CGPoint) -> CGPoint 

會給你點的設備根據你AVCaptureVideoPreviewLayer的設置集中。請參閱docs

+0

再次感謝您。 – Xrait

7

感謝JLW這裏是你如何在Swift 2中做到這一點。首先,我們需要設置點擊手勢,你可以通過編程或故事板來完成。

//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