美文网首页视频
使用摄像头采集视频数据

使用摄像头采集视频数据

作者: zjunchao | 来源:发表于2017-07-14 17:42 被阅读138次

    AVCaptureSession

    采集架构图

    通过AVCaptureSession把 input和output关联起来。

    输入设备

    • Input
      • Mic
      • Camare

    输出设备

    • outPut
      • AVCaptureMovieFileOutput
      • AVCaptureStillImageOutput
      • AVCaptureVideoPreViewLayer

    AVCaptureConnection 提供把输入和输出连接起来的功能


    采集图二

    AVCaptureSessionPreset

    使用AVCaptureSessionPreset 指定摄像头采集的分辨率


    AVCaptureVideoPreviewLayer

    创建预览视图,可以直接将采集到的画面渲染出来。

    关键代码参考

    - (void) setupVideoCamareWithPosition:(AVCaptureDevicePosition) cameraPosition{
        
        // 1. get input device
        NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
        for (AVCaptureDevice *device in devices) {
            if (device.position == cameraPosition) {
                self.inputCamareDevice = device;
            }
        }
        // 2. captureSession
        BOOL enableApplicationSession  =  YES;
        if (enableApplicationSession) {
            self.captureSession.automaticallyConfiguresApplicationAudioSession = YES;
            self.captureSession.usesApplicationAudioSession = YES;
        }
        [self.captureSession beginConfiguration];
        
        //  3. add input
        NSError * error = nil;
        self.videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:_inputCamareDevice error:&error];
        if ([self.captureSession canAddInput:self.videoInput]) {
            [self.captureSession addInput:_videoInput];
        }
        
        // 4. add outPut
        [self.videoDataOutPut setSampleBufferDelegate:self queue:_cameraProcessingQueue];
        if ([self.captureSession canAddOutput:_videoDataOutPut]) {
            [self.captureSession addOutput:_videoDataOutPut];
        } else {
            NSLog(@"can't add video output");
        }
        if ([self.captureSession canSetSessionPreset:AVCaptureSessionPreset640x480]) {
            [self.captureSession setSessionPreset:AVCaptureSessionPreset640x480];
        }
        [self.captureSession commitConfiguration];
    }
    //   5. setupPreivew
    - (void) setupPreviewWithView:(UIView *) view{
        if (!view) {
            return;
        }
        CALayer *layer = view.layer;
        layer.masksToBounds = YES;
        self.previewLayer.frame = layer.bounds;
        self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
        [layer insertSublayer:self.previewLayer atIndex:0];
    }
    - (void) startCapture{
        if (![_captureSession isRunning]) {
            [_captureSession startRunning];
        }
    }
    
    

    demo 地址:https://github.com/zjunchao/streaming_tutorial/tree/master/tutorial01

    相关文章

      网友评论

        本文标题:使用摄像头采集视频数据

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