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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With