windows phone 摄像头得到图片是旋转90°

我上个随笔讲到,windows phone 拍出来的photo如果直接使用是反转了90°的。

研究了很久。。终于发现问题。其实。。这是使用习惯问题。。。

CameraCaptureUI 得到的photo 其实是 以第2图水平的方向为基准的。为什么我会这样说呢。。让我们看一下用模拟器拍摄的photo。注意到左边那些字没有。

再给一个水平的,可以看的更清楚。YUY2(640x480)

说白了。。其实水平才是别人老外认为的默认视角。。但是有人说。。这样子。。竖着拍的时候就拿到的却会横着显示。。很奇怪。。不怕不怕。。其实我们找到规律。。可以对得到的流进行处理。

下面是代码

public static async Task RotateCaptureImageByDisplayInformationAutoRotationPreferences(IRandomAccessStream inStream, IRandomAccessStream outStream)
        {

            BitmapDecoder decoder = await BitmapDecoder.CreateAsync(inStream);

            BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(outStream, decoder);

            var ort = DisplayInformation.GetForCurrentView().CurrentOrientation;
            Debug.WriteLine(ort);
            switch (ort)
            {
                //The same as Portrait
                case DisplayOrientations.None:
                    encoder.BitmapTransform.Rotation = BitmapRotation.Clockwise90Degrees;
                    break;
                //The default view for capture. 
                case DisplayOrientations.Landscape:
                    encoder.BitmapTransform.Rotation = BitmapRotation.None;
                    break;
                case DisplayOrientations.Portrait:
                    encoder.BitmapTransform.Rotation = BitmapRotation.Clockwise90Degrees;
                    break;
                case DisplayOrientations.LandscapeFlipped:
                    encoder.BitmapTransform.Rotation = BitmapRotation.Clockwise180Degrees;
                    break;
                case DisplayOrientations.PortraitFlipped:
                    encoder.BitmapTransform.Rotation = BitmapRotation.Clockwise270Degrees;
                    break;
                default:
                    break;
            }
            await encoder.FlushAsync();
        }

有了它。。妈妈再也不怕。。等到的图片是旋转90°的了。。哈哈哈

原文地址:https://www.cnblogs.com/FaDeKongJian/p/5158945.html