Windows手机8.1相机预览黑色边缘旋转

时间:2016-06-07 07:01:39

标签: c# xaml camera windows-phone-8.1

我在Windows Phone 8.1上使用视频预览来拍照。没有旋转,预览没有黑色边缘。旋转90度后,捕捉元素上会出现条纹。

以下是我的截图和XAML,c#代码。

XAML

CaptureElement x:Name="capturePreview" Stretch="UniformToFill" Margin="27,158,10,10" Grid.ColumnSpan="2" />

C#

    public async void preview()
    {
        DeviceInformationCollection webcamList = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);

        DeviceInformation backWebcam = (from webcam in webcamList
                                        where webcam.EnclosureLocation != null
                                        && webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back
                                        select webcam).FirstOrDefault();
         newCapture = new MediaCapture();          
        await newCapture.InitializeAsync(new MediaCaptureInitializationSettings
        {
            VideoDeviceId = backWebcam.Id,
            AudioDeviceId = "",
            StreamingCaptureMode = StreamingCaptureMode.Video,
            PhotoCaptureSource = PhotoCaptureSource.VideoPreview
        });                 
        await newCapture.StartPreviewAsync();
        newCapture.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
    }

带旋转的图片

enter image description here

不轮换

enter image description here

1 个答案:

答案 0 :(得分:0)

请勿使用mediaCapture.SetPreviewRotation,因为它会在您的信息流中添加信箱。而是使用Camera Starter Kit UWP sampleMicrosoft GitHub repository的SetPreviewRotationAsync方法。如果你仔细看看它,你会更好地了解如何处理相机的旋转。

主要是,它归结为:

    // 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(int rotationDegrees)
    {
        // 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);
    }

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