【问题标题】:Knowing screen orientation + Side Windows Store App了解屏幕方向 + Side Windows Store App
【发布时间】:2014-05-28 15:46:29
【问题描述】:

我使用 MediaCapture API 创建了一个相机应用程序,并尝试让预览显示相对于用户持有设备的方式。例如,应用程序假设用户以纵向模式持有设备并显示提要,但是当用户将设备向左或向右旋转 90 度时,我如何判断用户是顺时针还是逆时针旋转设备相应地显示提要。

我知道我可以将屏幕方向设置为横向或纵向,但这并不能告诉我应该将提要旋转多少。

谢谢。

【问题讨论】:

    标签: c# windows winapi windows-phone-8 windows-runtime


    【解决方案1】:

    使用来自Windows.Graphics.Display.DisplayInformation 类的值(通过DisplayInformation.getForCurrentView() 获得)及其currentOrientation 属性。这标识了设备相对于其原生方向可能的四个旋转象限之一:横向、纵向、横向翻转和纵向翻转。还有一个orientationchanged 事件,您可以使用它来检测更改(有关此事件的用法,请参见Display orientation sample 的场景3)。

    【讨论】:

      【解决方案2】:

      您可以使用SimpleOrientation 传感器通过应用程序确定设备方向,检查 here

      【讨论】:

        【解决方案3】:

        Microsoft github 页面上发布了两个相关示例,尽管它们针对的是 Windows 10。不过,API 应该可以在 8/8.1 上运行。

        GetPreviewFrame:此示例不会锁定页面旋转,而是对预览流应用校正旋转。它不使用SetPreviewRotation,因为该方法比使用元数据方法更耗费资源。此示例不拍摄照片(只是预览帧)。

        UniversalCameraSample:这个确实可以拍摄照片,并且支持纵向和横向,尽管它会尝试将页面锁定为横向(通过AutoRotationPreferences)。

        这是第一个示例处理方向变化的方式:

        private async void DisplayInformation_OrientationChanged(DisplayInformation sender, object args)
        {
            _displayOrientation = sender.CurrentOrientation;
        
            if (_isPreviewing)
            {
                await SetPreviewRotationAsync();
            }
        }
        
        private async Task SetPreviewRotationAsync()
        {
            // Calculate which way and how far to rotate the preview
            int rotationDegrees = ConvertDisplayOrientationToDegrees(_displayOrientation);
        
            // Add rotation metadata to the preview stream to make sure the aspect ratio / dimensions match when rendering and getting preview frames
            var props = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);
            props.Properties.Add(RotationKey, rotationDegrees);
            await _mediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, props, null);
        }
        
        private static int ConvertDisplayOrientationToDegrees(DisplayOrientations orientation)
        {
            switch (orientation)
            {
                case DisplayOrientations.Portrait:
                    return 90;
                case DisplayOrientations.LandscapeFlipped:
                    return 180;
                case DisplayOrientations.PortraitFlipped:
                    return 270;
                case DisplayOrientations.Landscape:
                default:
                    return 0;
            }
        }
        

        仔细查看示例,了解如何获取所有详细信息。或者,要进行演练,您可以观看最近的 //build/ 会议中的camera session,其中包括一些相机示例的演练。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-01-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-10-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多