如何在CaptureElement中旋转相机视图?

时间:2014-11-24 16:46:08

标签: c# xaml windows-phone-8 camera windows-phone-8.1

我有以下问题。我用StackPanel写了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度。在我转向横向视图后,我有一个较小的窗口,其中是摄像机视图,但旋转是好的。

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

2 个答案:

答案 0 :(得分:5)

要旋转预览,您可以使用MediaCapture.SetPreviewRotation。另外,如果您想在手机的orintation更改后旋转预览,那么您可以订阅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 后,请记得取消订阅传感器。

答案 1 :(得分:0)

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

DisplayInformation.AutoRotationPreferences = DisplayOrientations.Landscape;

如果用户将设备保持纵向,这将显得很奇怪,因为您的其他控件可能方向错误。如果你想处理它,你可以像Romasz建议的那样,订阅OrientationChanged事件,并在那里为你的UI中的每个按钮应用RotateTransform(除了AppBar,它应该是正确的本身自动)抵消设备旋转并保持它们正面朝上。