在 Windows Phone 8.1 中,您可以更改纵横比两者的视频预览 and 拍摄的照片通过改变MediaCapture.VideoDeviceController环境。
视频预览是在实际拍摄照片之前您可以从屏幕上看到的相机视图。这是至关重要的纵横比视频预览和拍摄的照片相同避免奇怪的线条和黑条在拍摄的照片中。换句话说,就是保证拍摄出来的照片和你在屏幕上看到的预览一模一样。
首先,您可以检查 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;
}
检查了所有可用的分辨率后height and 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 可用的分辨率。
视频预览
- 我......高度*宽度..纵横比
- 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);