簡體   English   中英

Windows Phone 8.1:如何更改前置攝像頭的旋轉角度?

[英]Windows Phone 8.1: how to change front camera rotation?

var cameraID = await GetCameraID(Windows.Devices.Enumeration.Panel.Front);

mediaCapture = new MediaCapture();
await mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings
{
    StreamingCaptureMode = StreamingCaptureMode.Video,
    PhotoCaptureSource = PhotoCaptureSource.VideoPreview,
    AudioDeviceId = string.Empty,
    VideoDeviceId = cameraID.Id
});

當我設定

capturePreview.Source = mediaCapture;

預覽是顛倒的。

如果我設定方向

mediaCapture.SetPreviewRotation(VideoRotation.Clockwise180Degrees);

我看到了鏡像。 例如,我可以看到自己的左手在右側。

如果我嘗試

mediaCapture.SetPreviewMirroring(true);

程序崩潰。 我需要扭轉這些不良行為。

如果查看Microsoft GitHub存儲庫中的CameraStarterKit示例,您將獲得有關如何處理攝像頭旋轉的更好的主意。

主要是歸結為:

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

但這只是代碼的一部分。 您應該查看完整文件 (如果不是完整示例),以更好地了解其工作方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM