Get used exposure duration and ISO values after the capture is complete from the AVCapturePhotoOutput

↘锁芯ラ 提交于 2021-02-08 10:01:01

问题


Background

I am using AVCaptureSession with AVCapturePhotoOutput to save captures as JPEG images.

let captureSession = AVCaptureSession()
let stillImageOutput = AVCapturePhotoOutput()
var captureDevice : AVCaptureDevice?

...

func setupCamera() {

    captureDevice = AVCaptureDevice.default(AVCaptureDevice.DeviceType.builtInWideAngleCamera, for: AVMediaType.video, position: .back)

    if (captureDevice != nil)  {

        captureSession.addInput(try AVCaptureDeviceInput(device: captureDevice!))

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

    }

}

The AVCaptureDevice is set to automatically and continuously adjust exposure settings

func configureCamera() {

    do {

        try captureDevice?.lockForConfiguration()

        captureDevice?.exposureMode = AVCaptureDevice.ExposureMode.continuousAutoExposure

        captureDevice?.unlockForConfiguration()

    } catch let error as NSError {
        // Errors handled here...
    }

}

The capture is started by

func capture(){

    // Get an instance of AVCapturePhotoSettings class
    let photoSettings = AVCapturePhotoSettings()

    // Set photo settings
    photoSettings.isAutoStillImageStabilizationEnabled = true
    photoSettings.flashMode = .off

    // Call capturePhoto method by passing photo settings and a
    // delegate implementing AVCapturePhotoCaptureDelegate
    stillImageOutput.capturePhoto(with: photoSettings, delegate: self)

}

The parent class is set as an AVCapturePhotoCaptureDelegate and the photoOutput is handled by it

//Delegate
func photoOutput(_ captureOutput: AVCapturePhotoOutput,
                 didFinishProcessingPhoto photoSampleBuffer: CMSampleBuffer?,
                 previewPhoto previewPhotoSampleBuffer: CMSampleBuffer?,
                 resolvedSettings: AVCaptureResolvedPhotoSettings,
                 bracketSettings: AVCaptureBracketedStillImageSettings?,
                 error: Error?) {

    // Make sure there is a photo sample buffer
    guard error == nil,
        let photoSampleBuffer = photoSampleBuffer else {
            //Errors handled here
            return
    }

    // Convert photo same buffer to a jpeg image data by using // AVCapturePhotoOutput
    guard let imageData =
        AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: photoSampleBuffer, previewPhotoSampleBuffer: previewPhotoSampleBuffer) else {
            return
    }

    let capturedImage = UIImage.init(data: imageData , scale: 1.0)

    if let image = capturedImage {
        //save photo ...
    }

}

And everything works as it should, however...

Problem

I need to know the exposure duration and ISO values that were used for each capture. The values vary because the camera is set to automatically adjust exposure and it has to be like that.

I know the metadata of the capture holds these values but I can't figure out how to access them.

The exposure duration and ISO values are necessary for fine tuning the exposure to achieve optimal results. After fine tuning the capture is started with these manual exposure values

captureDevice?.setExposureModeCustom(duration: customTime, iso: customISO, completionHandler: nil)

回答1:


Instead of getting the used ISO and exposure duration from the capture metadata, I read these values just before capturing a photo. When doing it this way it is important to check that the exposure has finished adjusting.

Just before calling the capture:

check that the auto exposure is not adjusting

while ((captureDevice?.isAdjustingExposure)!){
    usleep(100000) // wait 100 msec
}

Read the current exposure parameters

let current_exposure_duration : CMTime = (captureDevice?.exposureDuration)!
let current_exposure_ISO : Float = (captureDevice?.iso)!

And then take a photo

stillImageOutput.capturePhoto(with: photoSettings, delegate: self)


来源:https://stackoverflow.com/questions/48702881/get-used-exposure-duration-and-iso-values-after-the-capture-is-complete-from-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!