2012-03-13 61 views
0

我想從iPad上的視頻捕獲圖片。我以Apple的AVCam例子爲出發點。使用AVFoundation從視頻中捕獲圖片

我能夠在我的應用程序中看到視頻並從中拍攝照片。我的問題是結果圖像的像素大小是錯誤的。我想要一個全屏圖片(1024x768),但我得到一個較小的(1024x720)。

這些都是我的實例變量:

@property (retain) AVCaptureStillImageOutput *stillImageOutput; 
@property (retain) AVCaptureVideoPreviewLayer *previewLayer; 
@property (retain) AVCaptureSession *captureSession; 
@property (retain) AVCaptureConnection *captureConnection; 
@property (retain) UIImage *stillImage; 

下面的代碼拍照:

- (void)takePicture 
{ 
    AVCaptureConnection *videoConnection = nil; 
    for (AVCaptureConnection *connection in [[self stillImageOutput] connections]) { 
     for (AVCaptureInputPort *port in [connection inputPorts]) { 
      if ([[port mediaType] isEqual:AVMediaTypeVideo]) { 
       videoConnection = connection; 
       break; 
      } 
     } 
     if (videoConnection) { 
      break; 
     } 
    } 
    NSLog(@"about to request a capture from: %@", [self stillImageOutput]); 
    [[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:videoConnection 
                 completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error) { 
                  CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL); 
                  if (exifAttachments) { 
                   NSLog(@"attachements: %@", exifAttachments); 
                  } else { 
                   NSLog(@"no attachments"); 
                  } 
                  NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]; 
                  UIImage *image = [[UIImage alloc] initWithData:imageData]; 
                  [self setStillImage:image]; 
                  [image release]; 
                  [[NSNotificationCenter defaultCenter] postNotificationName:kImageCapturedSuccessfully object:nil]; 
                 }]; 
} 

我想調整的最後畫面,但這種解決方案會降低圖像的質量。 我也意識到CFDictionaryRef exifAttachments字典包含一個值PixelYDimension = 720;,但我似乎無法找到與之交互的方式。

任何幫助將非常感激。 提前謝謝您,祝您有愉快的一天,

Alex。

編輯:我想指出的是,當我說「從視頻拍照」我的意思是,視頻直播從iPad的相機來了,它不是錄音。

回答

2

我找到了解決我的問題的方法。在這種情況下,有人會在未來尋找這一點。

要使用AVFoundation與相機交互,我們需要啓動一個AVCaptureSession變量。 這樣做後,我們可以修改sessionPreset表示輸出的質量等級或比特率。 有一個set of different constants。拍攝1024x768圖片我使用AVCaptureSessionPresetPhoto