Fiorila
Fiorila

Reputation: 65

CameraCaptureUI in Windows 8 application takes a mirrored photo

I coded my service thate takes photos in my windows 8 application. I used CameraCaptureUI API. It works but the result, the image taken is mirrored : the left is in right and vice-versa. I searched in the PhotoSettings of CameraCapture and searched in the internet, but nothing.

I was thinking maybe the solution would be to rotate the bitmapimage that i save from the stream of camera. This is what i've tried till now and it's not working :/

    public async Task<IPhoto> TakePhotoAsync(string filename)
    {   

        var dialog = new CameraCaptureUI();          
        dialog.PhotoSettings.CroppedAspectRatio = new Size(4, 3);
        dialog.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Png;
        StorageFile file = await dialog.CaptureFileAsync( CameraCaptureUIMode.Photo );
        if (file != null)
        {

            var stream = await file.OpenAsync(FileAccessMode.Read);
            //create decoder and encoder
            BitmapDecoder dec = await BitmapDecoder.CreateAsync(stream);
            BitmapEncoder enc = await BitmapEncoder.CreateForTranscodingAsync(stream, dec);
            enc.BitmapTransform.Rotation = BitmapRotation.Clockwise180Degrees;
            await enc.FlushAsync();
            var bitmap = new BitmapImage();
            bitmap.SetSource(stream);
            return new WPhotos(file);
        }
        return null;                   
    }

Thank you in advance for your time and help :)

Upvotes: 2

Views: 450

Answers (2)

Ben Gladman
Ben Gladman

Reputation: 71

A different solution to normalise the orientation on a photo captured by CameraCaptureUI is to decode it to a SoftwareBitmap and then re-encode it:

var captureUI = new CameraCaptureUI();
captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
captureUI.PhotoSettings.MaxResolution = CameraCaptureUIMaxPhotoResolution.SmallVga;

var photo = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);

if (photo == null) { return false; }

var photoprops = await photo.Properties.GetImagePropertiesAsync();

if (photoprops.Orientation != PhotoOrientation.Normal)
{
    using (var readStream = await photo.OpenAsync(FileAccessMode.Read))
    {
        var decoder = await BitmapDecoder.CreateAsync(readStream);

        using (var sb = await decoder.GetSoftwareBitmapAsync())
        using (var memStream = new Windows.Storage.Streams.InMemoryRandomAccessStream())
        {
            BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, memStream);
            encoder.SetSoftwareBitmap(sb);
            encoder.IsThumbnailGenerated = false;
            await encoder.FlushAsync();

            // photo data with normalised orientation available in memStream
        }
    }
}

Upvotes: 1

djerinic
djerinic

Reputation: 36

If you want to flip the image that you saved from the stream try this:

How to Verticaly Flip an BitmapImage

Basicly you should use ScaleTransform instead of Rotation.

Upvotes: 1

Related Questions