如何在Windows Phone 8.1中使用正确的旋转,宽高比拍摄照片? (使用MediaCapture)

时间:2014-10-28 23:53:04

标签: c# camera windows-store-apps windows-phone-8.1 photo

您是否可以使用 MediaCapture 元素提供有关如何拍摄和保存照片的实际工作示例。我已经尝试在 MSDN 中寻找实际的解决方案,但这些解释或代码都没有以简单的方式实际描述过程。

我需要拍照并将其保存到我的库中(我需要为此显示正确的预览),但是现在它旋转了90度而我无法调整它。我已经尝试设置视频预览的旋转,它适用于预览,但是当我这样做时,宽高比全部错误,保存的图像不正确。

第9频道的例子也很糟糕。我只需要一个简单的实现......

我使用的是运行时应用程序,而不是Windows Phone 8.1的Silverlight应用程序。

3 个答案:

答案 0 :(得分:5)

我遇到了同样的问题,SetRecordRotation并不适合我。我找到了解决方法 - 拍照并旋转图像,效果很好。我使用这样的方法:

private async void CapturePhoto()
    {
        string photoPath = string.Empty;
        ImageEncodingProperties format = ImageEncodingProperties.CreateJpeg();

        using (var imageStream = new InMemoryRandomAccessStream())
        {
            await MediaCapture.CapturePhotoToStreamAsync(format, imageStream);

            BitmapDecoder dec = await BitmapDecoder.CreateAsync(imageStream);
            BitmapEncoder enc = await BitmapEncoder.CreateForTranscodingAsync(imageStream, dec);

            enc.BitmapTransform.Rotation = BitmapRotation.Clockwise90Degrees;

            await enc.FlushAsync();

            StorageFolder folder = ApplicationData.Current.LocalFolder;
            StorageFile capturefile = await folder.CreateFileAsync("photo.jpg", CreationCollisionOption.GenerateUniqueName);
            photoPath = capturefile.Name;

            using (var fileStream = await capturefile.OpenAsync(FileAccessMode.ReadWrite))
            {
                try
                {  
                    await RandomAccessStream.CopyAsync(imageStream, fileStream);
                }
                catch {}
            }
        } 
    }

我修改了Marco Siccardi撰写的文章如何在Windows Phone 8.1运行时应用中捕获照片中的代码示例 http://dotnet.dzone.com/articles/how-capture-photo-your-windows-0

答案 1 :(得分:2)

Microsoft github页面上发布了两个相关的样本,尽管它们的目标是Windows 10.但是,API应该适用于8 / 8.1。

GetPreviewFrame:此示例不会锁定页面旋转,并将校正旋转应用于预览流。它不使用SetPreviewRotation,因为该方法比使用元数据方法更耗费资源。此示例不会捕获照片(仅预览帧)

UniversalCameraSample:此照片会捕捉照片,并支持纵向和横向方向。以下是相关部分:

var stream = new InMemoryRandomAccessStream();

try
{
    await _mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), stream);

    var photoOrientation = ConvertOrientationToPhotoOrientation(GetCameraOrientation());

    await ReencodeAndSavePhotoAsync(stream, photoOrientation);
}
catch (Exception ex)
{
    Debug.WriteLine("Exception when taking a photo: {0}", ex.ToString());
}

使用:

    private static async Task ReencodeAndSavePhotoAsync(IRandomAccessStream stream, PhotoOrientation photoOrientation)
    {
        using (var inputStream = stream)
        {
            var decoder = await BitmapDecoder.CreateAsync(inputStream);

            var file = await KnownFolders.PicturesLibrary.CreateFileAsync("SimplePhoto.jpeg", CreationCollisionOption.GenerateUniqueName);

            using (var outputStream = await file.OpenAsync(FileAccessMode.ReadWrite))
            {
                var encoder = await BitmapEncoder.CreateForTranscodingAsync(outputStream, decoder);

                var properties = new BitmapPropertySet { { "System.Photo.Orientation", new BitmapTypedValue(photoOrientation, PropertyType.UInt16) } };

                await encoder.BitmapProperties.SetPropertiesAsync(properties);
                await encoder.FlushAsync();
            }
        }
    }

仔细查看示例,了解如何首先获取相机的方向(在我发布的第一个片段中调用它)。

或者,如果您更喜欢视频,则可以观看最近//版本/会议中的camera session,其中包括一些相机样本的演练。

答案 2 :(得分:0)

您可以更改视频预览的宽高比&通过在 MediaCapture.VideoDeviceController 中设置来捕获照片。

此外,您可以使用以下代码直立设置视频预览。

 MediaCapture.SetPreviewRotation(VideoRotation.Clockwise90Degrees);

我在下面链接的另一篇文章中回答了类似的问题。希望它有所帮助。

https://stackoverflow.com/a/29875992/4672579