iOS : 如何在 AVCaptureSession 上激活图像稳定?

iOS : How to activate image stabilization on an AVCaptureSession?

我想在我的应用程序中捕捉稳定的图像,但我还没有找到实现它所需的配置。

这是我的代码:

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

所以会话预设设置正确但不支持图像稳定。

如何支持图像稳定?


** 有节奏的拳头反应后的第二次尝试: **

我切换到后置摄像头,我在设置之前将输出添加到 captureSession,但我的图像仍然没有稳定:

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

结果是:
不支持稳定性
稳定性未激活

首先,您忘记将 AVCaptureStillImageOutput 添加到 AVCaptureSession。您必须在查询其功能之前执行此操作!

captureSession.addOutput(stillImageOutput)

其次,neither Digital nor Optical Image Stabilisation are supported on the front camera

第三,在后置摄像头上,在支持的平台上(数字似乎在 5S 上可用)AVCaptureStillImageOutput automaticallyEnablesStillImageStabilizationWhenAvailable 默认为 YES,所以如果你切换到后置摄像头 - 那么你已经使用某种形式的图像稳定。

注意:光学图像稳定 is only available on the 6+ and 6S+(尽管链接的技术说明尚未针对 6S 型号更新)。