日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

在Windows IoT上使用网络摄像头

發布時間:2025/5/22 windows 56 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在Windows IoT上使用网络摄像头 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在樹莓派上可以使用它官方標配的攝像頭,但是這個攝像頭似乎不能被Windows IoT識別和使用。但是,可以在樹莓派的USB口上插入任意型號的攝像頭,就可以實現樹莓派的拍攝功能。

關于攝像頭的尋找和拍攝,我將其封裝成一個類,如下:

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 devicevar 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 picturesvar 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 storagestring 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;}

使用示例:

1.初始化

private WebCamHelper camera;if(camera==null){camera = new WebCamHelper();await camera.InitializeCameraAsync();}if(camera.IsInitialized()){tbMessage.Text = "Camera啟動成功...";}else{tbMessage.Text = "Camera啟動失敗...";}

2.拍攝

if (!camera.IsInitialized()) return;StorageFile imgFile = await camera.CapturePhoto();

拍攝完成的圖片文件就存儲在上面的imgFile中。

3.視頻預覽

如果想開啟視頻預覽,實時查看攝像頭捕獲的圖像,可以在XAML中先添加一個CaptureElement控件:

<CaptureElement x:Name="cameraElement"Loaded="cameraElement_Loaded"/>

在CaptureElement的Loaded事件中執行source綁定:

cameraElement.Source = camera.mediaCapture;

然后在想要開始視頻預覽的地方,執行:

await camera.StartCameraPreview();

關閉視頻預覽:

await camera.StopCameraPreview();

?

轉載于:https://www.cnblogs.com/mengnan/p/6790583.html

總結

以上是生活随笔為你收集整理的在Windows IoT上使用网络摄像头的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。