2014-12-30 23 views
0

我正在開發一個Windows Phone 8.1應用程序,它使用MediaCapture類來錄製視頻。我想讓用戶自由地通過旋轉設備來以縱向或水平方式錄製視頻。請注意我的應用有固定的縱向方向。由於固定方向,我使用SimpleOrientationSensor類的OrientationChanged事件。如何以縱向方向錄製視頻?

現在我已經注意到,在默認相機應用程序和我的應用程序中錄製的portait視頻有一些差異。您可以在下面給出的圖像中看到不同之處。所有的視頻都是720p分辨率。播放器圖像是肖像鎖定方向。代碼也在下面給出。任何人都可以解釋我的代碼記錄縱向視頻時出了什麼問題嗎?謝謝!

enter image description here enter image description here

MediaCaptureInitializationSettings set; 
MediaCapture _camCapture; 
VideoRotation videoRotation = VideoRotation.None; 
bool _isRecording = false; 

// Camera resource disposal is handled properly 
private async Task InitializeCamera() 
{ 
    try 
    { 
     if (set == null) 
     { 
      set = new MediaCaptureInitializationSettings(); 

      DeviceInformation deviceID = (await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture)) 
             .FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back); 

      if (deviceID != null) 
      { 
       set.VideoDeviceId = deviceID.Id; 
      } 
     } 

     _camCapture = new MediaCapture(); 
     await _camCapture.InitializeAsync(set); 
     VideoView.Source = _camCapture; //VideoView is CaptureElement object in XAML 
     _camCapture.SetPreviewRotation(VideoRotation.Clockwise90Degrees); 
     await _camCapture.StartPreviewAsync(); 
    } 
    catch (Exception ex) 
    { 
    } 
} 

private async void OrientationChanged(object sender, SimpleOrientationSensorOrientationChangedEventArgs e) 
{ 
    try 
    { 
     await Dispatcher.RunAsync(CoreDispatcherPriority.Normal,() => 
     { 
      DetectCurrentOrintation(e.Orientation); 
     }); 
    } 
    catch (Exception ex) 
    { 
    } 
} 

private void DetectCurrentOrintation(SimpleOrientation orientation) 
{ 
    try 
    { 
     switch (orientation) 
     { 
      case SimpleOrientation.NotRotated: 
       videoRotation = VideoRotation.Clockwise90Degrees; 
       break; 
      case SimpleOrientation.Rotated90DegreesCounterclockwise: 
       videoRotation = VideoRotation.None; 
       break; 
      case SimpleOrientation.Rotated180DegreesCounterclockwise: 
       videoRotation = VideoRotation.Clockwise270Degrees; 
       break; 
      case SimpleOrientation.Rotated270DegreesCounterclockwise: 
       videoRotation = VideoRotation.Clockwise180Degrees; 
       break; 
      default: 
       break; 
     } 
    } 
    catch (Exception ex) 
    { 
    } 
} 

private async void btnRecordVideo_Click(object sender, RoutedEventArgs e) 
{ 
    try 
    { 
     if (!_isRecording) 
     { 
      var _videoRecording = await MyFolderInLocalFolder.CreateFileAsync("testRecord.mp4", CreationCollisionOption.GenerateUniqueName); 
      MediaEncodingProfile mEncode = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Auto); 

      _isRecording = true; 

      //Two videos are with SetRecordRotation(value) and 
      //for two other I commeted out the line. 
      _camCapture.SetRecordRotation(videoRotation); 
      await _camCapture.StartRecordToStorageFileAsync(mEncode, _videoRecording); 
     } 
     else 
     { 
      await _camCapture.StopRecordAsync(); 
      _isRecording = false; 
     } 
    } 
    catch (Exception ex) 
    { 
    } 
} 
+0

問題與主要問題完全更新。 – Xyroid

回答

0

還有就是微軟的GitHub頁面上的樣本是相關的,雖然他們面向Windows 10.不過,API應該在8/8.1的工作。

UniversalCameraSample:這一個捕獲視頻並支持縱向和橫向方向。下面是相關的部分:

// Create storage file in Pictures Library 
var videoFile = await KnownFolders.PicturesLibrary.CreateFileAsync("SimpleVideo.mp4", CreationCollisionOption.GenerateUniqueName); 

var encodingProfile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Auto); 

// Calculate rotation angle, taking mirroring into account if necessary 
var rotationAngle = 360 - ConvertDeviceOrientationToDegrees(GetCameraOrientation()); 
encodingProfile.Video.Properties.Add(RotationKey, PropertyValue.CreateInt32(rotationAngle)); 

await _mediaCapture.StartRecordToStorageFileAsync(encodingProfile, videoFile); 

有在樣品仔細看看,看看如何讓照相機的姿態擺在首位(對它的調用在我張貼的片斷正在取得)。您通常不想調用SetRecordRotation(或SetPreviewRotation),因爲這些對於添加旋轉元數據具有一定的影響。

觀看camera session從近期//編譯/會議,其中包括通過一些相機樣本演練的一點點,讓您在樣品周圍更多的上下文。

相關問題