AVFoundation - 如何控制曝光

Pat*_*cia 7 iphone objective-c avfoundation

攻进行拍照,我要锁定曝光,一旦曝光,不再调整关闭的火炬.所以,我添加了一个观察者来处理adjustExposure:

- (IBAction)configureImageCapture:(id)sender
{
    [self.session beginConfiguration];

    [self.cameraController device:self.inputDevice exposureMode:AVCaptureExposureModeAutoExpose];
    [self.cameraController device:self.inputDevice torchMode:AVCaptureTorchModeOn torchLevel:0.8f];

    [self.session commitConfiguration];

    [(AVCaptureDevice *)self.inputDevice addObserver:self forKeyPath:@"adjustingExposure" options:NSKeyValueObservingOptionNew context:MyAdjustingExposureObservationContext];        
}
Run Code Online (Sandbox Code Playgroud)

这是observeValueForKeyPath方法:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (context == MyAdjustingExposureObservationContext) {
        if( [keyPath isEqualToString:@"adjustingExposure"] )
        {
            BOOL adjustingExposure = [ [change objectForKey:NSKeyValueChangeNewKey] isEqualToNumber:[NSNumber numberWithInt:1] ];

            if (!adjustingExposure)
            {
                [(AVCaptureDevice *)self.cameraController.inputDevice removeObserver:self forKeyPath:@"adjustingExposure"];

                if ([self.inputDevice isExposureModeSupported:AVCaptureExposureModeLocked]) {
                    dispatch_async(dispatch_get_main_queue(),
                                   ^{
                                       NSError *error = nil;
                                       if ([self.inputDevice lockForConfiguration:&error]) {
                                           // 5) lock the exposure
                                           [self.cameraController device:self.inputDevice exposureMode:AVCaptureExposureModeLocked];

                                           // 6) turn off the Torch
                                           [self.cameraController device:self.inputDevice torchMode:AVCaptureTorchModeOn torchLevel:0.0001f];

                                           [self.inputDevice unlockForConfiguration];
                                       }
                                   });
                }                    
            }
        }
    } else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}
Run Code Online (Sandbox Code Playgroud)

@ user3115647发布了这个信息,这正是我想要做的.

但我的照片是火炬关闭之前拍摄的.

这是我的captureStillImageAsynchronouslyFromConnection:self.captureConnection completionHandler.拍摄图像后会出现此块.当摄像机在拍摄图像之前调整曝光时,应该发生observeValueForKeyPath.但是在拍摄照片之前我的火炬并没有变低.这是一个计时问题,或者我没有正确设置摄像头配置.

- (void)captureImage
{
    // configureImageCapture has already been done
    [self.stillImageOutput captureStillImageAsynchronouslyFromConnection:self.captureConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
     {
         if (imageSampleBuffer != NULL)
         {
             // Log the image properties
             CFDictionaryRef attachmentsRef = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
             NSDictionary *properties = (__bridge NSDictionary *)(attachmentsRef);
             NSLog(@"Image Properties => %@", (properties.count) ? properties : @"none");
Run Code Online (Sandbox Code Playgroud)

Eth*_*anP 1

我使用闪光灯而不是手电筒也发生了类似的情况。@"videoDevice.flashActive"我也有一个观察员。我确实尝试exposureModeLocked先使用,但它对我来说也不起作用。

  1. 打开闪光灯拍照
  2. 然后立即关闭闪光灯并在曝光调整之前拍摄另一张照片

下面的代码可能不仅仅可以单独运行,而且是我所做的简化后的代码。

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void*)context
{
    if (context == AdjustingExposureContext)
    {
        self.isAdjustingExposure = [change[NSKeyValueChangeNewKey] boolValue];
    }
    else if (context == FlashModeChangedContext)
    {
        self.isFlashActive = [change[NSKeyValueChangeNewKey] boolValue];
        if (!self.flashActive)
        {
            [self captureImage];  // QUICKLY! capture 2nd image without
        }                         // flash before exposure adjusts
    }
    if (!self.isAdjustingExposure && self.flashActive)
    {
        [self removeObserver:self forKeyPath:@"videoDevice.adjustingExposure" context:AdjustingExposureContext];
        [self captureImage];  // capture 1st image with the flash on
    }
}
Run Code Online (Sandbox Code Playgroud)

现在在 的回调中captureStillImageAsynchronouslyFromConnection:

if (self.isFlashActive)
    [self.videoDeviceInput.device setFlashMode:NO];
Run Code Online (Sandbox Code Playgroud)

但是,如果您需要在不使用闪光灯的情况下以较低的曝光度拍摄多张照片,则此策略可能不起作用。