Example code
→
Return to Development Section
ActiveX
ActiveX is a framework for defining reusable software components which can be easyly integrated into various programming-language independend environments. It was introduced in 1996 by Microsoft as a development of its Component Object Model (COM) and Object Linking and Embedding (OLE) technologies. The ActiveX interface with its diverse functionality gives you the possibility to communicate with the Unifeye ActiveX control (OCX). It allows the integration of Unifeye into any programming language/environment that supports ActiveX, like C#, Visual Basic, C++, etc.
The following sections describing shortly the integration of Unifeye OCX into web based applications using VBScript, as well as into offline application based on C# language. The demos shown below are available within the folder
<UnifeyeSDKInstallationFolder>/examples and from the Windows start menu entry for Unifeye, within the submenu SDK -> Demos.
Creating Application using ActiveX and VBScript
The Unifeye SDK from metaio is an ActiveX-control which is compatible with Internet Explorer 5.0. To bind the ActiveX-control to an internet site the following code must be inserted in the site's html code:
<OBJECT ID="AS_AR_CONTROL" WIDTH=100% HEIGHT=100% CLASSID="CLSID:35AE3DEE-6C80-4EF5-AE1E-87C5E15B0BF0">
</OBJECT> font>
The parameters WIDTH and HEIGHT allow you to specify the size of a video picture. E.g. WIDTH = 320 for a width of 240 pixels. The size can also be specified as a percentage of the windowsize, e.g. WIDTH = 100% for maximum size.
The ActiveX-control can be invoked via a Visual Basic script over an interface. The code below demonstrates the usage of the AR-functionality using this interface.
To start of, we must first specify a video source. We can either load an image source in terms of a video or an image by calling setImageSource(). Or we use a camera by calling getNumberOfCamsAvailable() to get the number of connected cameras and afterwards activateCamera(0) to activate the first connected camera. We are able to switch between different cameras at run-time, so that plug-n-play functionality is fully supported. E.g. if you plug in a camera, then simply call getNumberOfCamsAvailable() to get the latest number of cameras currently plugged in.
The next step is to load a VRML geometry with the function-call loadGeometry(). To bind the VRML to a coordinate system we use the function bindSceneToCos(). The first variable specifies the id of the VRML model which was is the return value of the function loadGeometry(). The second variable is the coordinate system id.
The following code demonstrates this functionality:
<HTML>
<HEAD>
<SCRIPT LANGUAGE="VBScript">
<!--
Sub init()
numCams = AS_AR_CONTROL.getNumberOfCamsAvailable()
call AS_AR_CONTROL.activateCamera(0)
testID = AS_AR_CONTROL.loadGeometry("C:\COS.wrl")
call AS_AR_CONTROL.bindSceneToCOS(testID,1)
end sub
-->
</SCRIPT>
</HEAD>
<BODY OnLoad="init()">
<BR>
<CENTER>
<OBJECT ID="AS_AR_CONTROL" WIDTH="320" HEIGHT="240" CLASSID="CLSID:35AE3DEE-6C80-4EF5-AE1E-87C5E15B0BF0">
</OBJECT>
</CENTER>
</BODY>
</HTML> Figure below, shows a sample AR-application. Most security properties in a browser such as Internet Explorer, require that the browser first asks for permission to run the ActiveX-control. Confirming with a "Yes" will display a 320x240 pixel window with the actual live video stream. Simply move the webcam over the marker (e.g. we see that marker 1 is referenced, this has a single black quadrant inside a black square) and we can see a 3D-coordinate system.

Please also refer to the HTML demo pages inside the "examples" section of the Unifeye SDK installation:
<UnifeyeSDKInstallationFolder>/examples/ARCube.html (very simple demo) and
<UnifeyeSDKInstallationFolder>/examples/UnifeyeSDK-DemoHTML.html (a more elaborate demo).
Note an 32-Bit and 64-Bit: Please note, that currently the Unifeye ActiveX control is only available as 32-Bit version. This implies that you will also have to use the 32-Bit version of Internet Explorer for running it. Otherwise the Unifeye control can't be loaded.
Using ActiveX component in C# applications (Windows Forms)
As an ActiveX-control, the Unifeye SDK can also be integrated in a .NET Windows Forms GUI application (e.g. C#, VB .Net). To create such an application only a few simple steps are necessary:
- Open your development environment, e.g. Visual Studio 2005/2008/2010
- Create a new empty C# Windows Forms application.
- Add the metaio Unifeye SDK to your toolbox, by right clicking into the toolbox and choosing, "Select Element", COM-elements:
AS_AR_Control Control.
- Now drag the
AS_AR_Control Control icon onto your windows form.
- Add further control elements and connect them to function calls of the Unifeye SDK.
There is also a more elaborate example C# application provided with the Unifeye SDK:
<UnifeyeSDKInstallationFolder>/examples/UnifeyeSDKExample. It is well documented and shows:
- how to load a tracking configuration file,
- how to load a simple 3D model,
- how to activate a connected camera and
- periodically retrieves the tracking values in order to change the color of the loaded 3D model according to the z rotation of the marker being used for tracking.
Note on 32-Bit and 64-Bit: As the Unifeye ActiveX component is currently only available as 32-Bit version you will have to set target platform to "x86" in the project settings. The default "Any CPU" won't work on 64-Bit systems as it will start the .Net environment in 64-Bit mode which then fails loading the 32-Bit Unifeye component.
Using ActiveX component in C#/VB.Net applications (Windows Presentation Foundation)
The Unifeye SDK ActiveX-control can also be integrated into a .NET Windows Presentation Foundation (WPF) GUI application (e.g. using C#). As WPF does not support ActiveX directly you will need to create an according Windows Forms control to host the ActiveX in order to use it in your WPF application.
To create such an application only a few simple steps are necessary:
- Open your development environment, e.g. Visual Studio 2010
- Create a new empty C# or VB.Net WPF application
- Add a new element of type "User Control" (which is a Windows Forms User Control) to the project and name it "UnifeyeOCXControl.cs" or "UnifeyeOCXControl.vb"
- Open the just created user control in design mode (this usually happens automatically after creation)
- Add the metaio Unifeye SDK to your toolbox (if not already happend), by right clicking into the toolbox and choosing, "Select Element", COM-elements: AS_AR_Control Control.
- Now drag the AS_AR_Control Control icon onto your newly created user control "UnifeyeOCXControl"
- Select the "Dock" property of the AS_AR_Control and set it to "Fill"
- Now open the C#/VB code for the "UnifeyeOCXControl" and add a method that returns the AS_AR_Control you just dragged onto your user control (usually it is named:
axAS_AR_Control1 if you do not change anything manually).:
- Add references to two .Net assemblies to the project: WindowsFormsIntegration (
WindowsFormsIntegration.dll) and Windows Forms ( System.Windows.Forms.dll).
- Now, from the toolbox, drag a "WindowsFormsHost" element onto your WPF main window
- Select the WPF main window again, click into the events tab and double click the event "Loaded". This will generate an according event handler and bring you directly to the according location in the source code
- Before adjusting the event handler source code, add a private variable to your WPF window's class holding a reference to the Windows Forms user control hosting the Unifeye SDK ActiveX control:
- In the event handler, add the following lines of code. They create a new instance of the user control you created above that hosts the actual AS_AR_Control and make it a child of the WindowsFormsHost you just dragged onto your main window:
- That's it. You have now placed a Windows Forms user control, that hosts the Unifeye SDK ActiveX onto your WPF window using a WindowsFormsHost. You can now access all functions of the Unifeye SDK using
Microsoft also provides a guide for using ActiveX elements in WPF applications:
http://msdn.microsoft.com/en-us/library/ms742735.aspx
Note on 32-Bit and 64-Bit: As the Unifeye ActiveX component is currently only available as 32-Bit version you will have to set target platform to "x86" in the project settings. The default "Any CPU" won't work on 64-Bit systems as it will start the .Net environment in 64-Bit mode which then fails loading the 32-Bit Unifeye component.
Note on application exiting when starting from Visual Studio: Some users report that if they create applications using .Net Framework 4.0 (which is needed for WPF applications) and start them from Visual Studio, the application exits directly after start up. This seems to be a compatibility issue with .Net Framework 4.0 which will be solved in the future. For now you can compile your application and start the resulting .exe file directly (e.g. using the Windows file explorer). Then attach the Visual Studio debugger to it (Debugging -> Attach to process -> Select the according process and attach.).
→
Return to Development Section
--
SupportMetaio - 2011-01-21