Search code examples
iosswiftavcapturesessionios-camera

iOS : How to activate image stabilization on an AVCaptureSession?


I'd like to capture stabilized images in my app but I haven't found the required configuration to acheive it.

This is my code :

let frontCamera = cameraWithPosition(AVCaptureDevicePosition.Front)

let captureSession = AVCaptureSession()

if captureSession.canSetSessionPreset(AVCaptureSessionPresetPhoto) {
    captureSession.sessionPreset =  AVCaptureSessionPresetPhoto 
    print("Session preset has been set ")
}
else {
    print("Session preset couldn't be set ")
}

var error: NSError?
var input: AVCaptureDeviceInput!
do {
    input = try AVCaptureDeviceInput(device: frontCamera)
} catch let error1 as NSError {
    error = error1
    input = nil
}

if error == nil && captureSession!.canAddInput(input) {
    captureSession.addInput(input)
    let stillImageOutput = AVCaptureStillImageOutput()
    stillImageOutput.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
    if stillImageOutput.stillImageStabilizationSupported {
        stillImageOutput.automaticallyEnablesStillImageStabilizationWhenAvailable = true
        print("Stabilization supported ")
    }
    else {
        print("Stabilization is not supported ")
    }
}

So the session preset are correctly set but the image stabilization is not supported.

What can I do to support image stabilisation ?


** 2nd ATTEMPT AFTER RHYTHMIC FISTMAN RESPONSE : **

I switched to the back camera, I've added the output to the captureSession before setting it, and I still don't have my image stabilized :

    let backCamera = cameraWithPosition(AVCaptureDevicePosition.Back)

    let captureSession = AVCaptureSession()

    if captureSession.canSetSessionPreset(AVCaptureSessionPresetPhoto) {
        captureSession.sessionPreset =  AVCaptureSessionPresetPhoto 
        print("Session preset has been set ")
    }
    else {
        print("Session preset couldn't be set ")
    }


    var error: NSError?
    var input: AVCaptureDeviceInput!
    do {
        input = try AVCaptureDeviceInput(device: backCamera)
    } catch let error1 as NSError {
        error = error1
        input = nil
    }

    if error == nil && captureSession.canAddInput(input) {
        captureSession.addInput(input)
        let stillImageOutput = AVCaptureStillImageOutput()
        stillImageOutput.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]


        if captureSession.canAddOutput(stillImageOutput) {
            captureSession.addOutput(stillImageOutput)

            if stillImageOutput.stillImageStabilizationSupported == true {
                stillImageOutput.automaticallyEnablesStillImageStabilizationWhenAvailable = true
                print("Stabilization supported ")
            }
            else {
                print("Stabilization is not supported ")
            }

            if stillImageOutput.stillImageStabilizationActive == true {
                print("Stabilization is active ")
            }
            else {
                print("Stabilization is not active ")
            }
        }
    }

The result is :
Stabilization is not supported
Stabilization is not active


Solution

  • Firstly, you've forgotten to add your AVCaptureStillImageOutput to the AVCaptureSession. You must do that before querying its capabilities!

    captureSession.addOutput(stillImageOutput)
    

    Secondly, neither Digital nor Optical Image Stabilisation are supported on the front camera.

    Thirdly, on the back camera, on supported platforms (digital appears to be available on 5S up) AVCaptureStillImageOutput automaticallyEnablesStillImageStabilizationWhenAvailable defaults to YES, so if you switch to the back camera - then you already will be using some form of image stabilisation.

    NB: Optical Image Stabilisation is only available on the 6+ and 6S+ (although the linked technote has not been updated for the 6S models yet).