如題,處理后攝像頭的光感值装黑,確定手電筒顯示與隱藏
- 遵循協(xié)議
協(xié)議:AVCaptureVideoDataOutputSampleBufferDelegate
- 添加監(jiān)聽
// 弱光識別監(jiān)聽
AVCaptureVideoDataOutput *buffer = [[AVCaptureVideoDataOutput alloc] init];
[buffer setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
if ([self.session canAddOutput:buffer]) [self.session addOutput:buffer];
- AVCaptureVideoDataOutputSampleBufferDelegate 代理
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
static BOOL isStop = false;
if (isStop) return;
isStop = true;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
isStop = false;
});
CFDictionaryRef metadataDict = CMCopyDictionaryOfAttachments(NULL,sampleBuffer, kCMAttachmentMode_ShouldPropagate);
NSDictionary *metadata = [[NSMutableDictionary alloc] initWithDictionary:(__bridge NSDictionary*)metadataDict];
CFRelease(metadataDict);
NSDictionary *exifMetadata = [[metadata objectForKey:(NSString *)kCGImagePropertyExifDictionary] mutableCopy];
float brightnessValue = [[exifMetadata objectForKey:(NSString *)kCGImagePropertyExifBrightnessValue] floatValue];
UIButton *lightBtn = [self.view viewWithTag:111111];
if (brightnessValue < 0 && !lightBtn.selected && lightBtn.isHidden) {
[lightBtn.layer removeAllAnimations];
lightBtn.hidden = false;
[lightBtn alphaOrOpacityAnimation]; // 心跳動畫
}
if (brightnessValue > 0 && !lightBtn.selected && !lightBtn.isHidden) {
[lightBtn.layer removeAllAnimations];
lightBtn.hidden = true;
}
}
- 心跳動畫:UIView的Animation類目
// 呼吸燈動畫
- (void)alphaOrOpacityAnimation {
CABasicAnimation *animation =[CABasicAnimation animationWithKeyPath:@"opacity"];
animation.fromValue = [NSNumber numberWithFloat:1.0f];
animation.toValue = [NSNumber numberWithFloat:0.1f]; // 透明度肋僧。
animation.autoreverses = YES;
animation.duration = 0.75;
animation.repeatCount = 2;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards; //removedOnCompletion,fillMode配合使用保持動畫完成效果
animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
[self.layer addAnimation:animation forKey:nil];
}
- 附上手電筒開關(guān)方法代碼
//照明按鈕點(diǎn)擊事件
- (void)lightBtnOnClick:(UIButton *)btn
{
[btn.layer removeAllAnimations];
// ppExt:不能使用_device:無相機(jī)權(quán)限不能打開
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
//判斷是否有閃光燈
if (![device hasTorch]) {
[self skipToApplicationSettingWith:Error_Torch_Failure IsToSetting:false CancelHanlder:nil];
return;
}
btn.selected = !btn.selected;
[device lockForConfiguration:nil];
if (btn.selected) {
[device setTorchMode:AVCaptureTorchModeOn];
}else
{
[device setTorchMode:AVCaptureTorchModeOff];
}
[device unlockForConfiguration];
}
記錄每個值得記錄的點(diǎn)