c# - 如何更改 Windows Phone 8,8.1 中的默认视频捕获分辨率

标签 c# vb.net windows-phone-8 windows-phone

我想构建一个应用程序,让用户可以选择视频录制的分辨率大小(例如全高清-1920x1080 或 vga-640x480 等)

我正在使用下面的代码,但是当我在 720p 模拟器上运行它时,它显示了其他部分的消息,即相机不支持此功能。 (当我将值 800,450 更改为 640,480 时,相机开始正常工作)

try
           {
              //string deviceName = DeviceStatus.DeviceName;
               //var deviceName = DeviceStatus.DeviceName;
               //if (deviceName.Contains("RM-885"))
               //{
                   Windows.Foundation.Size initialResolution = new Windows.Foundation.Size(800, 450);
                   Windows.Foundation.Size previewResolution = new Windows.Foundation.Size(800, 450);
                   Windows.Foundation.Size captureResolution = new Windows.Foundation.Size(800, 450);
                   if (AudioVideoCaptureDevice.AvailableSensorLocations.Contains(CameraSensorLocation.Back))
                   {
                       pops = await AudioVideoCaptureDevice.OpenAsync(CameraSensorLocation.Back, initialResolution);
                       await pops.SetPreviewResolutionAsync(previewResolution);
                       await pops.SetCaptureResolutionAsync(captureResolution);    


                   }
               }
          // }

           catch (Exception p) { Debug.WriteLine(p.Message); }

            try
            {
                if(pops != null)
                {


                    // create the videobrush for the viewfinder
                    videoRecordBrush = new VideoBrush();
                    videoRecordBrush.SetSource(pops);
                    // display the viewfinder image 
                    viewfinderRectangle.Fill = videoRecordBrush;
                    //shows available resolution message 
                    MessageBox.Show("statrt recording ");
                    MessageBox.Show(pops.PreviewResolution.ToString());
                    MessageBox.Show(pops.CaptureResolution.ToString());


                }
                else
                {
                    MessageBox.Show("camera not support this ");
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("exception" + ex);
            }
        }

这是在视频模式下更改分辨率的正确代码吗?或者还有其他方法吗?

最佳答案

在 Windows Phone 8.1 中,您可以通过更改 MediaCapture 来更改视频预览拍摄的照片宽高比。 VideoDeviceController 设置。

视频预览是在实际拍摄照片之前您可以从屏幕上看到的相机 View 。视频预览和拍摄的照片的宽高比必须相同,才能避免拍摄的照片中出现奇怪的线条和黑条。换句话说,就是保证拍摄出来的照片和你在屏幕上看到的预览一模一样。

首先,您可以检查 Windows Phone 8.1 中所有可用的分辨率。在下面的代码中,我演示了如何检查视频预览和拍摄的照片可用的分辨率。返回的高度和宽度是您手机可用的分辨率,例如 i=0、height=1080、width=1920 (1920x1080)。请在高度和宽度线上调试以检查不同的分辨率。

MediaCapture mediacapture = new MediaCapture();
await mediacapture.InitializeAsync(new MediaCaptureInitializationSettings{});
var previewResolution = mediacapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview);
var photoResolution = mediacapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo);

        VideoEncodingProperties allResolutionsAvailable;
        uint height, width;
  //use debugger at the following line to check height & width for video preview resolution
        for (int i = 0; i < previewResolution.Count; i++)
        {
            allResolutionsAvailable = previewResolution[i] as VideoEncodingProperties;
            height = allResolutionsAvailable.Height;
            width = allResolutionsAvailable.Width;
        }
  //use debugger at the following line to check height & width for captured photo resolution
        for (int i = 0; i < photoResolution.Count; i++)
        {
            allResolutionsAvailable = photoResolution[i] as VideoEncodingProperties;
            height = allResolutionsAvailable.Height;
            width = allResolutionsAvailable.Width;
        }

检查上面的高度宽度参数中的所有可用分辨率后,您可以使用 .ElementAt选择您想要的特定分辨率 (int) 方法,例如.ElementAt(0)

var selectedPreviewResolution = mediacapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview).ElementAt(1);
var selectedPhotoResolution = mediacapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo).ElementAt(1);

await mediacapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, selectedPreviewResolution);
await mediacapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.Photo, selectedPhotoResolution);

//in .xaml <CaptureElement Name="viewfinder"/>
viewfinder.Source = mediacapture;
//to set video preview upright
mediacapture.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
await mediacapture.StartPreviewAsync();

以相同的宽高比分别为视频预览和拍摄的照片设置 .ElementAt(i)。 以下是诺基亚 Lumia 1520 可用的分辨率。

视频预览

  • i.....高度*宽度..长宽比
  • 0.....1080*1920....16:9
  • 1......720*1280.....16:9
  • 2......450*800.......16:9
  • 3....1080*1440......4:3
  • 4......768*1024......4:3
  • 5......480*640........4:3

拍摄的照片

  • 我...高度*宽度..纵横比
  • 0....3024*4992.......4:3
  • 1.....1936*2592...162:121
  • 2.....1536*2048.......4:3
  • 3......480*640......4:3
  • 4.....3024*5376.....16:9
  • 5.....1728*3072.....16:9
  • 6.....1456*2592...162:91

最后,如果您想使用视频预览和拍摄的照片可用的最大分辨率,您可以使用以下代码。

//maximum resolution for 4:3 aspect ratio 
 var maxPhotoResolution = mediacapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo).Aggregate((i1, i2) => (i1 as VideoEncodingProperties).Height > (i2 as VideoEncodingProperties).Height ? i1 : i2);
 var maxPreviewResolution = mediacapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview).Aggregate((i1, i2) => (i1 as VideoEncodingProperties).Height > (i2 as VideoEncodingProperties).Height ? i1 : i2);

//maximum resolution for 16:9 aspect ratio 
 var maxPhotoResolution = mediacapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo).Aggregate((i1, i2) => (i1 as VideoEncodingProperties).Width > (i2 as VideoEncodingProperties).Width ? i1 : i2);
 var maxPreviewResolution = mediacapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview).Aggregate((i1, i2) => (i1 as VideoEncodingProperties).Width > (i2 as VideoEncodingProperties).Width ? i1 : i2);

await mediacapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.Photo, maxPhotoResolution);
await mediacapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, maxPreviewResolution);

关于c# - 如何更改 Windows Phone 8,8.1 中的默认视频捕获分辨率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24014566/

相关文章:

c# - 一款 C# 游戏统治一切(iOS、Android、WP7、W7、Mac OS X 和 XBOX)

vb.net - 有人可以用简单的术语解释这个 Miller-Rabin Primality 测试伪代码吗?

.net - 对条件属性使用编译器指令

c# - windows phone,实例化一个页面并展示

c# - Windows Phone 8 加速度计事件

c# - 使用 ThreadPool.QueueUserWorkItem 和使用 System.Thread 有什么区别?

c# - 用c#替换图像中的颜色

c# - Windows 手机 : How to stay in the Current page rather than navigating to another page?

c# - 什么是 '=>' ? (C#语法题)

c# - .NET 中的 TextFieldParser 等效项?