iOS

iOS 扫一扫 弱光检测

2018-04-13  本文已影响0人  pruple_Boy

如题,处理后摄像头的光感值,确定手电筒显示与隐藏

协议:AVCaptureVideoDataOutputSampleBufferDelegate
 // 弱光识别监听
AVCaptureVideoDataOutput *buffer = [[AVCaptureVideoDataOutput alloc] init];
[buffer setSampleBufferDelegate:self queue:dispatch_get_main_queue()];

if ([self.session canAddOutput:buffer]) [self.session addOutput:buffer];
- (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;
    }
}
// 呼吸灯动画
- (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];
}
//照明按钮点击事件
- (void)lightBtnOnClick:(UIButton *)btn
{
    [btn.layer removeAllAnimations];
    
    // ppExt:不能使用_device:无相机权限不能打开
    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];
}

记录每个值得记录的点

上一篇下一篇

猜你喜欢

热点阅读