Quantcast
Viewing all articles
Browse latest Browse all 1448

VB6 - DirectShow WebCam Minimal Code

There are a number of possible APIs in Windows for previewing and capturing from webcams. One of the most popular for its broad support on Windows versions and its relative ease of use when requirements are simple is the AviCap/Video for Windows API.

But a downside of VfW is that the driver model Windows uses to support video capture devices changed after the end of 16-bit Windows (Win3.1, etc.). This means several things, but most commonly frustrating is that instead of mapping multiple webcams as device 1, 2, ... 9 they work through a compatibility layer thats maps one of them as device 1.

This can make selecting the webcam to use difficult to impractical. And using more than one webcam at a time doesn't seem possible.

The usual answer has been: "Use DirectShow instead of VfW."


DirectShow vs. VB6

One problem with using DirectShow is that Microsoft seemed to have lost enthusiasm after providing only a partial implementation. The parts of DirectShow (also called ActiveMovie) we did get a VB6-friendly API for are implemented in the Quartz.dll which should be part of Windows in any recent version (and perhaps even back to most "late" Windows 95 versions like 95B or OSR2.x).

You can still do a lot of things using just what we have, but the finer points of using DirectShow in VB6 require a 3rd party DLL to wrap a few more DirectShow C++ interfaces.


This "Minimal Approach" to VB6, DirectShow, and WebCams

What I have done here is to try to stretch things as far as I could manage.

Here is what you can do:
  • Choose among your webcams and display a live preview image.
  • "Snap" and display a still image from the webcam feed.

Here is what you can't do:
  • Get a "friendly" list of just the usable webcams.
  • Control the capture resolution/dimensions or other capture settings or even raise the built-in dialogs to let the user do so.


The Demo

What I wanted to accomplish was to see how far I could get with the two tasks we can perform without using any 3rd party libraries.


Form1

This is the main UI Form, which uses Form2 as a dialog when requested via its menu ("Add new camera...").

There is a lot more code there than I'd like that does nothing except manage the menu. As you add cameras they are added to the menu. There is also code there to load and save "settings" which include the index of the selected camera and list of added cameras by name.

Basically a lot of UI-management code which I hope doesn't obscure the DirectShow-related logic itself.

The other ugly hunk of code in there is the BuildGraph() function, which is a small interpreter of a sort that processes a "filter script" and a "connection script" to add the necessary filters and connect them to create a webcam preview graph for DirectShow.


Form2

Since I can't find any way to find just the list of usuable webcams, the user has to pick them out from among the full list of available "filters" (as they are called)! Not practical at all for a real application, but it works for a test/demo program.

That's what Form2 in the demo Project is for, a dialog from which to pick new cameras.

Note that your camera might appear there once, twice, or even three or more times depending on how many "filters" of different kinds it supports. Just pick any of them, the demo program will just use the name and sort it out later.


Module1

This contains some GDI and OLE API calls to convert the captured frame from a "packed DIB" into a StdPicture object that can be used with PaintPicture, etc. This raw StdPicture is created using a "memory DC" so there are some limits on how you can use it, i.e. simply assigning it to a PictureBox.Picture has some issues.

But in this demo we need to scale it anyway since we can't control the actual capture dimensions.

You could rework this passing in the hDC of a Form, PictureBox etc. I suppose.


Running the DSMini1 Project

The attached ZIP archive contains the entire Project.

All you should need to do is unzip the archive, then open the Project in VB6. If you have a webcam connected, you can just go ahead and run it within the IDE.

From there you will have to "Add" your webcam by browsing the filter list.

After a valid add, the live preview starts immediately at the left.

Clicking on the Snap button should take a snapshot and display it in the PictureBox at the right.

If you have another webcam connectd you shuld be able to add that one too. Once you have two or more added you can choose among them via Form1's menu.

Settings are persisted in Settings.txt, so a subsequent run should save you the trouble of picking cameras again.


Remarks

I don't know what webcams this will work with, but I know it works with two very different ones I've tried so far.
Attached Images
Image may be NSFW.
Clik here to view.
  Image may be NSFW.
Clik here to view.
 
Attached Files

Viewing all articles
Browse latest Browse all 1448

Trending Articles