iOSスイープ弱光検出

3314 ワード

問題のように、処理後のカメラの光感度値は、懐中電灯の表示と非表示を決定します.
  • プロトコル
  • に従う
     :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];
    }
    

    各記録に値する点を記録する