Click here to Skip to main content
15,868,141 members
Articles / Desktop Programming / ATL
Article

Simultaneous Previewing & Video Capture using DirectShow

Rate me:
Please Sign up or sign in to vote.
4.35/5 (33 votes)
15 Apr 2004CPOL3 min read 642.6K   20.1K   140   210
A method for previewing and framegrabbing simultaniously from webcam using DirectShow.

Image 1

Introduction

This article explains a method to perform Previewing and Grabbing simultaneously from a webcam using DirectShow. Other than using the conventional ISampleGrabber interface, this application uses DirectShow VMR Filters & IPin Interfaces.

Background

There are many solutions for capturing and previewing but a few combining both. I tried to make an application which serves this purpose using ISampleGrabber and IVideoWindow interfaces. It worked well on Windows 2000 but showed reluctance on XP.

DirectShow "Windowless Sample" (Windowless mode in the Video Mixing Renderer) gives the clue for capturing frames while previewing.

Using the code

I had created a class named CVMR_Capture. To use this class:

  • Insert the VMR_Capture.h and VMR_Capture.cpp files into your project.
  • Link strmiids.lib, quartz.lib in VC++ Project Settings.
  • Add a static control into your dialog resource.
  • Add a member variable for that static control.
  • Declare a member variable of CVMR_Capture.

The public interfaces provided are:

int EnumDevices(HWND hList);
HRESULT Init(int iDeviceID,HWND hWnd,int iWidth,int iHeight);
DWORD GetFrame(BYTE ** pFrame);
BOOL StopVMR();
BOOL Pause();
DWORD ImageCapture(LPCTSTR szFile);
DWORD GrabFrame();
  • int EnumDevices(HWND hList); (Optional)

    Enumerates the available devices connected to the computer. HWND is the HANDLE to a combo box which displays the connected devices.

  • HRESULT Init(int iDeviceID,HWND hWnd,int iWidth,int iHeight);

    Initializing. iDeviceID is the selected device index (from combo Box), hWnd is the handle of the display window, iWidth and iHeight are the expected resolution (e.g.: 320/240). The display will adjust itself to the size of the window.

  • DWORD GrabFrame();

  • DWORD GetFrame(BYTE ** pFrame);

    Call these functions to get the captured buffer. GrabFrame(); will grab the image. Store it in a buffer and return value is the size of the buffer. User can get the buffer pointer by calling GetFrame(BYTE ** pFrame) which returns the size of the buffer.

    Don't delete the "pFrame" buffer value, which will be deleted by the class destructor. Use the Pointer(pFrame) for processing or copying the memory to a user allocated Buffer.

  • BOOL StopVMR();

    Stops previewing.

  • BOOL Pause();

    Pauses capturing. Call this method again to restart capturing.

  • DWORD ImageCapture(LPCTSTR szFile);

    Capture and write the Raw Image BYTES (RGB) to a file provided.

Special Functions and explanation

bool BindFilter(int deviceId, IBaseFilter **pFilter);
HRESULT InitializeWindowlessVMR(HWND hWnd);
HRESULT InitVideoWindow(HWND hWnd,int width, int height);
void StopCapture();
void CloseInterfaces(void);
void DeleteMediaType(AM_MEDIA_TYPE *pmt);
bool Convert24Image(BYTE *p32Img,BYTE *p24Img,DWORD dwSize32);
  • bool BindFilter(int deviceId, IBaseFilter **pFilter);

    Binds the Filer to the given device ID.

  • HRESULT InitializeWindowlessVMR(HWND hWnd);

    VMR initialization to the HWND provided.

  • HRESULT InitVideoWindow(HWND hWnd,int width, int height);

    Adjust the Display Video Size according to the size of the display window.

  • void StopCapture();

    Stops capturing.

  • void CloseInterfaces(void);

    Releases & deletes the interfaces /allocated memory.

  • void DeleteMediaType(AM_MEDIA_TYPE *pmt);

    Deletes Media Type object.

  • bool Convert24Image(BYTE *p32Img,BYTE *p24Img,DWORD dwSize32);

    The captured image using VMR is 32 bit RGB. This function will convert the 32 bit RGB to 24 bit RGB.

Points of Interest

This class is tested on Windows 2000 & Windows XP with DirectX9.0. Both VMR7 & VMR9 are supported. I haven't included the DirectShow Event Handling part in this sample. Please attach the required exception/error handling mechanism when you use.

Please drop me an email about any bugs, or suggestions to enhance the class. Your suggestions and guidance will be most valuable.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Control Frame Rate and Dimensio Pin
teddy15b22-Dec-05 15:51
teddy15b22-Dec-05 15:51 
GeneralRe: Control Frame Rate and Dimensio Pin
deji10129-Sep-06 6:09
deji10129-Sep-06 6:09 
GeneralNo 'dshow.h' file Pin
avogadro625-Sep-05 23:46
avogadro625-Sep-05 23:46 
GeneralRe: No 'dshow.h' file Pin
avogadro627-Sep-05 2:57
avogadro627-Sep-05 2:57 
GeneralJust install Platform SDK. Pin
torija19-Sep-05 23:59
torija19-Sep-05 23:59 
Generalplease , i have an error in dshow.h Pin
dynamica12330-Aug-05 4:20
dynamica12330-Aug-05 4:20 
GeneralCompilation error Pin
Mauro Gagna23-Aug-05 10:19
Mauro Gagna23-Aug-05 10:19 
GeneralRe: Compilation error Pin
Mauro Gagna23-Aug-05 10:52
Mauro Gagna23-Aug-05 10:52 
I find my own answer.

I didn't set the "aditional library path" to the DirectX SDK lib folder.D'Oh! | :doh:

sorry Smile | :)

----------------------------------------------------
May be you think "He's mad", but i'm not the only one.
GeneralRe: Compilation error Pin
mpawluk30-Mar-07 12:05
mpawluk30-Mar-07 12:05 
GeneralCan't download the source code Pin
Member 214463025-Jul-05 5:06
Member 214463025-Jul-05 5:06 
Generalvideo with layout Pin
uetiean15-Jun-05 3:35
uetiean15-Jun-05 3:35 
GeneralSome f-u_k-er advertiging web site Pin
Mahfuzur Rahman.3-Jun-05 21:36
Mahfuzur Rahman.3-Jun-05 21:36 
GeneralSome f***er advertiging web site Pin
Mahfuzur Rahman.3-Jun-05 21:34
Mahfuzur Rahman.3-Jun-05 21:34 
QuestionHow can I print captured image Pin
Alexey aka inko17-May-05 1:34
Alexey aka inko17-May-05 1:34 
AnswerRe: How can I print captured image Pin
Vitaly Tomilov31-May-05 3:12
Vitaly Tomilov31-May-05 3:12 
GeneralRe: How can I print captured image Pin
Alexey aka inko31-May-05 18:31
Alexey aka inko31-May-05 18:31 
GeneralRe: How can I print captured image Pin
Vitaly Tomilov31-May-05 23:57
Vitaly Tomilov31-May-05 23:57 
GeneralRe: How can I print captured image Pin
Alexey aka inko7-Jun-05 19:38
Alexey aka inko7-Jun-05 19:38 
QuestionI can't open the image! Why? Pin
Brian2005dubai6-May-05 21:33
Brian2005dubai6-May-05 21:33 
AnswerRe: I can't open the image! Why? Pin
Vitaly Tomilov9-May-05 0:55
Vitaly Tomilov9-May-05 0:55 
GeneralRe: I can't open the image! Why? Pin
krssagar23-Aug-05 11:09
krssagar23-Aug-05 11:09 
GeneralRe: I can't open the image! Why? Pin
owen99245-Sep-06 8:12
owen99245-Sep-06 8:12 
AnswerRe: I can't open the image! Why? Pin
mpawluk30-Mar-07 12:08
mpawluk30-Mar-07 12:08 
General<d3d9.h><vmr9.h> compiling problem Pin
mmartind15-Apr-05 0:48
mmartind15-Apr-05 0:48 
GeneralRe: &lt;d3d9.h&gt;&lt;vmr9.h&gt; compiling problem Pin
phanthai24-Apr-05 18:48
phanthai24-Apr-05 18:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.