2014-10-06 59 views
1

我正在嘗試創建一個基本的攝像頭應用程序(針對WP的第一個應用程序)。當然,我想在拍攝前將相機數據預覽到屏幕上。在Windows Phone 8.1上訪問攝像頭預覽流

但唯一的樣品我看到從MSDN等網絡太舊(很多對象已被刪除,即正在更新的庫經常使得文章過時)

我在剛開始使用的一個問題預覽流。如果有知識的人可以在這個問題上給我一些幫助,那將不勝感激。

謝謝。

回答

1

我建議使用CaptureElement控制

在您的XAML補充一點:

<CaptureElement x:Name="Capture" 
       Stretch="UniformToFill" 
       FlowDirection="LeftToRight" /> 

我不知道,如果你想用正面或背面的攝像頭,所以我會爲這兩個顯示的代碼。

在您的代碼隱藏(或您的視圖模型,如果你使用MVVM)添加以下代碼:

// First need to find all webcams 
DeviceInformationCollection webcamList = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture) 

// Then I do a query to find the front webcam 
DeviceInformation frontWebcam = (from webcam in webcamList 
where webcam.EnclosureLocation != null 
&& webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front 
select webcam).FirstOrDefault(); 

// Same for the back webcam 
DeviceInformation backWebcam = (from webcam in webcamList 
where webcam.EnclosureLocation != null 
&& webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back 
select webcam).FirstOrDefault(); 

// Then you need to initialize your MediaCapture 
var newCapture = new MediaCapture(); 
await newCapture.InitializeAsync(new MediaCaptureInitializationSettings 
{ 
    // Choose the webcam you want (backWebcam or frontWebcam) 
    VideoDeviceId = backWebcam.Id, 
    AudioDeviceId = "", 
    StreamingCaptureMode = StreamingCaptureMode.Video, 
    PhotoCaptureSource = PhotoCaptureSource.VideoPreview 
}); 

// Set the source of the CaptureElement to your MediaCapture 
Capture.Source = newCapture; 

// Start the preview 
await newCapture.StartPreviewAsync(); 

這將顯示在您的CaptureElement控制所選擇的網絡攝像頭的流,並且可以在Windows手機8.1和Windows 8.1

+0

非常感謝!這工作完美 – Tokfrans 2014-10-06 22:52:39

+0

很高興它幫助! – 2014-10-06 22:53:07