在樹莓派上能夠使用它官方標配的攝像頭,可是這個攝像頭彷佛不能被Windows IoT識別和使用。可是,能夠在樹莓派的USB口上插入任意型號的攝像頭,就能夠實現樹莓派的拍攝功能。web
關於攝像頭的尋找和拍攝,我將其封裝成一個類,以下:網絡
public class WebCamHelper { public MediaCapture mediaCapture; private bool initialized = false; /// <summary> /// 異步初始化網絡攝像頭 /// </summary> public async Task InitializeCameraAsync() { if (mediaCapture == null) { // 嘗試發現攝像頭 var cameraDevice = await FindCameraDevice(); if (cameraDevice == null) { // 沒有發現攝像頭 Debug.WriteLine("No camera found!"); initialized = false; return; } // Creates MediaCapture initialization settings with foudnd webcam device var settings = new MediaCaptureInitializationSettings { VideoDeviceId = cameraDevice.Id }; mediaCapture = new MediaCapture(); await mediaCapture.InitializeAsync(settings); initialized = true; } } /// <summary> /// 異步尋找攝像頭,若是沒有找到,返回null,不然返回DeviceInfomation /// </summary> private static async Task<DeviceInformation> FindCameraDevice() { // Get available devices for capturing pictures var allVideoDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); if (allVideoDevices.Count > 0) { // 若是發現,返回 return allVideoDevices[0]; } else { return null; } } /// <summary> /// 開啓攝像頭預覽 /// </summary> public async Task StartCameraPreview() { try { await mediaCapture.StartPreviewAsync(); } catch { initialized = false; Debug.WriteLine("Failed to start camera preview stream"); } } /// <summary> /// 關閉攝像頭預覽 /// </summary> public async Task StopCameraPreview() { try { await mediaCapture.StopPreviewAsync(); } catch { Debug.WriteLine("Failed to stop camera preview stream"); } } /// <summary> /// 拍攝照片,返回StorageFile,文件將被存儲到臨時文件夾 /// </summary> public async Task<StorageFile> CapturePhoto() { // Create storage file in local app storage string fileName = GenerateNewFileName() + ".jpg"; CreationCollisionOption collisionOption = CreationCollisionOption.GenerateUniqueName; StorageFile file = await ApplicationData.Current.TemporaryFolder.CreateFileAsync(fileName, collisionOption); // 拍攝而且存儲 await mediaCapture.CapturePhotoToStorageFileAsync(ImageEncodingProperties.CreateJpeg(), file); //await Task.Delay(500); return file; } /// <summary> /// 產生文件名稱 /// </summary> private string GenerateNewFileName() { return " IoTSample" + DateTime.Now.ToString("yyyy.MMM.dd HH-mm-ss"); } public string GenerateUserNameFileName(string userName) { return userName + DateTime.Now.ToString("yyyy.MM.dd HH-mm-ss") + ".jpg"; } /// <summary> /// 若是攝像頭初始化成功,返回true,不然返回false /// </summary> public bool IsInitialized() { return initialized; }
使用示例:app
1.初始化異步
private WebCamHelper camera; if(camera==null) { camera = new WebCamHelper(); await camera.InitializeCameraAsync(); } if(camera.IsInitialized()) { tbMessage.Text = "Camera啓動成功..."; } else { tbMessage.Text = "Camera啓動失敗..."; }
2.拍攝async
if (!camera.IsInitialized()) return; StorageFile imgFile = await camera.CapturePhoto();
拍攝完成的圖片文件就存儲在上面的imgFile中。ide
3.視頻預覽spa
若是想開啓視頻預覽,實時查看攝像頭捕獲的圖像,能夠在XAML中先添加一個CaptureElement控件:code
<CaptureElement x:Name="cameraElement" Loaded="cameraElement_Loaded"/>
在CaptureElement的Loaded事件中執行source綁定:orm
cameraElement.Source = camera.mediaCapture;
而後在想要開始視頻預覽的地方,執行:視頻
await camera.StartCameraPreview();
關閉視頻預覽:
await camera.StopCameraPreview();