在树莓派上能够使用它官方标配的摄像头,可是这个摄像头彷佛不能被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();