2012-12-13 114 views
1

我在iOS上構建了一個原型應用程序,並且我正在使用一些蘋果示例代碼來做它(薄冰,我知道 - 此代碼使用goto語句:\)。我正在使用Session 520中的AVCam項目 - Camera Capture中的新功能。我不需要視頻捕捉功能,只需要靜止照片。iPad捕捉16:9張照片

該裝置輸入和輸出被設置正是如此:

// Init the device inputs 
    AVCaptureDeviceInput *newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self backFacingCamera] error:nil]; 
    AVCaptureDeviceInput *newAudioInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self audioDevice] error:nil]; 


    // Setup the still image file output 
    AVCaptureStillImageOutput *newStillImageOutput = [[AVCaptureStillImageOutput alloc] init]; 
    NSDictionary *outputSettings = @{AVVideoCodecKey: AVVideoCodecJPEG}; 
    [newStillImageOutput setOutputSettings:outputSettings]; 


    // Create session (use default AVCaptureSessionPresetHigh) 
    AVCaptureSession *newCaptureSession = [[AVCaptureSession alloc] init]; 


    // Add inputs and output to the capture session 
    if ([newCaptureSession canAddInput:newVideoInput]) { 
     [newCaptureSession addInput:newVideoInput]; 
    } 
    if ([newCaptureSession canAddInput:newAudioInput]) { 
     [newCaptureSession addInput:newAudioInput]; 
    } 
    if ([newCaptureSession canAddOutput:newStillImageOutput]) { 
     [newCaptureSession addOutput:newStillImageOutput]; 
    } 

    [self setStillImageOutput:newStillImageOutput]; 
    [self setVideoInput:newVideoInput]; 
    [self setAudioInput:newAudioInput]; 
    [self setSession:newCaptureSession]; 

這裏是當我點擊快門按鈕時調用的方法:

- (void) captureStillImage 
{ 
    AVCaptureConnection *stillImageConnection = [[self stillImageOutput] connectionWithMediaType:AVMediaTypeVideo]; 
    if ([stillImageConnection isVideoOrientationSupported]) 
     [stillImageConnection setVideoOrientation:orientation]; 

    [[self stillImageOutput] 
     captureStillImageAsynchronouslyFromConnection:stillImageConnection 
      completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) { 

       ALAssetsLibraryWriteImageCompletionBlock completionBlock = ^(NSURL *assetURL, NSError *error) { 
        if (error) 
        { 
         if ([[self delegate] respondsToSelector:@selector(captureManager:didFailWithError:)]) 
         { 
          [[self delegate] captureManager:self didFailWithError:error]; 
         } 
        } 
       }; 

       if (imageDataSampleBuffer != NULL) 
       { 
        NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer]; 
        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 

        UIImage *image = [[UIImage alloc] initWithData:imageData]; 

        if ([self.delegate respondsToSelector:@selector(captureManagerCapturedImage:)]) 
        { 
         dispatch_async(dispatch_get_main_queue(), ^{ 
          [self.delegate captureManagerCapturedImage:image]; 
         }); 
        } 

        [library writeImageToSavedPhotosAlbum:[image CGImage] 
               orientation:(ALAssetOrientation)[image imageOrientation] 
              completionBlock:completionBlock]; 

       } 
       else 
       { 
        completionBlock(nil, error); 
       } 

       if ([[self delegate] respondsToSelector:@selector(captureManagerStillImageCaptured:)]) 
       { 
        [[self delegate] captureManagerStillImageCaptured:self]; 
       } 
      }]; 
} 

此代碼成功地捕獲圖像,並保存它到圖書館。然而,在我工作的某個時候,它從捕獲500萬像素4:3圖像變爲捕獲1920x1080 16:9圖像。我找不到任何指定寬高比的位置,並且我沒有更改任何與攝像頭配置,捕捉會話或捕捉連接相關的代碼。爲什麼我的相機開始拍攝16:9照片?

更新:我剛剛重新運行Apple的原始示例代碼,它似乎也保存從視頻直接捕獲的16:9圖像。之前我很瘋狂,或者我用Camera.app進行了一次測試,並且正在看這個。所以我真正的問題是,如何在拍攝時在屏幕上顯示來自相機的實時信息,並拍攝全分辨率照片。我不能使用UIImagePickerController,因爲我需要能夠在實時攝像頭上疊加東西。

更新2:我能通過拋出我正在使用的AVCapture代碼來解決這個問題。事實證明,UIImagePickerController做我需要的。我沒有意識到你可以覆蓋自定義控件 - 我認爲它完成了整個屏幕,直到你完成拍照。

+0

那麼你從相機捕捉幀?因爲這意味着你最終以16:9結束。捕獲幀和拍照是不同的事情。 – Oritm

+0

啊,這可能是問題所在。我想要拍攝全分辨率的照片,而不僅僅是視頻中的一幀。據我所知,奇怪的是,我*拍攝了全屏照片,直到昨天的某個時刻,當它出現自發改變時。 –

+0

如果這可以解決您的問題,我會將我的評論複製到答案。如果沒有,我們必須深入研究代碼。 – Oritm

回答

1

如果您從視頻源捕捉幀,則最終將以16:9的分辨率結束。從視頻源捕獲幀並拍攝照片是不同的事情。

+1

如何從相機中顯示實時饋送,並仍然捕捉全分辨率靜止圖像?它看起來像' - [UIImagePickerController cameraOverlayView]'接近我所需要的(顯示一個覆蓋圖),但我需要能夠繼續拍照,而不會暫停每個UI之間的UI交互。 –

+0

其實我想我可以使它與'UIImagePickerController'一起工作。無論哪種方式,您的答案都是正確的 - 當我應該捕捉完整分辨率靜止圖像時,我正在捕捉實況視頻幀。 –

+0

林不知道..我明天會看看這個 – Oritm

相關問題