AVFoundation - 如何控制曝光 [英] AVFoundation - How to control exposure

查看:109
本文介绍了AVFoundation - 如何控制曝光的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AFTER 点按即可拍照,我想在曝光不再调整后立即锁定曝光并关闭手电筒。所以,我添加了一个观察者来处理adjustExposure:

AFTER tapping to take picture, I want to lock exposure and turn off torch as soon as exposure is no longer adjusting. So, I added an observer to handle adjustingExposure:

- (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];        
}

这是observeValueForKeyPath方法:

Here is the observeValueForKeyPath method:

- (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];
    }
}

@ user3115647发布此信息,这正是我的尝试

@user3115647 posted this information, which is exactly what I am trying to do.

但我的照片是之前关闭火炬。

But my picture is taken BEFORE the torch is turned off.

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

Here is my captureStillImageAsynchronouslyFromConnection:self.captureConnection completionHandler. This block occurs after the image is taken. The observeValueForKeyPath is supposed to occur while the camera is adjusting exposure BEFORE the image is taken. But my torch is not going low BEFORE the image is being taken. Either this is a timing issue or I'm not setting up the camera configuration correctly.

- (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");


推荐答案

这几乎肯定是一个时间问题。在 if 块内调用 captureStillImageAsynchronouslyFromConnection:completionHandler:。然后在锁定曝光后将始终执行捕获。

It is almost certainly a timing issue. Call captureStillImageAsynchronouslyFromConnection:completionHandler: inside your if block. Then the capture will always be executed after exposure has been locked.

if ([self.inputDevice isExposureModeSupported:AVCaptureExposureModeLocked]) {
    dispatch_async(dispatch_get_main_queue(), 
        ^{
            NSError *error = nil;
            if ([self.inputDevice lockForConfiguration:&error]) {
                //code to lock exposure here
                //take photo here
            }
        });
}

这篇关于AVFoundation - 如何控制曝光的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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