Take Picture with USB Web Camera with 2 lines of C, C++ Program

Following is a pure native C program to take picture from your attached web camera. The best thing is that this is a really short, 2 line program. We have tested with logitech USB camera.
(Rev. 10-May-2024)

Categories | About |     |  

Parveen,

Table of Contents (top down ↓)

A very small C, C++ program to capture a still image from a built-in or attached USB camera to your laptop or PC. We have tested it using a logitech USB camera. The code has been kept short to a bare minimum size so that it is easy to extend and adapt to your needs.

The Plan

IMPORTANT NOTE: ONLY FOR WINDOWS. NEEDS A COMPILER WITH WINDOWS SET UP. COMPILE USING Visual Studio C++, Visual C++ Express, any edition, any version

We have used the library called "video for windows" from the MSDN Win 32 library.

The relevant header file is vfw.h linked with vfw32.lib. The library itself is fairly complex, but only two functions are sufficient for our current task.

"capCreateCaptureWindow" is a macro that has been used to create a capture window. This window is usually embedded in a GUI to provide a running preview. But since we don't need that here, we have, instead, set it as a child of the main desktop. The window stays hidden throughout, and destroyed on exit.

Our next task is to connect this window to a web camera. The numbering of cameras on the windows is 0, 1, 2... In most cases, there is only one camera at index 0. However, on some computers the index 0 could be corrupted and un-available because of some different camera used in the past. So if things fail, try index 1 also.

The "capDriverConnect" macro is used to connect our window to the web camera at index 0.

Next, the first available shot is saved by "capFileSaveDIB". The file is "shot.bmp" saved in the current directory, usually the same directory where the exe is located, or alongside your .cpp file.

Source Code

The code can be copy-pasted to any .cpp file and compiled with a C++ compiler.

// COMPILE ON A WINDOWS COMPILER 
// THAT CAN include windows.h 
// WE HAVE USED VISUAL C++ 2012 
// ANY VERSION CAN BE USED 
// ******standard headers ********** // 
#include "windows.h"

#include "vfw.h"

#include <cstdio>

#pragma comment(lib, "Vfw32.lib")

// ********************************** // 
int main()
{

  // create the preview window 
  HWND hCam = capCreateCaptureWindow (
   L"hoven",
   WS_CHILD,
   0, 0, 0, 0,
   ::GetDesktopWindow(), 0);

  // connect to the first camera 
  // for other cameras try index 
  // 1, 2, in place of the 0 below 
  if(capDriverConnect(hCam, 0))
  {

    capFileSaveDIB(hCam, L"shot.bmp");

    // the screenshot is in the current 
    // directory, usually the same 
    // where the exe is created by 
    // your compiler 
    printf("Saved as shot.bmp!");

  }

  else 
  {

    printf("Check camera?");

  }

  DestroyWindow(hCam);

  return 0;

}


This Blog Post/Article "Take Picture with USB Web Camera with 2 lines of C, C++ Program" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.