c# - 使相机应用程序适应屏幕旋转 - Windows Phone RT

标签 c# camera windows-runtime windows-phone-8.1

我正在使用 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 布局,我发现如果我将屏幕保持水平布局,相机效果很好,但如果设备处于纵向模式,我无法在不拉伸(stretch)照片的情况下填充整个页面,有没有办法为了在屏幕上仅保留一个元素以某种旋转(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 {}
}

[编辑]:顺便说一句,您是否将其添加到您的 capturepreview 中?

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

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

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

关于c# - 使相机应用程序适应屏幕旋转 - Windows Phone RT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31430584/

相关文章:

c# - System.Text.Json 不会驼峰化动态属性的属性

c# - 使用 MVVM LIGHT (WPF) 在 UserControl 中导航

c# - 如何防止 "regex injection"?

android - OpenGL 渲染器 : Bitmap too large to be uploaded into a texture in android titanium

.net - Windows 应用商店应用程序在关闭优化的情况下未能通过认证?

c# - 为什么我不能将 Debug.Assert() 与接受动态并返回 bool 的方法一起使用?

ios - 保存我在我的应用程序中录制的视频

ios - 来自相机的原始图像数据,如 "645 PRO"

c# - .NETCore(Windows 8 框架)的 `GetCustomAttributes` 的等效方法是什么?

javascript - 重载属性有什么用?