Using Windows controls in the Web to implement camera functionality

Source: Internet
Author: User

A recent web-based video conferencing project that needs to play live video from a remote camera in a Web page, we already have a Windows control written in C # that plays remote live video, how do I embed it in a Web page? This requires the use of an ancient technique, ActiveX.

1. Convert. NET controls to ActiveX controls

The first thing to do is to convert our Windows video playback controls into ActiveX controls. First look at the definition of our video playback controls, which are based on the OMCS implementation and are fairly straightforward:

[CSharp]View PlainCopy
  1. Public partial class Cameravideoplayer:usercontrol
  2. {
  3. private Imultimediamanager Multimediamanager;
  4. Public Cameravideoplayer ()
  5. {
  6. InitializeComponent ();
  7. }
  8. public void Test ()
  9. {
  10. Random ran = new Random ();
  11. string userID = "BB" + ran. Next (1001,9999).  ToString ();
  12. This .  Initialize ("223.4.*.*", 9900, UserID, "Aa01");
  13. }
  14. public void Initialize (string serverip, int port, string UserID, string targetuserid)
  15. {
  16. Try
  17. {
  18. This.multimediamanager = Multimediamanagerfactory.getsingleton ();
  19. this.multimediaManager.Initialize (UserID, "", ServerIP, Port);
  20. This.cameraConnector1.BeginConnect (Targetuserid);
  21. }
  22. catch (Exception ee)
  23. {
  24. MessageBox.Show (EE. Message);
  25. }
  26. }

When its initialize method is called, it connects to the target user's camera and plays the video on its included CameraConnector1 control. This control works well in the Windows Form application, and now we step through the process to convert it to an ActiveX control.

(1) GUID

The ActiveX control is first a COM component, and the COM component has a unique GUID. As we can see later, in the Web, you need to locate and load the registered ActiveX control through a GUID.

If you are using VS2010, there is a "Create GUID" menu under the Tools menu, click it to create a new GUID, and then copy it as the Cameravideoplayer feature:

[CSharp]View PlainCopy
    1. [Guid ("d9906b42-56b3-4b94-b4f9-a767194a382f")]
    2. Public partial class Cameravideoplayer:usercontrol
(2) Implement IObjectSafety interface

When an ActiveX control is called in a browser, a warning box appears, prompting the unsafe control to be running. This is limited by the browser security policy, and the control implements the IObjectSafety interface to show the browser that it is legitimate. Add the definition of the IObjectSafety interface in the project:

[CSharp]View PlainCopy
    1. [guid ( "cb5bdc81-93c1-11cf-8f20-00805f2cd064"),  interfacetype (Cominterfacetype.interfaceisiunknown)]  
    2. interface iobjectsafety  
    3. {          
    4.      Void getinterfaccesafyoptions (system.int32 riid,out system.int32  Pdwsupportedoptions,out system.int32 pdwenabledoptions);   
    5.   
    6.     void  setinterfacesafetyoptions (system.int32 riid, system.int32 dwoptionssetmask,  system.int32 dwenabledoptions);   
    7. }  

And let Cameravideoplayer implement this interface:

[CSharp]View PlainCopy
  1. [Guid ("d9906b42-56b3-4b94-b4f9-a767194a382f")]
  2. Public partial class Cameravideoplayer:usercontrol, IObjectSafety
  3. {
  4. private Imultimediamanager Multimediamanager;
  5. Public Cameravideoplayer ()
  6. {
  7. InitializeComponent ();
  8. }
  9. public void Test ()
  10. {
  11. Random ran = new Random ();
  12. string userID = "BB" + ran. Next (1001,9999).  ToString ();
  13. This .  Initialize ("223.4.180.116", 9900, UserID, "Aa01");
  14. }
  15. public void Initialize (string serverip, int port, string UserID, string targetuserid)
  16. {
  17. Try
  18. {
  19. This.multimediamanager = Multimediamanagerfactory.getsingleton ();
  20. this.multimediaManager.Initialize (UserID, "", ServerIP, Port);
  21. This.cameraConnector1.BeginConnect (Targetuserid);
  22. }
  23. catch (Exception ee)
  24. {
  25. MessageBox.Show (EE. Message);
  26. }
  27. }
  28. public void getinterfaccesafyoptions (int riid, out int pdwsupportedoptions, out int pdwenabledoptions)
  29. {
  30. Pdwsupportedoptions = 1;
  31. Pdwenabledoptions = 2;
  32. }
  33. public void setinterfacesafetyoptions (int riid, int dwoptionssetmask, int dwenabledoptions) /c3>
  34. {
  35. }
  36. }

The implementation of the two methods of the IObjectSafety interface can be done using the code above.

(3) Assembly settings

Next, we need to make a setting for the control's Assembly (Omcs_activex) to indicate that it will be used as a COM component. Open the AssemblyInfo.cs file, and first set the ComVisible attribute to True. Second, increase the allowpartiallytrustedcallers characteristics. As shown below:

[CSharp]View PlainCopy
    1. Set ComVisible to False to make the types in this Assembly
    2. //is not visible to COM components. If you need to access the types in this assembly from COM,
    3. //sets the ComVisible attribute on the type to true.
    4. [Assembly:comvisible (true)]
    5. [Assembly:allowpartiallytrustedcallers ()]

Finally, on the build page of the project properties, check the checkbox for COM Interop registration.

Thus, in addition to Omcs_activex.dll, the generated product of the compilation has omcs_activex.tlb (the type library file used by COM).

2. Making the installation program

The converted Cameravideoplayer ActiveX control is deployed on the IIS server, the first time a user opens a Web page, the control does not exist on the user's machine, so it is necessary to download the installation and register the ActiveX control on the user's machine. These can be achieved through the features of the VS bring-your-own setup program, and it's fairly straightforward.

(1) Add a new Setup project to the current solution.

(2) Import the main output of the Omcs_activex project into the installation project under "Application Folder".

(3) Modify the main output the file installation property of the register entry is vsdrpcom.

(4) Set the project properties of the Setup project, primarily the "Install URL" entry, to be set as the deployment-time address.

(5) If necessary, tick or remove some of the items in prerequisites.

(6) Compile the installation project, will generate two files Setup.exe, Setup1.msi. Copy them to the root directory of the Web site virtual directory.

3.Web Integration

Now let's write one of the simplest HTML to try to load the video playback of the ActiveX control Cameravideoplayer. As shown below:

[HTML]View PlainCopy
  1. <HTML xmlns="http://www.w3.org/1999/xhtml" >
  2. <head>
  3. <title> Webcam video player Test </title>
  4. </head>
  5. <body>
  6. <form id="Form1">
  7. <table>
  8. <tr>
  9. <TD Align="center">
  10. <object id="Cameravideoplayer"
  11. <strong> classid="clsid:{d9906b42-56b3-4b94-b4f9-a767194a382f}" codebase=" Setup.exe "</strong> width=" " height=">
  12. </Object>
  13. </td>
  14. </tr>
  15. <tr>
  16. <TD Align="center">
  17. <input type=button id="Button1" value="Connect webcam" onclick="javascript: Dotest () "/>
  18. </td>
  19. </tr>
  20. </table>
  21. <script type="Text/javascript">
  22. function Dotest ()
  23. {
  24. var obj = document.getElementById ("Cameravideoplayer");
  25. Obj. Test ();
  26. }
  27. </Script>
  28. </form>
  29. </Body>
  30. </html>

Note the bold section, which shows two points:

(1) The browser uses the GUID to locate the ActiveX control.

(2) If the target ActiveX control does not exist on this computer, the installation program that is indicated by the CodeBase property is automatically downloaded.

After you have deployed the HTML file, open the Web page for the first time, as follows:

After you run the installation, the page refreshes and you can see that the ActiveX control has been successfully loaded in. Then, click on the "Connect webcam" button to test if the ActiveX control can display the video captured by the remote camera as follows:

This way, ActiveX controls that are embedded in a Web page run as normal as normal Windows controls:)

Using Windows controls in the Web to implement camera functionality

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.