Silverlight4: Detection of camera occupation status and two screenshot capture methods

Source: Internet
Author: User

Status detection mainly includes two aspects: whether the camera is installed, and whether the camera is otherwiseProgramOccupied

There are two video Methods: one is to directly use the captureimageasync asynchronous screenshot of the capturesource class, and the other is to directly use writeablebitmap to capture the screen. The difference between the two screenshot methods is that, captureimageasync always captures the original video content. If you use writeablebitmap To Capture screenshots in a specified area, if there are other controls (such as textblock) on the video ), other contents of the final screenshot will be stacked on the top (for example)
 

XAML SectionCode:

<Usercontrol X: class = "cameracheck. mainpage "xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation "xmlns: x =" http://schemas.microsoft.com/winfx/2006/xaml "xmlns: D =" http://schemas.microsoft.com/expression/blend/2008 "xmlns: MC =" http://schemas.openxmlformats.org/markup-compatibility/2006 "MC: ignorable =" D "> <grid X: name = "layoutroot" background = "white"> <grid. rowdefinitions> <rowdefinition Height = "162"> </rowdefinition> <rowdefinition Height = "190"> </rowdefinition> </grid. rowdefinitions> <stackpanel grid. row = "0"> <canvas Height = "120" width = "160" horizontalalignment = "center" X: Name = "cvideo" margin =, 160 "> <rectangle width =" 120 "Height =" "X: name = "rectvideo" strokethickness = "1" stroke = "black"> </rectangle> <textblock X: name = "lblstatus" text = "Yang Guo under the bodhi tree" foreground = "white" canvas. top = "50" width = "160" textalignment = "center"> </textblock> </canvas> <stackpanel orientation = "horizontal" horizontalalignment = "center"> <button width = "80" Height = "22" content = "detection camera" X: name = "btncheck" Click = "btncheck_click"> </button> <button width = "80" Height = "22" content = "original video" X: name = "btncapture1" margin = "5, 0, 0, 0 "Click =" btncapturew.click "> </button> <button width =" 80 "Height =" 22 "content =" bitmap "X: name = "btncapture2" margin = "5, 0, 0" Click = "btncapture2_click"> </button> </stackpanel> <scrollviewer X: name = "imglist" Height = "180" horizontalscrollbarvisibility = "visible" verticalscrollbarvisibility = "hidden" margin = "," grid. row = "1"> <stackpanel X: Name = "SP" orientation = "horizontal"> </stackpanel> </scrollviewer> </GRID> </usercontrol>

backend CS code:

Using system. windows; using system. windows. controls; using system. windows. media; using system. windows. media. imaging; namespace cameracheck {public partial class mainpage: usercontrol {bool _ isworked = false; capturesource _ webcamsource; Public mainpage () {initializecomponent ();} private void btncheck_click (Object sender, routedeventargs e) {If (_ isworked) {return;} videocapturedevice webcam = Ca Pturedeviceconfiguration. getdefavidevideocapturedevice (); If (webcam = NULL) {lblstatus. Text = "no camera device detected! "; Return;} If (capturedeviceconfiguration. requestdeviceaccess () {_ webcamsource = new capturesource (); _ webcamsource. videocapturedevice = webcam; videobrush Video = new videobrush (); video. setsource (_ webcamsource); video. stretch = stretch. uniformtofill; try {_ webcamsource. start (); this. rectvideo. fill = video; lblstatus. TEXT = "the camera is working... "; // test with _ isworked = true; _ webcamsource. capturefailed + = Webcamsource_capturefailed; _ webcamsource. captureimagecompleted + = webcamsource_captureimagecompleted;} catch {lblstatus. TEXT = "unable to use camera (possibly occupied)" ;}} else {lblstatus. TEXT = "you do not agree to use the camera device in this program \ n. ";}} /// <summary> /// captureimageasync original video content /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> private void btncapture1_click (Object sender, routedeventargs e) {If (_ iswor Ked) {_ webcamsource. captureimageasync () ;}} void webcamsource_captureimagecompleted (Object sender, captureimagecompletedeventargs e) {writeablebitmap WB = E. result; image = new image (); image. height = 120; image. margin = New thickness (5); image. source = WB; SP. children. add (image);} void webcamsource_capturefailed (Object sender, exceptionroutedeventargs e) {lblstatus. TEXT = "screenshot failed! ";} /// <Summary> /// screenshot capture using writeablebitmap /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> private void btncapture2_click (Object sender, routedeventargs e) {writeablebitmap WB = new writeablebitmap (cvideo, null); image = new image (); image. height = 120; image. margin = New thickness (5); image. source = WB; SP. children. add (image );}}}

In addition, when using a camera in Silverlight 4, the system prompts the user whether to agree to use the camera. The "remember me" option is added to the interface.

This feature is intended to be convenient for users and does not have to be prompted every time, but also has a side effect: for example, during the first selection, if you accidentally check "remember my answer" and select "no", the system will reject the use of camera devices without any prompt!

Of course, you can also right-click and remove the memory of such errors (such as) in the Silverlight option)

But the problem is: The user may not know where to go to this interface, so I personally think if Silverlight automatically remembers "Disable camera", can I give a prompt? This will be more friendly

 

Finally, technically speaking, the camera of Silverlight 4 is not as considerate as flash. In flash, the camera has Activity events that can be used to monitor whether the camera has activity, but not in Silverlight. This is useful in some fields. For example, the monitoring system often needs to judge whether the camera is facing a static object. (Of course, the disguised method in SL can compare the differences between the two images through continuous screenshots, but this is too troublesome. I don't know if it will be improved in the official version)

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.