AVCaptureDevice低光增强不起作用

时间:2014-03-17 05:54:32

标签: ios objective-c avfoundation

AVCaptureDevice的低光增强属性无法启用。我正在使用iOS 6的iPhone 5上测试这个。这是代码:

    // finds a device that supports the video media type
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    NSArray *allDevices = [AVCaptureDevice devices];
    for (AVCaptureDevice *currentDevice in allDevices) {
        if (currentDevice.position == AVCaptureDevicePositionBack) {
            device = currentDevice;
        }
    }

    NSError *deviceError = nil;
    if (device.isFlashAvailable){
        [device lockForConfiguration:&deviceError];
        device.flashMode = AVCaptureFlashModeAuto;
        [device unlockForConfiguration];
    }
    if ([device respondsToSelector:@selector(isLowLightBoostSupported)]) {
        if ([device lockForConfiguration:nil]) {
            if (device.isLowLightBoostSupported)
                device.automaticallyEnablesLowLightBoostWhenAvailable = YES;
                [device unlockForConfiguration];
        }
    }
    if ([device isExposureModeSupported:AVCaptureExposureModeContinuousAutoExposure]) {
        [device lockForConfiguration:&deviceError];
        device.exposureMode = AVCaptureExposureModeContinuousAutoExposure;
        // CODE FOR device.exposurePointOfInterest determined from wherever the face is based off of the faceScan method
        [device unlockForConfiguration];
    }

    AVCaptureDeviceInput *newVideoInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:&deviceError];

    AVCaptureStillImageOutput *newStillImageOutput = [[AVCaptureStillImageOutput alloc] init];

    NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:
                                AVVideoCodecJPEG, AVVideoCodecKey,
                                nil];


    [newStillImageOutput setOutputSettings:outputSettings];


    self.sess = [[AVCaptureSession alloc] init];

    if ([self.sess canAddInput:newVideoInput]) {
        [self.sess addInput:newVideoInput];
    }
    if ([self.sess canAddOutput:newStillImageOutput]) {
        [self.sess addOutput:newStillImageOutput];
    }
    self.stillImageOutput = newStillImageOutput;

    if (device.lowLightBoostEnabled) {
        NSLog(@"ENABLED");
    }

    // register as an observer of changes to lowLightBoostEnabled
    [device addObserver:self forKeyPath:@"automaticallyEnablesLowLightBoostWhenAvailable" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:NULL];

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

    if ([keyPath isEqual:@"lowLightBoostEnabled"]) {

        NSLog(@"lowLightBoostEnabled changed");

        NSNumber *boostIsActiveValue = [change objectForKey:NSKeyValueChangeNewKey];

        BOOL boostIsActive = boostIsActiveValue.boolValue;

        NSLog(@"is low light boost currently active: %d", boostIsActive);
    }
}

任何人都可以给我任何帮助吗?我看过网上但没有找到非常确定的结果。我很感激能得到的所有帮助。

1 个答案:

答案 0 :(得分:0)

根据文档(以及头文件),你需要lockForConfiguration:

if ([[self backFacingCamera] respondsToSelector:@selector(isLowLightBoostSupported)]) {
  if ([[self backFacingCamera] lockForConfiguration:nil]) {
    if ([self backFacingCamera].isLowLightBoostSupported)
      [self backFacingCamera].automaticallyEnablesLowLightBoostWhenAvailable = YES;
    [[self backFacingCamera] unlockForConfiguration];
  }
}

此外,isLowLightBoostEnabled会告诉您实际上是否正在提升低光,而不是它是否可以提升。这是isLowLightBoostSupported选择器,如上所述(仅iOS 6设备响应)。