3
我從來沒有與AVFoundation框架合作過,我想從後置攝像頭獲取視頻幀並使用這些幀進行處理。任何人都可以幫助我,您的經驗將不勝感激。由於攝像頭的視頻幀在iOS中使用AVFoundation框架?
我從來沒有與AVFoundation框架合作過,我想從後置攝像頭獲取視頻幀並使用這些幀進行處理。任何人都可以幫助我,您的經驗將不勝感激。由於攝像頭的視頻幀在iOS中使用AVFoundation框架?
您可以使用下面的代碼與AVFoundation啓動相機會以拍攝靜止圖像:
AVCaptureSession *session;
AVCaptureStillImageOutput *stillImageOutput;
session = [[AVCaptureSession alloc] init];
[session setSessionPreset:AVCaptureSessionPresetPhoto];
AVCaptureDevice *inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error;
AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:&error];
if ([session canAddInput:deviceInput]) {
[session addInput:deviceInput];
}
AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
[previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
CALayer *rootLayer = [[self view] layer];
[rootLayer setMasksToBounds:YES];
CGRect frame = self.frameForCapture.frame;
[previewLayer setFrame:frame];
[rootLayer insertSublayer:previewLayer atIndex:0];
stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG, AVVideoCodecKey, nil];
[stillImageOutput setOutputSettings:outputSettings];
[session addOutput:stillImageOutput];
[session startRunning];
然後,以實際拍攝的圖像,你可以用一個按鈕具有以下代碼:
- (IBAction)takePhoto:(id)sender {
AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in stillImageOutput.connections) {
for (AVCaptureInputPort *port in [connection inputPorts]) {
if ([[port mediaType] isEqual:AVMediaTypeVideo]) {
videoConnection = connection;
break;
}
}
if (videoConnection) {
break;
}
}
[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection
completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
if (imageDataSampleBuffer != NULL) {
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
UIImage *image = [UIImage imageWithData:imageData];
}
}];
}
然後,你可以做任何你想做的事情與保存的圖像。
過程意味着什麼?您想做什麼? – naresh
此外,我想匹配模板:naresh –
http://stackoverflow.com/questions/23882605/how-to-capture-frame-by-frame-images-from-iphone-video-recording-in-real-time – naresh