简体   繁体   中英

Knowing screen orientation + Side Windows Store App

I have created a camera app using the MediaCapture API and trying to get the preview to be display relative to how the user is holding the device. For example the app assumes that the user is holding the device in portrait mode and displays the feed as such but when the user rotates the device by 90 degrees left or right how could I tell if the user turned the device clock wise or counter clockwise to display the feed accordingly.

I know that I can get the screen orientation as landscape or portrait but that does not tell me by how much should I rotate the feed.

Thanks.

Use the value from the Windows.Graphics.Display.DisplayInformation class (obtained via DisplayInformation.getForCurrentView()) and its currentOrientation property. This identifies one of the four rotation quadrants that are possible for a device relative to its native orientation: landscape, portrait, landscapeFlipped, and portraitFlipped. There's also an orientationchanged event that you can use to detect changes (see scenario 3 of the Display orientation sample for usage of this event).

您可以使用SimpleOrientation传感器通过应用确定设备的方向,请在此处查看

There are two samples posted on the Microsoft github page that are relevant, although they target Windows 10. Still, the APIs should work on 8/8.1.

GetPreviewFrame : This sample will not lock the page rotation, and apply a corrective rotation to the preview stream. It does not use SetPreviewRotation , as that method is more resource-heavy than using the metadata approach. This sample doesn't capture photos (just preview frames).

UniversalCameraSample : This one does capture photos, and supports portrait and landscape orientations, although it tries to lock the page to Landscape (via AutoRotationPreferences ).

Here is how the first sample handles orientation changes:

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

Have a closer look at the sample to see how to get all the details. Or, to have a walkthrough, you can watch the camera session from the recent //build/ conference, which includes a little bit of a walkthrough through some camera samples.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM