2015-07-12 149 views
0

我試圖通過此API實現圖像捕獲,並且需要通過桌面應用程序實現。MediaCapture Windows 8桌面應用程序 - 無法啓動它

問題是,當我將圖像保存到一個文件(與CapturePhotoToStorageFileAsync)我得到一個黑暗的圖片(幾乎是黑色),或者我得到零大小的文件。

我的代碼是非常簡單的,但:

MediaCapture = new MediaCapture(); 
await mediaCapture.InitializeAsync(); 

Windows.Storage.StorageFile photoStorageFile; 
String PHOTO_FILE_NAME = "photo.jpg"; 
photoStorageFile = await Windows.Storage.KnownFolders.PicturesLibrary.CreateFileAsync(PHOTO_FILE_NAME, Windows.Storage.CreationCollisionOption.GenerateUniqueName); 
ImageEncodingProperties imageProperties = ImageEncodingProperties.CreateJpeg(); 
await mediaCapture.CapturePhotoToStorageFileAsync(imageProperties, photoStorageFile); 

我讀到它另一個線程: Captured photos are black when using MediaCapture on Windows Phone 8.1

的問題是,MediaCapture.StartPreviewAsync引發異常「意外的錯誤已請求的操作出錯」 ,我想這是MediaElement,我沒有(再次,桌面)的原因。

我也很高興找到工作的例子,當我搜索,我沒有找到實現的桌面應用程序MediaCapture任何真正的樣品,我看到一篇文章,但沒有附代碼: http://blogs.msdn.com/b/eternalcoding/archive/2013/10/29/how-to-use-specific-winrt-api-from-desktop-apps-capturing-a-photo-using-your-webcam-into-a-wpf-app.aspx

我還發現其他人也詢問了一個樣品,但沒有真正的答案...

我怎樣才能得到一個工作樣本?

回答

1

獲取MediaCapture的網絡攝像頭功能使用攝像頭的權限在桌面應用程序中預覽攝像頭視頻不是微不足道的。我不確定所有的代碼都在那裏,所以我在GitHub上放了一些代碼來嘗試它,它似乎工作正常。

https://github.com/mmaitre314/MediaCaptureWPF

警告:構建MediaCapture/WPF互操作所需的某些本機代碼(C++/CLI),因此該應用需要被構建爲x86或x64,但不是AnyCPU。

+0

謝謝Matthieu!我想知道是否有可能在C#中實現它? – Programmer

+0

到目前爲止,我還沒有找到一個C#方法。 WPF需要某種CaptureElement,它依賴於DirectX和Media Foundation。他們有一些C#投影(SharpDX等),所以也許有辦法將C++代碼翻譯成C#。 –

0

爲了讓您的商店的應用程序與相機交互實際上有兩種方式,一個簡單的和先進的之一:

  • 或者通過使用所提供的用戶界面,它採用的是常見的UI捕獲媒體,並節省您處理基本捕獲操作的痛苦,或通過自己與MediaCapture API進行交互來獲得對整個操作流程的完全控制。

,如果你只是想拍照或從 攝像頭的視頻,我建議你使用它主要利用CameraCaptureUI API第一種方法,但是如果你需要完全控制的拍攝操作,你需要一些行代碼,與MediaCapture的API

這裏使用CameraCaptureUI API一個簡單的例子互動:

讓的說我們有以下界面:

<StackPanel Orientation="Vertical"> 
     <Button Click="TakePicture_Click" Content="Capture Photo" HorizontalAlignment="Stretch"/> 
     <Border BorderBrush="White" BorderThickness="3"> 
      <Image x:Name="Picture" Height="700" VerticalAlignment="Center" HorizontalAlignment="Center" /> 
     </Border>    
    </StackPanel> 

這裏拍攝按鈕單擊處理程序:

private async void TakePicture_Click(object sender, RoutedEventArgs e) 
    { 
     var camera = new CameraCaptureUI(); 
     var image = await camera.CaptureFileAsync(CameraCaptureUIMode.Photo); 
     if (image != null) 
     { 
      var stream = await image.OpenAsync(FileAccessMode.Read); 
      var bitmap = new BitmapImage(); 
      bitmap.SetSource(stream); 
      Picture.Source = bitmap; 
     } 
     else 
     { 
      (new MessageDialog("Something went wrong")).ShowAsync();     
     } 
    } 

,你也需要提供您的應用程序通過檢查應用清單

enter image description here

+0

謝謝,但**桌面**應用程序不支持CameraCaptureUI。 – Programmer

+0

你是什麼意思桌面應用程序??,CameraCaptureUI在winrt/store應用程序中效果很好!!,如果yu想要我可以給你一個MediaCapture樣本? – Usama

+0

閱讀以下文章:http://www.samlogic.net/articles/windows-8-desktop-app-vs-store-app.htm。由於某些原因,我無法使用商店應用程序,所以我需要使用桌面應用程序(Winform/WPF/Console應用程序的新名稱)。我非常感謝你在答案中的努力,但這不是我可以使用的平臺。 – Programmer

相關問題