2015-09-29 19 views
7

Qual è il modo migliore per creare un accurato messa a fuoco automatica e l'esposizione per AVFoundation fotocamera strato personalizzato ?, per esempio, Attualmente il mio strato di anteprima della fotocamera è quadrato, ho vorrebbe che la messa a fuoco e l'esposizione della fotocamera siano specificate a quel fotogramma associato. Ho bisogno di questo in Swift 2 se possibile, altrimenti scrivere la tua risposta sarei in grado di convertirlo da solo.messa a fuoco automatica e l'esposizione automatica a AVFoundation su Custom livello della fotocamera

Messa a fuoco ed esposizione automatiche correnti: Tuttavia, come si può vedere, verrà valutata l'intera vista durante la messa a fuoco.

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

livello della fotocamera: nulla nel rapporto 1: 1 dovrebbe essere considerato come punto di messa a fuoco e l'esposizione, e nulla al di fuori questo limite non sarebbe nemmeno essere considerato come un evento di tocco per la messa a fuoco.

enter image description here

+2

Si sta utilizzando AVCaptureVideoPreviewLayer? In tal caso: 'public func captureDevicePointOfInterestForPoint (pointInLayer: CGPoint) -> CGPoint' – jlw

+0

@jlw Sì, sto usando' AVCaptureVideoPreviewLayer' ma non vedo quella funzione. :/ – Xrait

+1

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

risposta

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

vi darà il punto per il dispositivo di concentrarsi su in base alle impostazioni del tuo AVCaptureVideoPreviewLayer. Vedi lo docs.

+0

Grazie ancora. – Xrait

7

Grazie a JLW ecco come lo si fa in Swift 2. Per prima cosa, è necessario impostare Tap gesture è possibile farlo a livello di programmazione o 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) 

creare una base funzione sul nostro selettore nella 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() 
     } 
    } 
} 

Inoltre, se si desidera utilizzare l'indicatore di animazione, si prega di utilizzare TouchPoint al vostro tocco di un evento e assegnarlo al tuo livello animato.

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