2013-05-25 85 views
0

該代碼被寫入以獲得最佳(最大)分辨率的凸輪,然後把照片這一點,但它拍照只爲VGA,而凸輪具有800萬像素的XAML應用相機的最高分辨率解析度。請糾正我這個代碼有什麼問題。感謝無法取得宏碁平板電腦

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

    } 

回答

0

只是走一個猜測在這裏 - 而不是使用VideoPreview讓你的決議,你應該嘗試使用Photo

//res = mediaCaptureMgr.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview); 
res = mediaCaptureMgr.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo); 
+0

我嘗試這也是,但不工作,即使cameracapturecui正在給予完美的解決方案。 – chetan