在Windows IoT上使用網路攝影機

來源:互聯網
上載者:User

標籤:date   ble   user   網路攝影機   inf   rar   dcamera   ons   型號   

在樹莓派上可以使用它官方標配的網路攝影機,但是這個網路攝影機似乎不能被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 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;        }

使用樣本:

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();

 

在Windows IoT上使用網路攝影機

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.