使摄像头应用程序适应屏幕旋转——windows phone RT

本文关键字:windows phone RT 旋转 屏幕 摄像头 应用程序 | 更新日期: 2023-09-27 18:02:44

我正在使用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)我尝试旋转它与旋转变换,但这也影响了元素的实际位置。谢谢你

使摄像头应用程序适应屏幕旋转——windows phone RT

最好让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,作为对提问者评论的回应]:使用它来旋转位图,然后保存它。(摘自你的文章"自动对焦")

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