iOS相机:手动曝光时间但自动ISO? [英] iOS camera: manual exposure duration but auto ISO?

查看:18
本文介绍了iOS相机:手动曝光时间但自动ISO?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用相机视频源进行一些图像处理,并希望优化以获得最快的快门速度.我知道你可以手动设置曝光时间和 ISO 使用

I'm using the camera video feed for some image processing and would like to optimise for fastest shutter speed. I know you can manually set exposure duration and ISO using

setExposureModeCustomWithDuration:ISO:completionHandler:

但这需要手动设置这两个值.是否有方法或巧妙的技巧可以让您手动设置曝光持续时间但让 ISO 句柄本身尝试正确曝光图像?

but this requires one to set both the values by hand. Is there a method or clever trick to allow you to set the exposure duraction manually but have the ISO handle itself to try to correctly expose the image?

推荐答案

我不确定这个解决方案是否是最好的,因为我和你一样在这个问题上苦苦挣扎.我所做的是聆听曝光偏移的变化,并从中调整 ISO,直到达到可接受的曝光水平.此代码大部分取自 Apple 示例代码

I'm not sure if this solution is the best one, since I was struggling with this as you were. What I've done is to listen to changes in the exposure offset and, from them, adjust the ISO until you reach an acceptable exposure level. Most of this code has been taken from the Apple sample code

所以,首先,您要监听 ExposureTargetOffset 的变化.添加你的类声明:

So, First of all, you listen for changes on the ExposureTargetOffset. Add in your class declaration:

static void *ExposureTargetOffsetContext = &ExposureTargetOffsetContext;

然后,一旦您正确完成了设备设置:

Then, once you have done your device setup properly:

[self addObserver:self forKeyPath:@"captureDevice.exposureTargetOffset" options:NSKeyValueObservingOptionNew context:ExposureTargetOffsetContext];

(而不是 captureDevice,使用您的设备属性)然后在你的类中实现 KVO 的回调:

(Instead of captureDevice, use your property for the device) Then implement in your class the callback for KVO:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{

if (context == ExposureTargetOffsetContext){
        float newExposureTargetOffset = [change[NSKeyValueChangeNewKey] floatValue];
        NSLog(@"Offset is : %f",newExposureTargetOffset);

        if(!self.device) return;

        CGFloat currentISO = self.device.ISO;
        CGFloat biasISO = 0;

        //Assume 0,3 as our limit to correct the ISO
        if(newExposureTargetOffset > 0.3f) //decrease ISO
            biasISO = -50;
        else if(newExposureTargetOffset < -0.3f) //increase ISO
            biasISO = 50;

        if(biasISO){
            //Normalize ISO level for the current device
            CGFloat newISO = currentISO+biasISO;
            newISO = newISO > self.device.activeFormat.maxISO? self.device.activeFormat.maxISO : newISO;
            newISO = newISO < self.device.activeFormat.minISO? self.device.activeFormat.minISO : newISO;

            NSError *error = nil;
            if ([self.device lockForConfiguration:&error]) {
                [self.device setExposureModeCustomWithDuration:AVCaptureExposureDurationCurrent ISO:newISO completionHandler:^(CMTime syncTime) {}];
                [self.device unlockForConfiguration];
            }
        }
    }
}

使用此代码,快门速度将保持不变,并且将调整 ISO 以使图像不会过曝或过曝.

With this code, the Shutter speed will remain constant and the ISO will be adjusted to leave the image not too under or overexposed.

不要忘记在需要时移除观察者.希望这适合你.

Don't forget to remove the observer whenever is needed. Hope this suits you.

干杯!

这篇关于iOS相机:手动曝光时间但自动ISO?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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