c# - 如何在 CaptureElement 中旋转相机 View ?

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

我有以下问题。我用 CaptureElement 编写了 StackPanel:

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

在该 View 下的 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));
        }
    }

当我以纵向旋转方式运行应用程序时,相机 View 逆时针旋转 90 度。当我转向横向 View 后,我有一个较小的窗口,其中是相机 View ,但旋转效果很好。

有什么想法可以解决这个问题吗?

最佳答案

要旋转预览,您可以使用 MediaCapture.SetPreviewRotation 。此外,如果您想在手机方向更改后旋转预览,则可以订阅 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后,还请记住取消订阅传感器。

关于c# - 如何在 CaptureElement 中旋转相机 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27109817/

相关文章:

c# - 在 map 上作为图钉的图像 - Windows Phone 8

c# - 在 C# 中设置和获取

c# - Azure 上的 ASP.NET Core 本地化

windows-phone-8 - System.NullReferenceException 使用 app.xaml.cs 中的 T 列表

azure - 如何在 Windows Phone 8 上从 Azure 中的表读取数据

c# - 如何绑定(bind)到集合集合下的对象的属性?

c# - 任务层次结构示例未按预期工作

c# - 俄罗斯增值税号验证

c# - 只有托盘图标的 WPF 应用程序

c# - 与大写字母一样大的 TextBlock(忽略字体升序/降序)