標籤: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分量與設定一致
視頻,掃描,識別相關