美文网首页iOS
iOS 扫一扫 弱光检测

iOS 扫一扫 弱光检测

作者: pruple_Boy | 来源:发表于2018-04-13 12:01 被阅读0次

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

    • 遵循协议
    协议:AVCaptureVideoDataOutputSampleBufferDelegate
    
    • 添加监听
     // 弱光识别监听
    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];
    }
    
    • 附上手电筒开关方法代码
    //照明按钮点击事件
    - (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];
    }
    

    记录每个值得记录的点

    相关文章

      网友评论

        本文标题:iOS 扫一扫 弱光检测

        本文链接:https://www.haomeiwen.com/subject/bhrckftx.html