Reputation: 1856
I am creating a camera app using MediaCapture. I am trying to adapt to screen rotation and thus I have created the following method:
private void Current_SizeChanged(object sender, Windows.UI.Core.WindowSizeChangedEventArgs e)
{
width = Window.Current.Bounds.Width;
height = Window.Current.Bounds.Height;
//captureManager.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
//if (ApplicationView.GetForCurrentView().Orientation.ToString() == "Portrait") capturePreview.Stretch = Stretch.Fill;
capturePreview.Width = width;
capturePreview.Height = height;
imagePreview.Width = width;
imagePreview.Height = height;
//if (ApplicationView.GetForCurrentView().Orientation.ToString() == "Portrait") capturePreview.Stretch = Stretch.Fill;
//else capturePreview.Stretch = Stretch.Uniform;
}
When I open my app the camera fills the whole page but when I flip the camera the capturePreview only takes half of the page, It doesn't matter if I start the app in portrait or horizontal mode, the other mode won't work but the first mode will also if flipped in to.
Another question is about the actual capturePreview layout, I found that if I keep the screen at horizontal layout the camera works great but if the device is in portrait mode I cant fill the whole page without stretching the photo, is there a way to keep only one element on the screen in a certain rotation (capturePreview) I tried rotating it with a rotation tranform but that also affects the actual place of the element. Thank you
Upvotes: 2
Views: 1297
Reputation: 153
It's better to let the capturepreview follow the orientation of the camera. If you rotate the camera, the capturepreview should rotate as well.
private void Current_SizeChanged(object sender, Windows.UI.Xaml.SizeChangedEventArgs e)
{
try
{
string currentorientation = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().CurrentOrientation.ToString();
switch (currentorientation)
{
case "Landscape":
captureManager.SetPreviewRotation(VideoRotation.None);
break;
case "Portrait":
captureManager.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
break;
case "LandscapeFlipped":
captureManager.SetPreviewRotation(VideoRotation.Clockwise180Degrees);
break;
case "PortraitFlipped":
captureManager.SetPreviewRotation(VideoRotation.Clockwise270Degrees);
break;
default:
captureManager.SetPreviewRotation(VideoRotation.None);
break;
}
capturePreview.Width = Math.Floor(Window.Current.Bounds.Width);
capturePreview.Height = Math.Floor(Window.Current.Bounds.Height);
}
catch {}
}
[edit]: Btw, did you add this to your capturepreview?
<CaptureElement Name="capturePreview" Stretch="UniformToFill"/>
[edit 2, as response to askers comment]: Use this to rotate the bitmap and then save it. (took parts from your post about "the autofocus)
async private void CapturePhoto_Click(object sender, RoutedEventArgs e)
{
ImageEncodingProperties imgFormat = ImageEncodingProperties.CreateJpeg();
InMemoryRandomAccessStream imageStream = new InMemoryRandomAccessStream();
// take photo. Instead of saving to file, save it to stream so it can be manipulated.
await captureManager.CapturePhotoToStreamAsync(imgFormat, imageStream);
BitmapDecoder dec = await BitmapDecoder.CreateAsync(imageStream);
BitmapEncoder enc = await BitmapEncoder.CreateForTranscodingAsync(imageStream, dec);
string currentorientation = DisplayInformation.GetForCurrentView().CurrentOrientation.ToString();
switch (currentorientation)
{
case "Landscape":
enc.BitmapTransform.Rotation = BitmapRotation.None;
break;
case "Portrait":
enc.BitmapTransform.Rotation = BitmapRotation.Clockwise90Degrees;
break;
case "LandscapeFlipped":
enc.BitmapTransform.Rotation = BitmapRotation.Clockwise180Degrees;
break;
case "PortraitFlipped":
enc.BitmapTransform.Rotation = BitmapRotation.Clockwise270Degrees;
break;
default:
enc.BitmapTransform.Rotation = BitmapRotation.None;
break;
}
await enc.FlushAsync();
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(
"Photo.jpg",
CreationCollisionOption.ReplaceExisting);
var filestream = await file.OpenAsync(FileAccessMode.ReadWrite);
await RandomAccessStream.CopyAsync(imageStream, filestream);
// Get photo as a BitmapImage
BitmapImage bmpImage = new BitmapImage(new Uri(file.Path));
// imagePreivew is a <Image> object defined in XAML
imagePreview.Source = bmpImage;
}
Upvotes: 4