MediaCapture Windows Phone和Windows 8.1 App处理适用于所有方案的方向

时间:2016-04-07 20:26:37

标签: windows-phone-8.1 windows-store-apps windows-8.1

我已经通过网络尝试了所有解决方案,但其中没有一个解决了所有旋转和方向案例。

是否有完整的,更好的解决方案或文档让我能够很好地使用mediacapture对象?

1 个答案:

答案 0 :(得分:0)

如果您查看CameraStarterKit中的Microsoft GitHub repository样本,您就可以更好地了解如何处理相机的旋转。它针对Windows 10,但很多代码应该可以移植到8.1。

主要是,它归结为:

    // Receive notifications about rotation of the device and UI and apply any necessary rotation to the preview stream and UI controls       
    private readonly DisplayInformation _displayInformation = DisplayInformation.GetForCurrentView();
    private readonly SimpleOrientationSensor _orientationSensor = SimpleOrientationSensor.GetDefault();
    private SimpleOrientation _deviceOrientation = SimpleOrientation.NotRotated;
    private DisplayOrientations _displayOrientation = DisplayOrientations.Portrait;

    // Rotation metadata to apply to the preview stream and recorded videos (MF_MT_VIDEO_ROTATION)
    // Reference: http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh868174.aspx
    private static readonly Guid RotationKey = new Guid("C380465D-2271-428C-9B83-ECEA3B4A85C1");

    /// <summary>
    /// Gets the current orientation of the UI in relation to the device (when AutoRotationPreferences cannot be honored) and applies a corrective rotation to the preview
    /// </summary>
    private async Task SetPreviewRotationAsync()
    {
        // Only need to update the orientation if the camera is mounted on the device
        if (_externalCamera) return;

        // Calculate which way and how far to rotate the preview
        int rotationDegrees = ConvertDisplayOrientationToDegrees(_displayOrientation);

        // The rotation direction needs to be inverted if the preview is being mirrored
        if (_mirroringPreview)
        {
            rotationDegrees = (360 - rotationDegrees) % 360;
        }

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

    /// <summary>
    /// Registers event handlers for hardware buttons and orientation sensors, and performs an initial update of the UI rotation
    /// </summary>
    private void RegisterEventHandlers()
    {
        // If there is an orientation sensor present on the device, register for notifications
        if (_orientationSensor != null)
        {
            _orientationSensor.OrientationChanged += OrientationSensor_OrientationChanged;

            // Update orientation of buttons with the current orientation
            UpdateButtonOrientation();
        }

        _displayInformation.OrientationChanged += DisplayInformation_OrientationChanged;
    }

但这只是代码的一部分。您应该查看the full file(如果不是完整示例),以便更好地了解其工作原理。