2012-12-05 24 views
1

我使用下面的代碼使用PhotoCaptureDevice選項拿卡中的Windows Phone 8圖像捕捉設備不顯示任何

if (PhotoCaptureDevice.AvailableSensorLocations.Contains(CameraSensorLocation.Back) || 
       PhotoCaptureDevice.AvailableSensorLocations.Contains(CameraSensorLocation.Front)) 
      { 
       // Initialize the camera, when available. 
       if (PhotoCaptureDevice.AvailableSensorLocations.Contains(CameraSensorLocation.Back)) 
       { 
        // Use the back camera. 
        System.Collections.Generic.IReadOnlyList<Windows.Foundation.Size> SupportedResolutions = 
         PhotoCaptureDevice.GetAvailableCaptureResolutions(CameraSensorLocation.Back); 
        Windows.Foundation.Size res = SupportedResolutions[0]; 
        d = await PhotoCaptureDevice.OpenAsync(CameraSensorLocation.Back, res); 
       } 
       else 
       { 
        // Otherwise, use the front camera. 
        System.Collections.Generic.IReadOnlyList<Windows.Foundation.Size> SupportedResolutions = 
         PhotoCaptureDevice.GetAvailableCaptureResolutions(CameraSensorLocation.Front); 
        Windows.Foundation.Size res = SupportedResolutions[0]; 
        d = await PhotoCaptureDevice.OpenAsync(CameraSensorLocation.Front, res); 
       } 

       await d.SetPreviewResolutionAsync(new Windows.Foundation.Size(640, 480)); 
       await d.SetCaptureResolutionAsync(new Windows.Foundation.Size(640, 480)); 

       d.SetProperty(KnownCameraGeneralProperties.EncodeWithOrientation, 
           d.SensorLocation == CameraSensorLocation.Back ? 
           d.SensorRotationInDegrees : -d.SensorRotationInDegrees); 

       _device = d; 
      } 

最後我該設備設置爲視頻刷的來源。但是當應用程序通過按住後退鍵進入該狀態時,我會從休眠狀態進入休眠狀態,顯示空白頁並且不顯示任何攝像機。你能請任何人幫助我嗎?

回答

3

諾基亞擁有WP8的PhotoCaptureDevice中啓用的許多功能的很棒的示例應用程序。看看@http://projects.developer.nokia.com/cameraexplorer

甚至還有一個在第一頁上使用PhotoCaptureDevice作爲VideoBrush.SetSource()的端到端示例。從MainPage.xaml.cs中的OnNavigatedTo方法開始看到如何初始化PhotoCaptureDevice一個相機取景器@http://projects.developer.nokia.com/cameraexplorer/browser/CameraExplorer/MainPage.xaml.cs

以下是有關部分:

/// <summary> 
    /// If camera has not been initialized when navigating to this page, initialization 
    /// will be started asynchronously in this method. Once initialization has been 
    /// completed the camera will be set as a source to the VideoBrush element 
    /// declared in XAML. On-screen controls are enabled when camera has been initialized. 
    /// </summary> 
    protected async override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     if (_dataContext.Device == null) 
     { 
      ShowProgress("Initializing camera..."); 

      await InitializeCamera(CameraSensorLocation.Back); 

      HideProgress(); 
     } 

     videoBrush.RelativeTransform = new CompositeTransform() 
     { 
      CenterX = 0.5, 
      CenterY = 0.5, 
      Rotation = _dataContext.Device.SensorLocation == CameraSensorLocation.Back ? 
         _dataContext.Device.SensorRotationInDegrees : 
        - _dataContext.Device.SensorRotationInDegrees 
     }; 

     videoBrush.SetSource(_dataContext.Device); 

     overlayComboBox.Opacity = 1; 

     SetScreenButtonsEnabled(true); 
     SetCameraButtonsEnabled(true); 

     base.OnNavigatedTo(e); 
    } 

    /// <summary> 
    /// Initializes camera. Once initialized the instance is set to the DataContext.Device property 
    /// for further usage from this or other pages. 
    /// </summary> 
    /// <param name="sensorLocation">Camera sensor to initialize</param> 
    private async Task InitializeCamera(CameraSensorLocation sensorLocation) 
    { 
     Windows.Foundation.Size initialResolution = new Windows.Foundation.Size(640, 480); 
     Windows.Foundation.Size previewResolution = new Windows.Foundation.Size(640, 480); 
     Windows.Foundation.Size captureResolution = new Windows.Foundation.Size(640, 480); 

     PhotoCaptureDevice d = await PhotoCaptureDevice.OpenAsync(sensorLocation, initialResolution); 

     await d.SetPreviewResolutionAsync(previewResolution); 
     await d.SetCaptureResolutionAsync(captureResolution); 

     d.SetProperty(KnownCameraGeneralProperties.EncodeWithOrientation, 
         d.SensorLocation == CameraSensorLocation.Back ? 
         d.SensorRotationInDegrees : - d.SensorRotationInDegrees); 

     _dataContext.Device = d; 
    } 

和有關XAML:

 <Canvas x:Name="VideoCanvas"> 
      <Canvas.Background> 
       <VideoBrush x:Name="videoBrush"/> 
      </Canvas.Background> 
      <Rectangle x:Name="FocusIndicator" Stroke='Red' Opacity="0.7" Width="80" Height="80" StrokeThickness="5" Visibility="Collapsed"/> 
     </Canvas> 
+1

它將在我們從休眠狀態進入時重新初始化VideoBrush時起作用。 –

+0

嗨賈斯汀,有沒有什麼辦法來改變預覽和捕捉分辨率。如果我設置了一些值,它會引發異常。你能幫我解決這個問題嗎? –