Using the webcam on Windows IoT

Source: Internet
Author: User

It can be used on Raspberry Pi as the official standard camera, but this camera does not seem to be recognized and used by Windows IoT. However, you can insert any type of camera on the Raspberry Pi's USB port to achieve the Raspberry Pi shooting function.

Regarding the camera search and shooting, I encapsulate it into a class, as follows:

     Public classWebcamhelper { PublicMediaCapture MediaCapture; Private BOOLinitialized =false; /// <summary>        ///initializing The webcam asynchronously/// </summary>         Public AsyncTask Initializecameraasync () {if(MediaCapture = =NULL)            {                //try to find the camera                varCameradevice =awaitFindcameradevice (); if(Cameradevice = =NULL)                {                    //no cameras found .Debug.WriteLine ("No camera found!"); Initialized=false; return; }                //creates MediaCapture initialization settings with FOUDND webcam device                varSettings =Newmediacaptureinitializationsettings {Videodeviceid =cameradevice.id}; MediaCapture=NewMediaCapture (); awaitMediacapture.initializeasync (settings); Initialized=true; }        }        /// <summary>        ///asynchronously looks for the camera, returns null if not found, otherwise returns deviceinfomation/// </summary>        Private Static AsyncTask<deviceinformation>Findcameradevice () {//Get available devices for capturing pictures            varAllvideodevices =awaitDeviceinformation.findallasync (deviceclass.videocapture); if(Allvideodevices.count >0)            {                //if found, returns                returnallvideodevices[0]; }            Else            {                return NULL; }        }        /// <summary>        ///turn on camera preview/// </summary>         Public AsyncTask Startcamerapreview () {Try            {                awaitMediacapture.startpreviewasync (); }            Catch{initialized=false; Debug.WriteLine ("Failed to start camera preview stream"); }        }        /// <summary>        ///Turn off camera preview/// </summary>         Public AsyncTask Stopcamerapreview () {Try            {                awaitMediacapture.stoppreviewasync (); }            Catch{Debug.WriteLine ("Failed to stop camera preview stream"); }        }        /// <summary>        ///take photo, return storagefile, file will be stored in temp folder/// </summary>         Public AsyncTask<storagefile>Capturephoto () {//Create storage file in local app storage            stringFileName = Generatenewfilename () +". jpg"; Creationcollisionoption collisionoption=Creationcollisionoption.generateuniquename; StorageFile file=awaitApplicationData.Current.TemporaryFolder.CreateFileAsync (FileName, collisionoption); //Capture and store            awaitMediacapture.capturephototostoragefileasync (Imageencodingproperties.createjpeg (), file); //await Task.delay (+);            returnfile; }        /// <summary>        ///Generate file name/// </summary>        Private stringGeneratenewfilename () {return "Iotsample"+ DateTime.Now.ToString ("yyyy. MMM.DD Hh-mm-ss"); }         Public stringGenerateusernamefilename (stringuserName) {            returnUserName + DateTime.Now.ToString ("yyyy. MM.DD Hh-mm-ss") +". jpg"; }        /// <summary>        ///Returns true if the camera initialization succeeds, otherwise false/// </summary>         Public BOOLisinitialized () {returninitialized; }

Examples of Use:

1. Initialization

        PrivateWebcamhelper camera; if(camera==NULL) {Camera=NewWebcamhelper (); awaitcamera.            Initializecameraasync (); }            if(camera. IsInitialized ()) {Tbmessage.text="camera started successfully ..."; }            Else{Tbmessage.text="camera failed to start ..."; }    

2. Shooting

            if return ;             await camera. Capturephoto ();

The completed picture file is stored in the imgfile above.

3. Video preview

If you want to turn on video previews and view the images captured by the camera in real time, you can add a Captureelement control in XAML first:

<x:name= "Cameraelement"                        Loaded= "cameraelement_loaded"  />

To perform a source binding in the Captureelement loaded event:

Cameraelement.source = camera.mediacapture;

Then, in the place where you want to start the video preview, do:

await camera. Startcamerapreview ();

To turn off video preview:

await camera. Stopcamerapreview ();

Using the webcam on Windows IoT

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.