2014-11-24 72 views
1

我有以下问题。我写StackPanelCaptureElement如何在CaptureElement中旋转摄像头视图?

<StackPanel> 
     <CaptureElement x:Name="PreviewElement" 
         HorizontalAlignment="Center" 
         VerticalAlignment="Center" 
         Width="380" 
         Height="560" 
         Stretch="UniformToFill"/> 
</StackPanel> 

在xaml.cs文件视图下:

protected override async void OnNavigatedTo(NavigationEventArgs e) 
{ 
    cameraCapture = new CameraCapture(); 
    PreviewElement.Source = await cameraCapture.Initialize(); 
    await cameraCapture.StartPreview();    
} 

而且我有至极类CameraCapture.cs我有所有的方法:

public class CameraCapture 
    { 
     MediaCapture mediaCapture; 
     ImageEncodingProperties imgEncodingProperties; 

     public async Task<MediaCapture> Initialize(CaptureUse primaryUse = CaptureUse.Photo) 
     { 
      //Create media capture and INIT 
      var cameraID = await GetCameraID(Windows.Devices.Enumeration.Panel.Back); 
      mediaCapture = new MediaCapture(); 
      await mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings 
      { 
       StreamingCaptureMode = StreamingCaptureMode.Video, 
       PhotoCaptureSource = PhotoCaptureSource.Photo, 
       AudioDeviceId = string.Empty, 
       VideoDeviceId = cameraID.Id 
      }); 
      mediaCapture.VideoDeviceController.PrimaryUse = primaryUse; 

      //Create photo encoding properties as JPEG and set the size that should be use for photo capturing 
      imgEncodingProperties = ImageEncodingProperties.CreateJpeg(); 
      imgEncodingProperties.Width = 640; 
      imgEncodingProperties.Height = 480; 

      return mediaCapture; 
     } 
     public async Task StartPreview() 
     { 
      //Start preview stream 
      await mediaCapture.StartPreviewAsync(); 
     }  
     private static async Task<DeviceInformation> GetCameraID(Windows.Devices.Enumeration.Panel desired) 
     { 
      DeviceInformation deviceID = (await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture)) 
       .FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == desired); 

      if (deviceID != null) return deviceID; 
      else throw new Exception(string.Format("Camera of type {0} doesn't exist.", desired)); 
     } 
    } 

当我在纵向旋转中运行应用程序时,我将摄像头视图顺时针旋转了90度。转到风景视图后,我有一个较小的窗口,其中是相机视图,但旋转很好。

任何想法如何解决这个问题?

+0

你试过'cameraCapture.SetPreviewRotation(VideoRotation.Clockwise90Degrees);'? – Romasz 2014-11-24 17:40:43

+0

我已经试过这个解决方案,结果是:纵向视图是好的,但横向视图现在旋转-.- – Sowiarz 2014-11-24 20:32:22

回答

4

要旋转你的预览,你可以使用MediaCapture.SetPreviewRotation。 Additianally如果你想旋转预览,一旦手机的神经改变,那么你可以订阅SimpleOrientationSensor.OrientationChanged事件。它可能看起来例如像这样:

captureManager.SetPreviewRotation(VideoRotation.Clockwise90Degrees); 
capturePreview.Source = captureManager; 
await captureManager.StartPreviewAsync(); 
SimpleOrientationSensor sensor = SimpleOrientationSensor.GetDefault(); 
sensor.OrientationChanged += (s, arg) => 
{ 
    switch (arg.Orientation) 
    { 
     case SimpleOrientation.Rotated90DegreesCounterclockwise: 
      captureManager.SetPreviewRotation(VideoRotation.None); 
      break; 
     case SimpleOrientation.Rotated180DegreesCounterclockwise: 
     case SimpleOrientation.Rotated270DegreesCounterclockwise: 
      captureManager.SetPreviewRotation(VideoRotation.Clockwise180Degrees); 
      break; 
     default: 
      captureManager.SetPreviewRotation(VideoRotation.Clockwise90Degrees); 
      break; 
    } 
}; 

请也记得从传感器到退订一旦你完成预览/处置MediaCapture

0

我发现最好的方法是实际旋转控件,或者对于相机预览是主控件的完整相机应用程序,使用CaptureElement的页面的OnNavigatedTo事件的整个页面:

DisplayInformation.AutoRotationPreferences = DisplayOrientations.Landscape; 

这看起来很奇怪,如果用户保持其纵向装置,因为你的其他控件可能有错误的方向。如果您想要处理该问题,可以按照Romasz的建议进行操作,订阅OrientationChanged事件,然后在其中应用RotateTransform至您的用户界面中的每个按钮(AppBar除外),以抵消设备旋转并保持他们正确的一面。

相关问题