Webcam Image Capture

This is a discussion on Webcam Image Capture within the Windows Programming forums, part of the Platform Specific Boards category; I am using win32 api in vc 6 and trying to get an image from my webcam but once i ...

  1. #1
    Registered User AtomRiot's Avatar
    Join Date
    Jan 2003
    Posts
    120

    Webcam Image Capture

    I am using win32 api in vc 6 and trying to get an image from my webcam but once i get this image, i want to be able to manipulate the data to convert it and use it for other purposes.

    so far my google'ing has come up with a few programs that got images from a webcam but other than that, these do a whole lot more than what i need.

    I am using vfw.h (video for windows header) and can start my camera and get an image going without a problem but am not sure where to go to get the actual picture data.

    I used the capEditCopy(camhwnd); call to send one frame to the clipboard wher i know i can paste it into programs like paint or word but it has header info and all and i could care less about the header info i just need the image data for analyzing.

    I have used teh ddcam dll to get images but like before it has more than what i need.

    if anyone can lead me into the right direction then that would be awsome.

    thanks in advance.


    here is my code so far:
    Code:
    #include <windows.h>
    #include <vfw.h>
    #include "my cam test.h"
    #pragma comment(lib, "winmm.lib")
    #pragma comment(lib, "vfw32.lib")
    then some normal win32 stuff to create teh window,

    then:

    Code:
    LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	HINSTANCE hInstance = GetModuleHandle(NULL);
    	
    	HWND hButtStartCam;
    	HWND hButtStopCam;
    	HWND hButtGetFrame;
    	
        switch (message)                  /* handle the messages */
        {
    		case WM_CREATE:
    		{
    			
    			hButtStartCam = CreateWindowEx(0,"BUTTON","Start Camera",WS_CHILD | WS_VISIBLE,
    				0,0,100,20,hwnd,(HMENU)HBUTTSTART,hInstance, 0);
    			hButtStopCam = CreateWindowEx(0,"BUTTON","Stop Camera",WS_CHILD | WS_VISIBLE,
    				0,50,100,20,hwnd,(HMENU)HBUTTSTOP,hInstance, 0);
    			hButtGetFrame = CreateWindowEx(0,"BUTTON","Get Frame",WS_CHILD | WS_VISIBLE,
    				0,25,100,20,hwnd,(HMENU)HBUTTFRAME,hInstance, 0);
    			break;
    		}
    		case WM_COMMAND:
    		{
    			switch(LOWORD(wParam))
    			{
    				case HBUTTSTART:
    				{	camhwnd = capCreateCaptureWindow ("camera window", WS_CHILD | WS_VISIBLE, 110, 0, 640, 480, hwnd, 0);
    					capDriverConnect(camhwnd,0);
    					capPreviewRate(camhwnd,300);
    					capPreview(camhwnd,TRUE);
    					break;
    				}
    				case HBUTTSTOP:
    				{
    					capDriverDisconnect(camhwnd);
    					DestroyWindow (camhwnd);
    					break;
    				}
    				case HBUTTFRAME:
    				{
    					//capGrabFrame(camhwnd);//this pauses the video feed in teh current camera hwnd
    					//capEditCopy(camhwnd);///this copies one frame to the clipboard where you can paste it in a image editor
    					//ddcamsaveimage("rawdata.bmp");
    					break;
    				}
    			}
    			break;
    		}
    	    case WM_DESTROY:
    		{
    			PostQuitMessage(0);        /* send a WM_QUIT to the message queue */
    	        break;
    		}
    	    default:                   /* for messages that we don't deal with */
    	        return DefWindowProc(hwnd, message, wParam, lParam);
        }
        return 0;
    }
    All Your Base Are Still Belong to Someone!!!
    And you Remember that!!!

  2. #2
    Registered User AtomRiot's Avatar
    Join Date
    Jan 2003
    Posts
    120
    ok, i have been doing some more google'ing and researching and it looks like there is a macro to capture the information from the webcam before it hits the screen into the hwnd your directing it too. this macro is capVideoStreamCallback and it is on msdn's site at:

    http://msdn.microsoft.com/library/de...amcallback.asp

    and it tells me it needs to have a VIDEOHDR variable set up so it can put the info and the VIDEOHDR info on msdn is here:

    http://msdn.microsoft.com/library/de...deohdr_str.asp

    and it should be found in the lpData portion of the VIDEOHDR variable.

    i tried making a new VIDEOHDR and calling that macro like this
    Code:
    ...
    VIDEOHDR vdHdr;
    ...
    capVideoStreamCallback(camhwnd, vdHdr);
    ...
    and it gives me
    Code:
    error C2065: 'capVideoStreamCallback' : undeclared identifier
    even though its dependancies are in vfw.h which i have to have included anyway and in vfw32.lib which i have pragma's and i made sure it is in the list under project -> settings then under link in the modules box it is there too but i still get this error. any ideas? i looked in my vfw.h file in the include directory vc98 and there was no function in there. "capVideoStreamCallback" was not even anywhere in the file. i searched for other callback functions but did not see any that were similar.

    does anyone have any idea on this? thanks in advance.
    All Your Base Are Still Belong to Someone!!!
    And you Remember that!!!

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    See the last few examples:
    http://msdn.microsoft.com/library/de...eo_capture.asp

    Note that the image in lpData will be in the format returned by capGetVideoFormat() or set in a call to capSetVideoFormat() or capDlgVideoFormat().

    This may help:
    http://www.google.com/search?q=Cavicap

    If you don't want to deal with the different formats and you really don't care about performance, you can use capEditCopy(), GetClipboardData() will return an HBITMAP, which you can SelectObject() into a memory DC and then use GetPixel() to get the desired pixels in RGB format.

    If you don't want to deal with the different formats and you care slightly about performance, you can copy lpData into a dib section.

  4. #4
    Registered User AtomRiot's Avatar
    Join Date
    Jan 2003
    Posts
    120
    thx i will give some of those options a try, i was really trying to stay away from the getpixel option because it would take a while (in computer time) to actually get the images and then get alll the pixels and although i do not need streaming media, i do need it to be a little faster than get one image and scan all pixels then process and hope that you do not need another image too soon kinda thing.

    i will let you know how it turns out and post my source when i get it but today i have to do some family stuff

    happy forth and god bless
    All Your Base Are Still Belong to Someone!!!
    And you Remember that!!!

  5. #5
    Registered User AtomRiot's Avatar
    Join Date
    Jan 2003
    Posts
    120
    here is some of my code to get this working.

    Code:
    capEditCopy(camhwnd);
    hdc = BeginPaint(camhwnd, &ps);
    hdcMem = CreateCompatibleDC(hdc); 
    if (hdcMem != NULL)
    {
    	if (OpenClipboard(camhwnd))
    	{		
    		hbm = (HBITMAP) GetClipboardData(CF_BITMAP);
    		SelectObject(hdcMem, hbm);
    		GetClientRect(camhwnd, &rc);
    		CloseClipboard();
    	}
    }
    COLORREF tmpColor;
    HDC newHdc;
    newHdc = GetDC(hwnd);
    int x,y;
    for(x=0;x<rc.right;x++)
    {
    	for(y=0;y<rc.bottom;y++)
    	{
    		tmpColor = GetPixel(hdcMem,x,y);
    		nImageData[(x*3)][y] = GetRValue(tmpColor);
    		nImageData[(x*3)+1][y] = GetGValue(tmpColor);
    		nImageData[2+(x*3)][y] = GetBValue(tmpColor);
    		nBwImageData[x][y] = ((GetRValue(tmpColor) + 
    				GetGValue(tmpColor) + 
    				GetBValue(tmpColor)) / 3);
    		SetPixelV(newHdc, x+110, y+242, RGB(nBwImageData[x][y], 
    			nBwImageData[x][y], nBwImageData[x][y]));
    		SetPixelV(newHdc,x+110, y+484, RGB(nImageData[(x*3)][y], 
    			nImageData[1+(x*3)][y], nImageData[2+(x*3)][y]));
    	}
    }
    in this code i just basically copy the image from teh camera using the CapEditCopy() function and put it into the clipboard. then create an image dc and empty the clipboard into it. then i traverse the hdc one pixel at a time to get the image and store it in black and white and in color then display those images.

    this method actually wasnt too processor consuming so i probably will stick with it.

    if anyone has any shorter versions or wants to see more of the whole code then just ask and i can post it.

    thanks again for all the help and ideas.

    oh and this hunk of code is found inside the HBUTTFRAME case in the WM_COMMAND section of my code, just to clarify
    Last edited by AtomRiot; 07-13-2004 at 09:53 PM.
    All Your Base Are Still Belong to Someone!!!
    And you Remember that!!!

  6. #6
    Registered User
    Join Date
    May 2007
    Posts
    16
    Could you post all of your code for me? I'm working on a program and I just need to get bitmap images. But the camera I'm using can take images at 1024x768, would I be able to get an image of this quality? I'm using Dev C++.
    Thanks!
    Blair

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    3-year thread bump.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem reading tiff image files?
    By compz in forum C++ Programming
    Replies: 9
    Last Post: 10-30-2009, 05:17 AM
  2. webcam image capture
    By halfbrain in forum C Programming
    Replies: 1
    Last Post: 05-11-2008, 10:29 AM
  3. memory usage keeps going up when retrieving webcam image
    By khristopher in forum C++ Programming
    Replies: 2
    Last Post: 03-22-2006, 09:19 PM
  4. Window Image Capture
    By Lazy Student in forum Windows Programming
    Replies: 1
    Last Post: 02-17-2005, 08:06 AM
  5. WebCam Capture, and display
    By simly01 in forum Windows Programming
    Replies: 1
    Last Post: 06-26-2002, 01:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21