Capturing Webcam Images
It seemed like a simple task really; capture a still image from a webcam and save it to a file. Unfortunately something that appeared so simple was quite complex. If you’re familiar with C++ and want to use the Windows Image Acquisition (WIA) API, have at it. My personal preference is to stay with C# or VB.NET and while it’s possible to use the Windows Image Acquisition Automation Layer, that didn’t really seem to fit the bill (at least for this scenario).
Thankfully there is the Touchless SDK, created by Michael Wasserman, Gary Caldwell and John Conwell. This SDK not only provides the assemblies to interface with a webcam (just 2 as a matter of fact: TouchlessLib.dll and WebCamLib.dll) but since it’s a CodePlex project, the source as well. The latter point is important as in my case I wanted to capture high resolution images as opposed to the default size of 320x240 pixels.
Using this SDK shields you from a lot of the low level coding and with a simple reference you’re up and running in no time. The objects that I was most concerned with were the Camera and TouchlessMgr classes. By simply “newing up” a TouchlessMgr you can have access to all of the available cameras via iteration. Note: You can only attach to one camera at a time.
//initialize the Touchless Manager object
Manager = new TouchlessMgr();
From here it’s possible to assign the camera (integrated or USB) and manipulate it at will. I especially liked the ShowPropertiesDialog method which with one line of code provides access to a plethora of properties settings.
Now all this is great but what about the original problem of capturing a single image from the video feed? Not a problem as the following code snippet does exactly that:
PictureBox1.Image = Manager.CurrentCamera.GetCurrentImage();
Saving the image is straight ahead by invoking a SaveFileDialog box and setting the image format to “Jpeg” based on the PictureBox.
As mentioned earlier, the default capture of 320x240 pixels was just too small so to adjust that I had to load the TouchlessLib project and change the CaptureWidth and CaptureHeight dimensions. Note: You have to adjust it here as these are read-only properties unless you change the “set” attribute.
I will mention that I wanted to create a typical WinForms application however with Windows 8 arriving in the not so distant future, the next logical question would be: How does one accomplish this for say a Metro styled application? Good question and the answer is by using the MediaCapture API.
All that being said, the Touchless SDK works well, has good examples and is extensible. The only item I’d like to see improved would be the documentation (i.e. Touchless.chm). The help file could add VB examples (it already has sections for VB syntax) and be updated to use the Microsoft Help System. Otherwise, kudos for an extremely useful SDK!