简体   繁体   English

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

[英]How do i take a photo with the correct rotation, aspect ratio in Windows Phone 8.1? (using MediaCapture)

Can any of you provide an actual working sample of how to take and save a photo using the MediaCapture element. 您是否可以使用MediaCapture元素提供有关如何拍摄和保存照片的实际工作示例。 I've tried looking for an actual solution in MSDN but none of those explanations or code actually describe the process in a simple way. 我曾尝试在MSDN中寻找实际的解决方案,但这些解释或代码都没有以简单的方式描述过程。

I need to take a picture and save it to my library (i need to show the correct preview for this), however right now it is rotated 90 degrees and i can't adjust it. 我需要拍照并将其保存到我的库(我需要为此显示正确的预览),但是现在它旋转了90度而我无法调整它。 I've tried setting the rotation of the video preview and it works for the preview however when i do this the aspect ratio its all wrong and the saved image its not correct. 我已经尝试设置视频预览的旋转,它适用于预览,但是当我这样做时,宽高比全部错误,保存的图像不正确。

The examples from channel 9 kind of suck too. 第9频道的例子也很糟糕。 I just need a simple implementation... 我只需要一个简单的实现......

Im using a Runtime app NOT a silverlight app for Windows Phone 8.1. 我使用运行时应用程序而不是Windows Phone 8.1的Silverlight应用程序。

I have had the same issue, SetRecordRotation doesn't work for me. 我遇到了同样的问题,SetRecordRotation对我不起作用。 I found workaround - take photo and rotate an image, it works great. 我找到了解决方法 - 拍照并旋转图像,效果很好。 I use method like that: 我使用这样的方法:

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 {}
            }
        } 
    }

I modified sample of code from article How to capture a photo in your Windows Phone 8.1 Runtime app by Marco Siccardi http://dotnet.dzone.com/articles/how-capture-photo-your-windows-0 我修改了文章如何在您的Windows Phone 8.1运行时应用程序中捕获照片的代码示例,作者:Marco Siccardi http://dotnet.dzone.com/articles/how-capture-photo-your-windows-0

There are two samples posted on the Microsoft github page that are relevant, although they target Windows 10. Still, the APIs should work on 8/8.1. Microsoft github页面上发布了两个相关的样本,尽管它们针对Windows 10.但是,API应该适用于8 / 8.1。

GetPreviewFrame : This sample will not lock the page rotation, and apply a corrective rotation to the preview stream. GetPreviewFrame :此示例不会锁定页面旋转,并将校正旋转应用于预览流。 It does not use SetPreviewRotation , as that method is more resource-heavy than using the metadata approach. 它不使用SetPreviewRotation ,因为该方法比使用元数据方法更耗费资源。 This sample doesn't capture photos (just preview frames) 此示例不捕获照片(仅预览帧)

UniversalCameraSample : This one does capture photos, and supports portrait and landscape orientations. UniversalCameraSample :这张照片可以拍摄照片,并支持纵向和横向方向。 Here is the relevant part: 以下是相关部分:

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

With: 附:

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

Have a closer look at the sample to see how to get the orientation of the camera in the first place (a call to it is being made in the first snippet I posted). 仔细查看示例,了解如何首先获取摄像头的方向(在我发布的第一个片段中调用它)。

Or, if you prefer a video, you can watch the camera session from the recent //build/ conference, which includes a little bit of a walkthrough through some camera samples. 或者,如果您更喜欢视频,您可以观看最近//构建/会议中的相机会话 ,其中包括一些相机样本的演练。

you can change the aspect ratio for your video preview & captured photo by setting in the MediaCapture.VideoDeviceController . 您可以通过在MediaCapture.VideoDeviceController中进行设置来更改视频预览和捕获的照片的宽高比

Also, you can set your video preview upright by using the following code. 此外,您可以使用以下代码直立设置视频预览。

 MediaCapture.SetPreviewRotation(VideoRotation.Clockwise90Degrees);

I have answered a similar questions in the another post in the link below. 我在下面链接的另一篇文章中回答了类似的问题。 Hope it helps. 希望能帮助到你。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用现有的相机应用程序(不是使用 MediaCapture 创建一个)并在 windows phone 8.1(WinRT) 中拍照? - How to use existing camera app(not create one using MediaCapture) and take picture in windows phone 8.1(WinRT)? 如何在 Windows 手机/桌面上使用相机拍摄照片/照片 - How do I take a picture/photo using a camera on windows phone/desktop 如何在Windows Phone 8.1中使用MediaCapture设置最大分辨率? - How to set maximum resolution with MediaCapture in windows phone 8.1? Windows Phone 8.1 MediaCapture会冻结手机 - Windows Phone 8.1 MediaCapture freezes the phone 如何在MediaCapture Windows Phone 8.1 RT上更改Camera的默认设置? - How to change the default settings of Camera on MediaCapture Windows Phone 8.1 RT? 处理MediaCapture挂起/恢复Windows Phone 8.1 - Handling MediaCapture Suspend/Resume Windows Phone 8.1 在Windows Phone Silverlight 8.1上使用MediaCapture打开手电筒 - Turn on torch with MediaCapture on Windows Phone Silverlight 8.1 Windows Phone 8.1应用程序MediaCapture下暴露 - Windows Phone 8.1 app MediaCapture under exposed 使用MediaCapture时,长宽比/缩放级别更改是否存在问题? - Issue with aspect ratio / zoom level changing when using MediaCapture? Windows Phone 8.1 MediaCapture的FocusAsync不起作用 - Windows Phone 8.1 MediaCapture's FocusAsync does not work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM