Detect camera condition in AVCaptureSession swift

539 Views Asked by At

I am working on a swift application and I want to take a picture during the video when the camera is not moving or when user focuses on something. i used AVCaptureVideoDataOutputSampleBufferDelegate *captureOutput method which giving me image every time after starting camera. but I want to take only when the camera is not moving or focused.

func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {        
        print("didOutput")
        guard let hasImage = CMSampleBufferGetImageBuffer(sampleBuffer) else {
            print("no image")
            return
        }
        let ciimage : CIImage = CIImage(cvPixelBuffer: hasImage)
        DispatchQueue.main.async {
            self.liveCamImage = self.convert(cmage: ciimage)
        }
    }

is there any solution for this

1

There are 1 best solutions below

0
Asperi On

You can try to use adjusting focus property of your capture device (AVCaptureDevice), when it is false the focus is stable. See detailed documentation below.

/**
 @property adjustingFocus
 @abstract
    Indicates whether the receiver is currently performing a focus scan to adjust focus.

 @discussion
    The value of this property is a BOOL indicating whether the receiver's camera focus is being automatically adjusted by means of a focus scan, because its focus mode is AVCaptureFocusModeAutoFocus or AVCaptureFocusModeContinuousAutoFocus. Clients can observe the value of this property to determine whether the camera's focus is stable.
 @seealso lensPosition
 @seealso AVCaptureAutoFocusSystem
 */
open var isAdjustingFocus: Bool { get }