了解屏幕方向+侧面Windows商店应用程序



我使用MediaCapture API创建了一个相机应用程序,并试图让预览显示相对于用户持有设备的方式。例如,应用程序假设用户以纵向模式持有设备并显示feed,但是当用户将设备向左或向右旋转90度时,我怎么知道用户是将设备按时钟方向转动还是逆时针方向转动以相应地显示feed。

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

谢谢。

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

您可以使用SimpleOrientation传感器与应用程序确定设备方向,查看这里

在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/会议中的摄像机会话,其中包括一些摄像机示例的一点点演练。

最新更新