Virtual Developer Workshop: Containerized Development with Docker

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.

  1. I replaced all MFC data types with ordinary C/C++ or Win32 data types where I could.
  2. I got rid of multithreading altogether because converting MFC threads to Win32 threads looked like it was going to be a major headache.
  3. I removed the MFC #includes.
  4. 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;
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.

About the Author

Audrey Mbogho

I am a senior lecturer of computer science at the University of Cape Town and I program in Java now.



  • hyeeee...need help here

    Posted by ezat on 02/06/2011 07:40pm

    i'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

  • Resolution

    Posted by hundsmiachn on 07/03/2008 03:07pm

    Hi 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

  • memory leak?

    Posted by Armageddon421 on 12/12/2007 12:58am

    I modified the programm to continuously capture frames and display them. However, after a while my ram fills up. The strange thing about it is that the programm itself takes only 17 mb in the processes list, but my overall ram usage increases steadily until I exit that programm.
    If I look at the ram usage of your example without changes, the ram usage increases by about 400bk everytime I grab a new frame. I don't think I've made any mistakes, but I really can't find anything wrong. Please help!

    • Re: Third buffer

      Posted by Walegwa on 12/18/2007 11:33am

      If you don't see it, I probably imagined it--as I
      said 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 05:17pm

      Where did I create that third one? Could you please tell me the line number ?

    • Re: More complext problem

      Posted by Walegwa on 12/13/2007 12:54pm

      I didn't have time to take a good look at your code,
      but 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 10:05pm

      Since I dod not need the save frame feature
      anymore, 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:

      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 11:41am

      The 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:

      		delete pbmi; // or should this be delete[]??

  • VFW in another Windows thread?

    Posted by Henew on 02/09/2007 11:29am

    Hi! 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!

    • Re: VFW in another Windows thread?

      Posted by Walegwa on 02/09/2007 11:47am

      I 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

  • Lost focus?

    Posted by prompete on 09/14/2006 04:18pm

    Why even your example the main window loose the focus? Is possible to avoid this "side effect"?

    • Re: Lost focus?

      Posted by Walegwa on 09/15/2006 11:48am

      Hi. Look up the SetFocus function. It should fix that.

  • How TO - Capturing two or more webcams?

    Posted by luizmenezesjr on 03/16/2006 08:46am

    Hi, 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...

    • choose webcam

      Posted by alch32 on 06/20/2006 02:46am

      i'm interested to establish netmeeting call and change web cam of control.In netmeeting can be in fly but in SDK i can't do it.I searching dessesperate this function but i dont find nothing of this can help me please?

    • Just a remark

      Posted by luizmenezesjr on 03/16/2006 08:57am

      After 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.. :)

  • how to capture 8 bit gray image

    Posted by er_john on 02/02/2006 12:23am

    Hi how can we capture 8 bit gray image? this code works for 24 bit image thanks

    • Re: how to capture 8 bit gray image

      Posted by Walegwa on 02/02/2006 01:53am

      After 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.

  • About the Xtra

    Posted by zsunshine on 02/17/2005 09:56am

    I 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!

    • About the Xtra

      Posted by Walegwa on 02/18/2005 04:19am

      If 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.

  • How to get the program started?

    Posted by Udayshankarc on 10/07/2004 10:58am

    The 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

    • Re: How to get the program started?

      Posted by Walegwa on 10/09/2004 01:17am

      I 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 :)

  • MFC and MOA

    Posted by cbrown on 06/20/2004 01:14am

    Nothing 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

    • MFC and MOA

      Posted by Walegwa on 07/26/2004 07:07am

      Yes, 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.

  • Loading, Please Wait ...

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date