How to take a picture with correct rotation, aspect ratio in Windows Phone 8.1? (using MediaCapture)

Can any of you provide an actual working example of how to take and save a photo using the MediaCapture element . I tried to find the actual solution on MSDN , but none of these explanations or code actually describe the process in an easy way.

I need to take a photo and save it to my library (I need to show the correct view for this), however right now it is rotated 90 degrees and I cannot adjust it. I tried to set the rotation of the video preview and it works for the preview, however when I do this aspect ratio its all wrong and the saved image is not correct.

Examples from Channel 9 are kind of suck too. I just need a simple implementation ...

I am using a Runtime Application NOT a Windows Phone 8.1 Silverlight Application.

+3


source to share


3 answers


I had the same problem, SetRecordRotation doesn't work for me. I found a workaround - take photograph and rotate the image, it works great. I use a method like this:

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 the sample code from How to Take a Snapshot in Your Windows Phone 8.1 Runtime App by Marco Siccardi http://dotnet.dzone.com/articles/how-capture-photo-your-windows-0

+5


source


There are two samples on Microsoft's github page that are relevant to them, although they target Windows 10. The APIs should work with 8 / 8.1, however.

GetPreviewFrame : This sample does not block page rotation and applies rotation correction to the preview stream. It does not use SetPreviewRotation

it since this method is more resource intensive than using metadata. This sample does not take pictures (only preview frames)

UniversalCameraSample : It takes photos and supports portrait and landscape orientation. Here's 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());
}

      



FROM

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

      

Check out the sample to learn how to get the camera orientation in the first place (the call is made in the first snippet I posted).

Or, if you prefer video, you can watch a camera session from a recent // build / conference, which includes a little walkthrough of some camera samples.

+2


source


you can change aspect ratio for video preview and captured photo by setting in MediaCapture.VideoDeviceController.

Alternatively, you can set the video preview in vertical position using the following code.

 MediaCapture.SetPreviewRotation(VideoRotation.Clockwise90Degrees);

      

I answered similar questions in another post at the link below. Hope it helps.

fooobar.com/questions/2173938 / ...

0


source







All Articles