Video for Windows Single-Frame Capture Class Without MFC
This is a thoroughly trimmed-down version of Ken Varn's Video For Windows wrapper class (CVFWImageProcessor). I was creating a Macromedia Director Xtra that gets a live image and processes it. I needed to grab the image without using MFC because, to the best of my knowledge, MFC and MOA (Macromedia Open Architecture) are not compatible. Briefly, here's what I did in case someone wants to do it themselves.
- I replaced all MFC data types with ordinary C/C++ or Win32 data types where I could.
- I got rid of multithreading altogether because converting MFC threads to Win32 threads looked like it was going to be a major headache.
- I removed the MFC #includes.
- Finally, I took out everything else I didn't need, such as the functions for dialogs and for streaming to a file. Oh, and it doesn't seem right to call the class CVFWImageProcessor any more since it doesn't do much processing, so I call it CVFWCapture.
The demo project includes VFWCaptureTest.cpp, which shows you how to use the CVFWCapture class. You call the CaptureDIB method exactly as shown in Ken's article.
// Sample source on implemented CVFWCapture class
// to capture a single DIB frame to memory.
CVFWCapture cap;
PBITMAPINFO pBitmap;
ULONG BitmapSize;
cap.Initialize(); // Intialize first found VFW device.
pBitmap = NULL; // CaptureDIB will automatically
// allocate this if it is set to NULL.
// Capture an image from the capture device.
if (cap.CaptureDIB(&pBitmap, 0, &BitmapSize))
{
// pBitmap captured successfully. Do whatever with it.
delete pBitmap; // Delete it when done.
}
cap.Destroy(); // Done using VFW object.

Comments
hyeeee...need help here
Posted by ezat on 02/06/2011 11:40ami'm using codegear C++ builder compiler. i'm a new with c++ language. i tried to compile your code but there is an error. it show like the specific library required for the code to work. may i know the LIBRARY needed for your code? plz emel me ..i do really nid your concern... for my undergraduate final year project ... my emel : ezat2020@gmail.com really looking fwd for ur reply.. :D
ReplyResolution
Posted by hundsmiachn on 07/03/2008 08:07amHi I'm using a logitech quickcam pro9000 webcam, and I cant choose a higher res than 640x480 in the video format dialog. Is there any way of capturing with the full res of 1600x1200 with this class ??? thanks in advance regards Erich
Replymemory leak?
Posted by Armageddon421 on 12/11/2007 04:58pm-
-
-
-
-
ReplyRe: Third buffer
Posted by Walegwa on 12/18/2007 03:33amIf you don't see it, I probably imagined it--as I
Replysaid I didn't get a chance to take a good
look at your code. Also I didn't understand whether
you're saying that you're having the same
problem as before (increasing memory usage)
or there is a new problem. I suggest you comment
out most of your code and test the smallest action
your program performs. If that's not the
source of the problem, reinstate
one more action. Do this more and more until
you identify the offending portion. Sorry not to
give you more specific advice but reading other
people's code is tricky and time-consuming. All the best.
Third buffer?
Posted by Armageddon421 on 12/13/2007 09:17amWhere did I create that third one? Could you please tell me the line number ?
ReplyRe: More complext problem
Posted by Walegwa on 12/13/2007 04:54amI didn't have time to take a good look at your code,
Replybut I see that you delete two buffers, but you create
three. Is that right? By the way codeguru formats comments poorly.
It you don't use html to format them yourself
they stretch beyond the screen. And then when you want to edit,
the edit button is sometimes covered by an ad! Hopefully
someone who can do something about the problem will read this.
More complex problem
Posted by Armageddon421 on 12/12/2007 02:05pmSince I dod not need the save frame feature
Replyanymore, I just merged the stuff from the
CBmpUtils file into the main file. I'm having
a destructor for that variable, however, it is
called pBitmap in my verion. Here you can
have a look at it:
http://utilitybase.com/paste/5057
I left my calculations out because that would be
too much :P They should not be the cause of
the problem.
Please help!
Memory leak? Probably
Posted by Walegwa on 12/12/2007 03:41amThe captured bitmap resides in a CBmpUtils object, and I did not put any code in the destructor for that class. So I think therein lies the problem. See whether the following change in bmpUtils.cpp does the trick:
CbmpUtils::~CbmpUtils() { if(pbmi) delete pbmi; // or should this be delete[]?? }ReplyVFW in another Windows thread?
Posted by Henew on 02/09/2007 03:29amHi! Im working on a project which needs a camera preview in a child window. Im using a CapCreateCaptureWindow function from vfw library. Everything works, but it makes application work really slowly. I wanted to create another thread (_createthread...) in application to operate previewing. In the thread's function there is : capGrabFrame(hwndC)only; anyway it acts the same like without the thread. And when i'm creating capture window in a thread (not like before in WM_CREATE event) the window doesnt even appears. Can you help me with it? Big thanks!
-
ReplyRe: VFW in another Windows thread?
Posted by Walegwa on 02/09/2007 03:47amI think you should use DirectShow which to the best of my knowledge handles multithreading automatically. I have a somewhat outdated article on it, but the demo does work and you can start from there and then investigate further. The link is http://www.codeguru.com/cpp/g-m/multimedia/video/article.php/c9551
ReplyLost focus?
Posted by prompete on 09/14/2006 09:18amWhy even your example the main window loose the focus? Is possible to avoid this "side effect"?
-
ReplyRe: Lost focus?
Posted by Walegwa on 09/15/2006 04:48amHi. Look up the SetFocus function. It should fix that.
ReplyHow TO - Capturing two or more webcams?
Posted by luizmenezesjr on 03/16/2006 12:46amHi, I'm using Borland C++ Builder and your works runs pretty smootly... But I'm working with two or more webcams, and I need to choose wich webcam will be captured... How can I do this? I don't want to use Directx... With Netmeeting we can choose the webcam source from the Video For Windows Dialog configuration... Thanks in advance...
-
Reply
-
Replychoose webcam
Posted by alch32 on 06/19/2006 07:46pmJust a remark
Posted by luizmenezesjr on 03/16/2006 12:57amAfter I've posted my question, I've tryied to create TWO objects of the CbmpUtils class. When the second object is started, an "Choose Source" dialog open, then I can select my second webcam. Now I'm trying to select it automatically, because in my Project I will capture about 10 webcams... And choose all those webcams at every run of the program will be very anoying and time consuming.. :)
Replyhow to capture 8 bit gray image
Posted by er_john on 02/01/2006 04:23pmHi how can we capture 8 bit gray image? this code works for 24 bit image thanks
-
ReplyRe: how to capture 8 bit gray image
Posted by Walegwa on 02/01/2006 05:53pmAfter the image is captured you can calculate the gray value for each pixel with this formula
(( r * 299 ) + ( g * 587 ) + ( b * 114 )) / 1000
Hope that helps.
ReplyAbout the Xtra
Posted by zsunshine on 02/17/2005 01:56amI am also doing a similar project as yours. The aim is to create a xtra which can capture the image from a usb camera in director. However, I am new to the Xtra programming. So could you please give me an example of your work? Thanks a lot!
-
ReplyAbout the Xtra
Posted by Walegwa on 02/17/2005 08:19pmIf you haven't already, get familiar with the DrAccess example xtra that comes with the XDK, particularly the readMedia function. It reads a bitmap from file and can be changed to get it from a camera using CVFWCapture. I can send you some of my code by replying to you if you send me email.
ReplyHow to get the program started?
Posted by Udayshankarc on 10/07/2004 03:58amThe program looks excellent.But when i run the application/program only a window with title WebCam Capture appears. The web cam is not detected and nor the image is captured. At this point i don't what to do? I use a Logitech webcam. What I need to add or modify the program, for it to capture the image from the webcam. Please help me out in solving this problem. Waiting for ur reply. Thanks, Uday
-
ReplyRe: How to get the program started?
Posted by Walegwa on 10/08/2004 06:17pmI don't really have enough informaiton to answer your question (nor can I since I'm not there where you are :). But one scenario I've encountered is that when you have two programs that use the camera and you run one and close it, it might not disconnect the video driver and so when you run the other the connection fails. Does your camera work with other programs? If so, try running this program before those others. I don't think you need to modify the program since you're not even connecting to the driver. Once you connect, the capture should work. (Of course, make sure the camera is plugged in :)
ReplyMFC and MOA
Posted by cbrown on 06/19/2004 06:14pmNothing against your approach;but you might be interested in the article at: http://www.printomatic.com/mfcxtras.cfm which discusses how to mix MOA and MFC (mmmm... XTRAlicious...) Cheers - chas
-
ReplyMFC and MOA
Posted by Walegwa on 07/26/2004 12:07amYes, I'm aware of the printomatic article. Other Xtra developers who tried it said it didn't work anymore at the time I wrote the article. Also, I wrote the article, not for Xtra developers, but for a lot of other people who were requesting my class after I added a comment to Ken's article saying I had an MFC-free version. I think these people were users of other compilers, e.g., Borland. And now, as you might already know, Macromedia has fixed the problem in their latest XDK (came out this summer) and include instructions in the readme file on how to prepare to use MFC.
ReplyLoading, Please Wait ...