码迷,mamicode.com
首页 > 其他好文 > 详细

视频,扫描,识别相关

时间:2014-09-05 17:51:01      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:color   os   io   ar   for   文件   数据   art   cti   

头文件

包含AVFoundation.framework

1、初始化Capture    // Grab the back-facing camera
    AVCaptureDevice *backFacingCamera = nil;
    NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    for (AVCaptureDevice *device in devices)
    {
        if ([device position] == AVCaptureDevicePositionBack)
        {
            backFacingCamera = device;
            break;
        }
    }
    
    // Create the capture session
    _captureSession = [[AVCaptureSession alloc] init];
    
    // Add the video input
    NSError *error = nil;
    _videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:backFacingCamera error:&error];
    if ([_captureSession canAddInput:_videoInput])
    {
        [_captureSession addInput:_videoInput];
    }
    // Add the video frame output
    _videoOutput = [[AVCaptureVideoDataOutput alloc] init];
    [_videoOutput setAlwaysDiscardsLateVideoFrames:YES];
    // Use RGB frames instead of YUV to ease color processing
    NSDictionary *settings = @{(id)kCVPixelBufferPixelFormatTypeKey: [NSNumber numberWithInt:kCVPixelFormatType_32BGRA]};
    [_videoOutput setVideoSettings:settings];
    //    dispatch_queue_t videoQueue = dispatch_queue_create("com.sunsetlakesoftware.colortracking.videoqueue", NULL);
    //    [videoOutput setSampleBufferDelegate:self queue:videoQueue];
    
    [_videoOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
    
    if ([_captureSession canAddOutput:_videoOutput])
    {
        [_captureSession addOutput:_videoOutput];
    }
    else
    {
        NSLog(@"Couldn‘t add video output");
    }
    
    [_captureSession setSessionPreset:AVCaptureSessionPresetMedium];
 

2、启动 录制

[_captureSession startRunning];

3、检测识别

- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
       fromConnection:(AVCaptureConnection *)connection {
    CVPixelBufferRef pixelBuffer = (CVPixelBufferRef)CMSampleBufferGetImageBuffer(sampleBuffer);
    
    CVPixelBufferLockBaseAddress(pixelBuffer, 0);
    
    unsigned char *data = (unsigned char *)CVPixelBufferGetBaseAddress(pixelBuffer);
    //        int bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer);
    
    int bufferHeight = (int)CVPixelBufferGetHeight(pixelBuffer);
    int bufferWidth = (int)CVPixelBufferGetWidth(pixelBuffer);
    int countPerRow = bufferWidth * 4;
   

    //do something


    CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);

}

 

3、停止 录制

 [_captureSession stopRunning];

 

4、注意事项

    > 处理时,尽量避免算法过于复杂,导致卡顿

    > 注意数据的 R、G、B、A分量与设定一致

 

视频,扫描,识别相关

标签:color   os   io   ar   for   文件   数据   art   cti   

原文地址:http://www.cnblogs.com/innovator/p/3958400.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!