2013-03-14 137 views
10

我正在使用CaptureElement顯示我的Windows應用商店應用程序中的相機Feed。現在,我想在用戶點擊顯示屏時將照片捕獲爲流,我使用下面的代碼工作。不幸的是,返回的圖像只有640 x 360的分辨率,但相機(Surface RT)可以拍攝1280x800的圖像,我希望這樣做。使用MediaCapture從CaptureElement拍攝自定義分辨率的照片

我試着設置

 encodingProperties.Height = 800; 
     encodingProperties.Width = 1280; 

,但沒有奏效。那麼如何改變分辨率?

private async void captureElement_Tapped(object sender, TappedRoutedEventArgs e) 
    { 
     var encodingProperties = ImageEncodingProperties.CreateJpeg(); 
     //encodingProperties.Height = 800; 
     //encodingProperties.Width = 1280; 
     WriteableBitmap wbmp; 

     using (var imageStream = new InMemoryRandomAccessStream()) 
     { 
      await captureMgr.CapturePhotoToStreamAsync(encodingProperties, imageStream); 
      await imageStream.FlushAsync(); 
      imageStream.Seek(0); 
      wbmp = await new WriteableBitmap(1, 1).FromStream(imageStream); 
     } 

     capturedImage.Source = wbmp; 
    } 

回答

15

所以,我終於想通了如何通過這個來,也擺脫了可怕的「HRESULT:0xC00D36B4」的錯誤,這要部分歸功於代碼在這裏找到: http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/751b8d83-e646-4ce9-b019-f3c8599e18e0

我做了一些調整,這就是爲什麼我在這裏

MediaCapture mediaCapture; 
    DeviceInformationCollection devices; 

    protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); 
     this.mediaCapture = new MediaCapture(); 
     if (devices.Count() > 0) 
     { 
      await this.mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings { VideoDeviceId = devices.ElementAt(1).Id, PhotoCaptureSource = Windows.Media.Capture.PhotoCaptureSource.VideoPreview }); 
      SetResolution(); 
     } 
    } 


    //This is how you can set your resolution 
    public async void SetResolution() 
    { 
     System.Collections.Generic.IReadOnlyList<IMediaEncodingProperties> res; 
     res = this.mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview); 
     uint maxResolution = 0; 
     int indexMaxResolution = 0; 

     if (res.Count >= 1) 
     { 
      for (int i = 0; i < res.Count; i++) 
      { 
       VideoEncodingProperties vp = (VideoEncodingProperties)res[i]; 

       if (vp.Width > maxResolution) 
       { 
        indexMaxResolution = i; 
        maxResolution = vp.Width; 
        Debug.WriteLine("Resolution: " + vp.Width); 
       } 
      } 
      await this.mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, res[indexMaxResolution]); 
     } 
    } 

轉貼我的代碼雖然拍照,請確保您始終與.VideoPreview,不.Photo工作!

+1

謝謝,我一直在尋找這個,很棒! – Chin 2013-10-24 04:33:49

+0

此表面pro 2開始滯後。 – Yawar 2015-10-14 13:16:17

+0

此方法不適用於拍照 – gayan1991 2016-05-25 02:04:05

相關問題