了解屏幕方向+ Side Windows Store App

时间:2014-05-28 15:46:29

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

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

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

感谢。

3 个答案:

答案 0 :(得分:1)

使用Windows.Graphics.Display.DisplayInformation类(通过DisplayInformation.getForCurrentView()获取)及其currentOrientation属性中的值。这标识了设备相对于其原始方向可能的四个旋转象限之一:landscape,portrait,landscapeFlipped和portraitFlipped。还有一个orientationchanged事件可用于检测更改(请参阅Display orientation sample的方案3以了解此事件的使用情况。)

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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;
    }
}

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

相关问题