2015-07-28 6 views
3

Я никогда не работал с AVFoundation Framework, я хочу получить видеокадры с задней камеры и обработать эти кадры. Любой, кто поможет мне, ваш опыт будет оценен по достоинству. БлагодаряВидеокадр с камеры с использованием AVFoundation Framework в iOS?

+0

процесс означает? что ты хочешь делать? – naresh

+0

Далее я хочу совместить с шаблоном: naresh –

+1

http://stackoverflow.com/questions/23882605/how-to-capture-frame-by-frame-images-from-iphone-video-recording-in-real-time – naresh

ответ

5

Вы можете использовать следующий код для запуска камеры сессии с 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]; 
                 } 
                }]; 
} 

Затем вы можете делать все, что хотите, с сохраненным изображением.

Смежные вопросы