首页 > 代码库 > 视频,扫描,识别相关
视频,扫描,识别相关
头文件
包含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 = http://www.mamicode.com/(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分量与设定一致
视频,扫描,识别相关