使相机应用适应屏幕旋转 - windows phone RT

Adapting a camera app to the screen rotation - windows phone RT

我正在使用 MediaCapture 创建相机应用程序。我试图适应屏幕旋转,因此我创建了以下方法:

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

当我打开我的应用程序时,相机会填满整个页面,但当我翻转相机时,capturePreview 只占页面的一半,无论我以纵向模式还是横向模式启动应用程序,其他模式都没有关系将不起作用,但如果切换到第一种模式,也会起作用。

另一个问题是关于实际的 capturePreview 布局,我发现如果我将屏幕保持在水平布局,相机效果很好,但如果设备处于纵向模式,我无法在不拉伸照片的情况下填满整个页面,是吗?一种以特定旋转(capturePreview)在屏幕上仅保留一个元素的方法我尝试使用旋转变换旋转它,但这也会影响元素的实际位置。 谢谢

最好让捕捉预览跟随相机的方向。 如果旋转相机,capturepreview 也应该旋转。

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]:顺便说一句,你把这个添加到你的 capturepreview 了吗?

<CaptureElement Name="capturePreview" Stretch="UniformToFill"/>

[编辑 2,作为对提问者评论的回应]: 使用它来旋转位图,然后保存它。 (摘自您 post 关于“自动对焦”的内容)

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