iOS 掃一掃 弱光檢測

如題,處理后攝像頭的光感值,確定手電筒顯示與隱藏

  • 遵循協(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)方法代碼
//照明按鈕點擊事件
- (void)lightBtnOnClick:(UIButton *)btn
{
    [btn.layer removeAllAnimations];
    
    // ppExt:不能使用_device:無相機權(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];
}

記錄每個值得記錄的點

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • github排名https://github.com/trending,github搜索:https://gith...
    小米君的demo閱讀 4,946評論 2 38
  • 一日一簽 世界上最棒的事是,每天早上醒來又是嶄新的一天,完全免費,永不斷貨。 疼痛的日子,總是希望身體的健康。也總...
    愛畫的飯飯閱讀 181評論 0 3
  • 每晚睡前,我都會習慣性地刷一遍朋友圈,默默圍觀親戚妯娌和七姑八姨們輪番轉(zhuǎn)發(fā)的養(yǎng)生鏈接、微商小販的廣告宣傳、電商賣家...
    我從未見過你閱讀 383評論 0 0
  • 萬曇一閱讀 144評論 0 0
  • 篇目:《拆掉思維的墻》第五、六章 文章復述: 今天看了兩章,第一章的主題是職業(yè),作者從職業(yè),找到感興趣的職業(yè)、不要...
    情情歌閱讀 207評論 0 0

友情鏈接更多精彩內(nèi)容