Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto Focus and Auto Exposure in AVFoundation on Custom Camera Layer

What is the best way to create an accurate Auto Focus and Exposure for AVFoundation custom layer camera?, for example, currently my camera preview layer is square, I would like the camera focus and exposure to be specify to that frame bound. I need this in Swift 2 if possible, if not please write your answer I would be able to convert it myself.

Current Auto Focus and Exposure: But as you can see this will evaluate the entire view when focusing.

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

Camera Layer: Anything in the 1:1 ratio should be considered as focus and exposure point, and anything outside this bound would not even be considered as a touch event for camera focus.

enter image description here

like image 345
Brian Nezhad Avatar asked Sep 29 '15 15:09

Brian Nezhad


Video Answer


2 Answers

 public func captureDevicePointOfInterestForPoint(pointInLayer: CGPoint) -> CGPoint

will give you the point for the device to focus on based on the settings of your AVCaptureVideoPreviewLayer. See the docs.

like image 134
jlw Avatar answered Oct 19 '22 12:10

jlw


Thanks to JLW here is how you do it in Swift 2. First, we need to setup Tap gesture you can do this programmatically or 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)

Create a function base on our selector in 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()
        }
    }
}

Also, if you like to use your animation indicator, please use touchPoint at your touch of an event and assign it to your animated layer.

//Assign Indicator Position
touchIndicatorOutside.frame.origin.x = touchPoint.x - 10
touchIndicatorOutside.frame.origin.y = touchPoint.y - 10
like image 44
Brian Nezhad Avatar answered Oct 19 '22 12:10

Brian Nezhad