捕获完成后,从AVCapturePhotoOutput获取使用的曝光时间和ISO值 [英] Get used exposure duration and ISO values after the capture is complete from the AVCapturePhotoOutput

查看:63
本文介绍了捕获完成后,从AVCapturePhotoOutput获取使用的曝光时间和ISO值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

我正在将AVCaptureSession与AVCapturePhotoOutput一起使用,以将捕获的图像另存为JPEG图像.

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

    }

}

将AVCaptureDevice设置为自动 连续调整曝光设置

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...
    }

}

捕获开始于

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)

}

将父类设置为AVCapturePhotoCaptureDelegate,并由其处理photoOutput

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...

问题

我需要知道每次拍摄所用的曝光时间和ISO值.这些值会有所不同,因为相机已设置为自动调整曝光,并且必须像这样.

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.

曝光时间和ISO值是微调曝光以获得最佳结果所必需的.进行微调后,将使用这些手动曝光值开始捕获

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)

推荐答案

我不是从捕获元数据中获取已使用的ISO和曝光时间,而是在捕获照片之前读取了这些值.以这种方式进行操作时,重要的是要检查曝光是否已完成调整.

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.

就在调用捕获之前:

检查自动曝光没有调整

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

读取当前的曝光参数

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

然后拍照

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

这篇关于捕获完成后,从AVCapturePhotoOutput获取使用的曝光时间和ISO值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆