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

时间:2014-06-03 11:56:15

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

我想构建一个应用程序,让用户可以选择视频录制的分辨率大小(例如全HD-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);
            }
        }

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

2 个答案:

答案 0 :(得分:3)

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

视频预览是在您实际拍摄照片之前可以从屏幕上看到的相机视图。视频预览和拍摄的照片的宽高比必须与避免奇怪的线条相同。拍摄照片中的黑条。换句话说,它是为了确保拍摄的照片与您在屏幕上看到的预览完全相同。

首先,您可以检查Windows Phone 8.1中的所有可用分辨率。在下面的代码中,我将演示如何检查可用的分辨率以进行视频预览&拍摄的照片。返回的高度和宽度是手机可用的分辨率,例如i = 0,高度= 1080,宽度= 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;
        }

检查上面 height 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)&amp;以相同的宽高比分别拍摄照片。 以下是适用于诺基亚Lumia 1520的分辨率。

视频预览

  • i ..... height * width..aspect ratio
  • 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

拍摄的照片

  • i .... height * width..aspect ratio
  • 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);

答案 1 :(得分:1)

如果您想要更好地控制录制的视频,则应使用新的Windows Phone 8 API:AudioVideoCaptureDevice,其中包含SetCaptureResolutionAsync方法。