Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos AVCaptureDevice Adjusting Exposure为假,但捕获的图像为暗_Macos_Avcapturesession_Avcapturedevice_Adjustment_Facetime - Fatal编程技术网

Macos AVCaptureDevice Adjusting Exposure为假,但捕获的图像为暗

Macos AVCaptureDevice Adjusting Exposure为假,但捕获的图像为暗,macos,avcapturesession,avcapturedevice,adjustment,facetime,Macos,Avcapturesession,Avcapturedevice,Adjustment,Facetime,我正在编写的Mac OS X应用程序正在使用macbook内置facetime摄像头拍摄照片 在MacBookAir3,2、MacBookPro8,2和MacBookPro10,2上,它工作正常,但在新的MacBook上,它会拍摄“黑色”照片。我知道这是因为自动曝光,但我很难让它工作。avcapture设备调整曝光设置为否,但拍摄的照片仍然完全黑暗 代码:setupCamera在应用程序启动期间调用一次 -(void) setupCamera { session = [[AVCaptur

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

在MacBookAir3,2、MacBookPro8,2和MacBookPro10,2上,它工作正常,但在新的MacBook上,它会拍摄“黑色”照片。我知道这是因为自动曝光,但我很难让它工作。
avcapture设备
调整曝光
设置为
,但拍摄的照片仍然完全黑暗

代码:
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 {
        // ...
    }
}
。。。然后,每次单击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 ...
    }];
}
其目的是检查相机设备是否正在调整焦距、曝光或白平衡。如果没有,请立即调用实际捕获。如果正在调整-添加观察者并从
observeValueForKeyPath
调用
actuallyCapture

问题是,
addObserver
调用从未被调用,因为设备返回所有
adjustingX==NO
——但捕获的照片仍然是黑色的

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


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

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

设置
调整曝光
调整焦距
调整白平衡
的观察者:

[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];
要捕获照片,请初始化一个
AVCaptureSession
,但设置一个1s延迟计时器,并在触发后实际捕获:

-(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];
            });
        }
    });
}
在观察值ForkeyPath: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];
        }
    }
}

什么是我的调整白平衡观察上下文?