Click here to Skip to main content
Licence CPOL
First Posted 15 Apr 2004
Views 362,885
Bookmarked 122 times

Simultaneous Previewing & Video Capture using DirectShow

By | 15 Apr 2004 | Article
A method for previewing and framegrabbing simultaniously from webcam using DirectShow.

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)

About the Author

krssagar

Software Developer (Senior)

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHow to save captured inage as bmp format in hard disk. and how to retrive it? PinmemberLu Maw17:03 9 Mar '11  
GeneralMy vote of 4 Pinmembermr colorbooks14:23 23 Dec '10  
QuestionHow to review the capture after any window cover and take out??? Pinmembergopalyap18:03 13 Aug '09  
Generalbmp is flipped vertically Pinmemberwyhzaf32020:54 4 Aug '09  
GeneralCrash on line: hr = m_pGB->Render(m_pCamOutPin); Pinmemberdescartes11:21 24 Mar '09  
GeneralFlickerless problem PinmemberQuyLV20:57 17 Feb '09  
GeneralThanX [modified] Pinmemberangello0:07 5 Jan '09  
GeneralIVMRWindowlessControl9::GetCurrentImage, Excessive use of this function will degrade video playback performance. Pinmemberyouhow200016:28 21 Dec '08  
GeneralRe: IVMRWindowlessControl9::GetCurrentImage, Excessive use of this function will degrade video playback performance. Pinmemberangello0:12 5 Jan '09  
QuestionBmp is flipped horizontally PinmemberMember 24354792:25 12 Nov '08  
AnswerRe: Bmp is flipped horizontally PinmemberQuyLV20:41 17 Feb '09  
GeneralRe: Bmp is flipped horizontally PinmemberKon1:21 28 Oct '10  
GeneralRTP sender/receiver DirectShow filter. PinmemberhINTModuleState5:56 18 Aug '08  
GeneralRe: RTP sender/receiver DirectShow filter. Pinmemberangello0:16 5 Jan '09  
Generaldirectx 9.0 Pinmemberani_ikram18:29 30 Jul '08  
GeneralCOMPILE ERROR PinmemberCollin Xie19:18 7 May '08  
GeneralRe: COMPILE ERROR PinmemberhINTModuleState6:00 18 Aug '08  
GeneralColor adjustment Pinmemberrenoreballos4:08 4 Jan '08  
Generalhi i have some question Pinmembermocderfd21:52 29 Nov '07  
QuestionCapture Image to Bitmap Pinmemberzecapt23:25 11 Oct '07  
AnswerRe: Capture Image to Bitmap [modified] Pinmemberprda2:37 31 Oct '07  
QuestionImageCapture() error Pinmemberkey_466:01 15 Sep '07  
Generaldshow not in directory??helpme.. Pinmemberjefryn17:54 15 Aug '07  
Generaltoo slow Pinmemberwharry84:02 19 Jul '07  
just the "m_pWC->GetCurrentImage(&lpCurrImage)" in CVMR_Capture::GrabFrame() will spend more than 100ms/f.

QuestionSave picture in picture video streams to avi file [modified] Pinmemberlonglac9:31 6 Mar '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120529.1 | Last Updated 16 Apr 2004
Article Copyright 2004 by krssagar
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid