2015-01-13 97 views
4

我正在開發一款windows phone應用程序,該應用程序需要通過前置攝像頭使用c#捕獲視頻,我可以使用後置攝像頭捕獲視頻,但我需要在前置攝像頭的幫助下拍攝。我在這方面搜索了很多,但找不到相關答案。您的幫助將不勝感激。如何使用c#在windows phone中使用前置攝像頭捕捉視頻#

public partial class Movies : PhoneApplicationPage 
    { 

     VideoBrush myvideobrush;  //for capturing video. 
     CaptureSource myvideosource; //source for capturing video. 
     VideoCaptureDevice mydevice; //device for capturing video. 
     FileSink myfilesink;   //for storing the video. 
     private string isoVideoFileName = "CameraMovie.mp4"; 
     private IsolatedStorageFileStream isoVideoFile; 


     public Movies() 
     { 

      InitializeComponent(); 
      if (myvideosource == null) 
      { 
       myvideosource = new CaptureSource(); 
       myfilesink = new FileSink(); 
       mydevice = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice(); 

       //System.Collections.ObjectModel.ReadOnlyCollection<System.Windows.Media.VideoCaptureDevice> supportedcams = CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices(); 
       //mydevice = supportedcams.ElementAt(0); 
      } 
      if (mydevice != null) 
      { 
       myvideobrush = new VideoBrush(); 

       myvideobrush.SetSource(myvideosource); 
       viewFinderRectangle.Fill = myvideobrush; 
       stop_btn.IsEnabled = false; 
       myvideosource.Start(); 
      } 

     } 
     public void startReccording() 
     { 
      start_btn.IsEnabled = false; 
      stop_btn.IsEnabled = true; 


      if (myvideosource.VideoCaptureDevice != null && myvideosource.State == CaptureState.Started) 
      { 
       myvideosource.Stop(); 
       myfilesink.CaptureSource = myvideosource; 
       myfilesink.IsolatedStorageFileName = isoVideoFileName; 
      } 
      if (myvideosource.VideoCaptureDevice != null && myvideosource.State == CaptureState.Stopped) 
      { 

       myvideosource.Start(); 
      } 
     } 
     public void stopRecording() 
     { 


      if (myvideosource.VideoCaptureDevice != null && myvideosource.State == CaptureState.Started) 
      { 
       myvideosource.Stop(); 

       myfilesink.CaptureSource = null; 
       myfilesink.IsolatedStorageFileName = null; 
       videoPriview(); 
      } 



     } 
     public void videoPriview() 
     { 

      if (isoVideoFile != null) 
      { 
       videoPlayer.Play(); 
      } 
      else 
      { 
       myvideosource.Stop(); 
       viewFinderRectangle.Fill = null; 
       isoVideoFile = new IsolatedStorageFileStream(isoVideoFileName, FileMode.Open, FileAccess.Read, IsolatedStorageFile.GetUserStoreForApplication()); 
       videoPlayer.SetSource(isoVideoFile); 
       videoPlayer.Play(); 
      } 
      start_btn.IsEnabled = true; 
      stop_btn.IsEnabled = false; 

     } 


     private void movies_goback_btn_Click(object sender, RoutedEventArgs e) 
     { 
      NavigationService.GoBack(); 
     } 

     private void start_btn_Click(object sender, RoutedEventArgs e) 
     { 
      startReccording(); 
     } 

     private void stop_btn_Click(object sender, RoutedEventArgs e) 
     { 
      stopRecording(); 
     } 
    } 
} 

回答

0

使用CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices()獲得的所有設備上可用的相機列表中,選擇一個面向前方,並將其分配給您的mydevice在變。

當訪問前置攝像頭時,不要忘記在清單中設置ID_REQ_FRONTCAMERA權限。

+0

我用這個,但仍無法我的要求做任務 System.Collections.ObjectModel.ReadOnlyCollection supportedcams = CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices(); mydevice = supportedcams.ElementAt(1); –

1

CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices()節目列表支持的相機從CaptureDeviceCaptureDevice返回ReadOnlyCollection<VideoCaptureDevice>

VideoCaptureDevice繼承擁有財產性FriendlyNameIsDefaultDevice

對於我的諾基亞FriendlyName可能值:

  • "Self portrait camera"
  • "Primary camera"

對於我的諾基亞IsDefaultDevicePrimary camera

所以最後的代碼,這有助於找到正面的相機總是如此看起來是這樣的:

VideoCaptureDevice frontDevice = null; 
ReadOnlyCollection<VideoCaptureDevice> devices = CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices(); 

foreach (VideoCaptureDevice dev in devices) 
{ 
    if (!dev.IsDefaultDevice) 
    { 
     device = dev; 
    } 
} 

// for now device contains front-face camera 
相關問題