我正在编写的 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 {
// ...
}
}
...然后每次单击 UI 中的 snap 按钮都会调用该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 ...
}];
}
这个想法是检查相机设备是否正在调整焦点、曝光或白平衡。如果不actuallyCapture
马上打电话。如果它正在调整 - 添加观察者并actuallyCapture
从observeValueForKeyPath
.
问题是addObserver
永远不会调用这些调用,因为设备会全部返回adjustingX==NO
- 但是,捕获的照片仍然很暗。
可能是什么原因?我是否在正确等待白平衡和曝光调整?
对我来说很难调试,因为我只拥有那些运行良好的设备。