编写此代码是为了获得 cam 的最佳(最大)分辨率,然后为此拍照,但它只为 VGA 拍照,而 cam 的分辨率为 8 兆像素。请纠正我这段代码有什么问题。谢谢
<Page
x:Class="win8appbycrs.BlankPage2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:win8appbycrs"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Image x:Name="image" HorizontalAlignment="Left" Height="424" Margin="254,45,0,0" VerticalAlignment="Top" Width="619"/>
<Button Content="Set" HorizontalAlignment="Left" Margin="978,293,0,0" VerticalAlignment="Top" Click="Button_Click"/>
<CaptureElement x:Name="ce" HorizontalAlignment="Left" Height="268" Margin="97,490,0,0" VerticalAlignment="Top" Width="421" Stretch="Fill"/>
<ComboBox x:Name="ComboBox1" HorizontalAlignment="Left" Margin="659,474,0,0" VerticalAlignment="Top" Width="120"/>
<Button Content="Capture" HorizontalAlignment="Left" Margin="840,499,0,0" VerticalAlignment="Top" Click="Button_Click_1"/>
<Image x:Name="Img" HorizontalAlignment="Left" Height="225" Margin="254,133,0,0" VerticalAlignment="Top" Width="356"/>
</Grid>
C#代码:
public BlankPage2()
{
InitializeComponent();
mediaCaptureMgr = new MediaCapture();
StartPreview();
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
int max_res = 0;
int selected = 0;
string sCameraName;
var interfaces = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(Windows.Devices.Enumeration.DeviceClass.VideoCapture);
foreach (DeviceInformation deviceInterface in interfaces)
{
sCameraName = deviceInterface.Name.ToString();
MediaCaptureInitializationSettings captureInitSettings = new Windows.Media.Capture.MediaCaptureInitializationSettings();
captureInitSettings.VideoDeviceId = deviceInterface.Id;
captureInitSettings.PhotoCaptureSource = PhotoCaptureSource.VideoPreview;
//enumerate each camera for supported configurations
MediaCapture mediaCaptureMgr = new Windows.Media.Capture.MediaCapture();
await mediaCaptureMgr.InitializeAsync(captureInitSettings);
//System.Collections.Generic.IReadOnlyList<IMediaEncodingProperties> res = mediaCaptureMgr.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoRecord);
res = mediaCaptureMgr.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview);
//if no settings available, bail
if (res.Count < 1) return;
//list the number of setting combinations for the device
string sCount = sCameraName + ": " + res.Count.ToString();
// ComboBox1.Items.Add(sCount);
//list the different format settings
for (int i = 0; i < res.Count; i++)
{
VideoEncodingProperties vp = (VideoEncodingProperties)res[i];
if (vp.Width * vp.Height > max_res)
{
resolutionMax=vp;
max_res = (int)vp.Width;
selected = i;
}
}
}
setres(selected);
}
async void StartPreview()
{
try
{
await mediaCaptureMgr.InitializeAsync();
ce.Source = mediaCaptureMgr;
await mediaCaptureMgr.StartPreviewAsync();
}
catch (Exception ex)
{
mediaCaptureMgr = null;
GC.Collect();
}
}
private async void setres(int maxres)
{
await mediaCaptureMgr.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview , res[maxres]);
}
private async void Button_Click_1(object sender, RoutedEventArgs e)
{
var captureSettings = new ImageEncodingProperties();
captureSettings.Height = resolutionMax.Height;
captureSettings.Width = resolutionMax.Width;
captureSettings.Subtype = "Jpeg";
// Requires documentsLibrary capability
var folder = ApplicationData.Current.RoamingFolder;
var folder1 = KnownFolders.PicturesLibrary;
var file = await folder.CreateFileAsync(@"Captured.jpg", Windows.Storage.CreationCollisionOption.GenerateUniqueName);
await mediaCaptureMgr.CapturePhotoToStorageFileAsync(captureSettings, file);
Img.Source = new BitmapImage(new Uri(@file.Path));
await file.CopyAsync(folder1);
}