AVCaptureDevice adjustExposure为False但拍摄的图像很暗

grz*_*aks 8 macos adjustment facetime avcapturesession avcapturedevice

我编写的Mac OS X应用程序正在使用macbook内置的facetime相机拍摄照片.

在MacBookAir3,2,MacBookPro8,2和MacBookPro10,2上工作正常,但在新的MacBook上它需要"黑暗"的照片.我理解这是因为自动曝光,但我很难让它工作.该AVCaptureDevice adjustingExposure设置为NO,但所拍摄的照片仍然是完全黑暗的.

代码:setupCamera在应用启动期间调用一次

-(void) setupCamera
{
    session = [[AVCaptureSession alloc] init];
    session.sessionPreset = AVCaptureSessionPresetPhoto;

    sessionInitialized = YES;

    device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    [device lockForConfiguration:NULL];
    if ([device isExposureModeSupported:AVCaptureExposureModeContinuousAutoExposure])
        [device setExposureMode:AVCaptureExposureModeContinuousAutoExposure];

    if ([device isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus])
        [device setFocusMode:AVCaptureFocusModeContinuousAutoFocus];

    if ([device isWhiteBalanceModeSupported:AVCaptureWhiteBalanceModeContinuousAutoWhiteBalance])
        [device setWhiteBalanceMode:AVCaptureWhiteBalanceModeContinuousAutoWhiteBalance];

    [device unlockForConfiguration];


    NSError *error = nil;
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
    if(error != nil) {
        // ...
    }

    if([session canAddInput:input]) {
        [session addInput:input];
    } else {
        // ...
    }

    output = [[AVCaptureStillImageOutput alloc] init];
    NSDictionary *outputSettings = @{ AVVideoCodecKey : AVVideoCodecJPEG };
    [output setOutputSettings:outputSettings];

    if([session canAddOutput:output]) {
        [session addOutput:output];
    } else {
        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

...然后每次点击UI中的按钮都会调用该shootPhoto函数:

-(void) shootPhoto
{
    [session startRunning];

    if([device lockForConfiguration:NULL]) {
        if ([device isExposureModeSupported:AVCaptureExposureModeContinuousAutoExposure])
            [device setExposureMode:AVCaptureExposureModeContinuousAutoExposure];

        if ([device isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus])
            [device setFocusMode:AVCaptureFocusModeContinuousAutoFocus];

        if ([device isWhiteBalanceModeSupported:AVCaptureWhiteBalanceModeContinuousAutoWhiteBalance])
            [device setWhiteBalanceMode:AVCaptureWhiteBalanceModeContinuousAutoWhiteBalance];

        [device unlockForConfiguration];
    }

    if(device.adjustingFocus == NO && device.adjustingExposure == NO && device.adjustingWhiteBalance == NO) {
        [self actuallyCapture];
    } else {
        [device addObserver:self forKeyPath:@"adjustingExposure" options:NSKeyValueObservingOptionNew context:MyAdjustingExposureObservationContext];
        [device addObserver:self forKeyPath:@"adjustingFocus" options:NSKeyValueObservingOptionNew context:MyAdjustingFocusObservationContext];
        [device addObserver:self forKeyPath:@"adjustingWhiteBalance" options:NSKeyValueObservingOptionNew context:MyAdjustingWhiteBalanceObservationContext];
    }
}

-(void) actuallyCapture
{
    if ([session isRunning] == NO)
        return;

    connection = [output connectionWithMediaType:AVMediaTypeVideo];
    [output captureStillImageAsynchronouslyFromConnection:connection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
        // save file etc ...
    }];
}
Run Code Online (Sandbox Code Playgroud)

我们的想法是检查相机设备是否正在调整焦距,曝光或白平衡.如果不立即打电话actuallyCapture.如果它正在调整 - 添加观察者并actuallyCapture从中调用observeValueForKeyPath.

问题是addObserver调用永远不会被调用,因为设备返回所有adjustingX==NO- 但仍然,捕获的照片是黑暗的.

可能是什么原因?我在等待白平衡和曝光调整吗?

对我来说很难调试,因为我只拥有那些工作正常的设备.

grz*_*aks 2

我自己设法解决了这个问题。我是这样做的:

设置adjustingExposureadjustingFocus和 的观察者adjustingWhiteBalance

[self.device addObserver:self forKeyPath:@"adjustingExposure" options:NSKeyValueObservingOptionNew context:MyAdjustingExposureObservationContext];
[self.device addObserver:self forKeyPath:@"adjustingFocus" options:NSKeyValueObservingOptionNew context:MyAdjustingFocusObservationContext];
[self.device addObserver:self forKeyPath:@"adjustingWhiteBalance" options:NSKeyValueObservingOptionNew context:MyAdjustingWhiteBalanceObservationContext];
Run Code Online (Sandbox Code Playgroud)

要捕获照片,请初始化 aAVCaptureSession但设置 1 秒延迟计时器,并在触发后实际捕获:

-(void) shootPhoto
{
    dispatch_async(self.sessionQueue, ^{
        if([self setupCamera]) {
            self.sessionInitialized = YES;
            [self.session startRunning];

            self.isWaitingToCaptureImage = YES;
            dispatch_async(dispatch_get_main_queue(), ^{
                self.captureDelayTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                                          target:self
                                                                        selector:@selector(actuallyCapture)
                                                                        userInfo:nil
                                                                         repeats:NO];
            });
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

检查observeValueForKeyPath:ofObject:change:context所有三个调整是否已完成,如果完成,则取消上面设置的计时器并拍摄照片:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if(!self.sessionInitialized || !self.isWaitingToCaptureImage) {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
        return;
    }

    if (context != MyAdjustingExposureObservationContext && context != MyAdjustingFocusObservationContext && context != MyAdjustingWhiteBalanceObservationContext) {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
        return;
    } else {
        if (self.device.adjustingExposure || self.device.adjustingFocus || self.device.adjustingWhiteBalance) {
            NSLog(@"not ready to capture yet");
            return;
        } else {
            NSLog(@"ready to capture");
            if (self.captureDelayTimer && self.captureDelayTimer.isValid) {
                [self.captureDelayTimer invalidate];
                self.captureDelayTimer = nil;
            }
            [self actuallyCaptureDispatch];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)