Using Webcam on windows should be easy, that is what i thought when i started out and wasted three days trying to get thing to work reliably. Most people used Video for Windows (VFW) in tutorials , but i found it very unreliable and crashing often, also when debugging it threw wired errors. Then I made up my mind to use the DirectX based DirectShow methods, Its needs DirectX SDK to build it and on downloading it and finally looking for the files i found that functionality have been moved to platform SDK. Anyhow i was not downloading another 4.5 GB of SDK to test if it worked. So after some Googling i found about the VideoInput library (uses DirectShow methods) which people sometimes use for OpenCV. I wished i had found it earlier , here is a small tutorial on hot to get started using it , since the original files which came with it did not work for me.There are a whole host of features supported so you can explore the documentation for more .
VideoInput Library homepage
How to use webcam/tv tuner/capture cards in VC++ to capture video and take pictures –
1. Create simple Dialog based MFC project.
2. In the dialog make a Picture control and a Button , Picture control will Display the Video stream and the button will be used to capture a snapshot.Change Picture control to type Bitmap and attach a CStatic variable to it.
3. Now we will use a simple timer ( WM_TIMER ) to update the image being captured there are better way but this should do for a tutorial. In OnInitDialog() set the timer to refresh at say 10 times a second,
SetTimer(NULL,100,NULL)
4. Include the video videoInput.h header file. Also add the videoinput.lib in the linker input and for ignore library atlthunk.lib ( if it causes errors ).
5. Initialize the device using VideoInput API .
// in header
int device,width,height,size;
videoInput VI;
unsigned char * captureBuffer;
// in InitDoalog
device = 0; // first webcam
VI.setupDevice(device,320,240); // 320 x 240 resolution
width = VI.getWidth(device);
height = VI.getHeight(device);
size = VI.getSize(device); // size to initialize buffer
captureBuffer = new unsigned char[size]; // our capturebuffer
6. Now lets capture video and display frame in the OnTimer
if(VI.isFrameNew(device))
{
VI.getPixels(device,captureBuffer, false, true);
DisplayImage(&m_webcam,height,width,captureBuffer);
}
7. There is also example of how to capture image using libjpeg, please download the source and check it out.
Downloads: 262 File Size: 950.7 KiB