背景
我正在使用AVCaptureSession和AVCapturePhotoOutput将捕获保存为JPEG图像。
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设置为自动并连续调整曝光设置
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由它处理
//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 ...
}
}
然而一切正常,但是......
问题
我需要知道每次捕获时使用的曝光持续时间和ISO值。由于相机设置为自动调整曝光值,因此值会有所不同。
我知道捕获的元数据包含这些值,但我无法弄清楚如何访问它们。
曝光持续时间和ISO值是微调曝光以获得最佳效果所必需的。微调后,使用这些手动曝光值
开始捕获captureDevice?.setExposureModeCustom(duration: customTime, iso: customISO, completionHandler: nil)
答案 0 :(得分:0)
我没有从捕获元数据中获取已使用的ISO和曝光持续时间,而是在捕获照片之前读取这些值。这样做时,检查曝光是否已完成调整非常重要。
在调用捕获之前:
检查自动曝光是否未调整
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)