• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5348
  • Last Modified:

WEB CAMERAS

does anyone know how to operate web cameras from C code? Working Souce code would be excelent!

0
cmg05
Asked:
cmg05
  • 4
  • 3
  • 3
  • +1
1 Solution
 
yzCommented:
Please check the part about Video Capture in MSDN, the functions and messages of capture window can help you control the preview and capture progress of camera.
For example:
capCreateCaptureWindow to create a capture window,
caoDriverConnect to connect to a camera device,
capPreviewRate and capPreviewScale to set the preview preference,
capPreview or capOverlay to begin prview,
capFileSetCaptureFile to set file name for capture.
capCaptureSequence to begin capture and store to a AVI file.

0
 
yzCommented:
Are you working under Windows platform ad using VC?

http://msdn.microsoft.com/library/psdk/multimed/avicap_8xyd.htm
0
 
cmg05Author Commented:
Writing for a PC, on 98 using VC.

should I already have the avi class + h files the site talks off?

can I download them?

are there java equiv.?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
yzCommented:
Yes, these functions, actrually they are macros, are defined in vfw.h, and use vfw32.lib, these two files were already contained in VC or Platform SDK, so you can use it directly.

Why you talk about java? no idea.

If you need some example, I can show you, good luck.
0
 
mitrakisCommented:
Maybe I misunderstood something...
Do you want to remotely control/operate a webcam or just grab live images from it ?

In case you're talking of a remote control, let me know.
In case you want to fiddel around with video/images/frames yz's suggestion using VFW is right.

Here's my example on a remote controlled pan/tilt camera:
http://bvlab12.fh-reutlingen.de

Best regards
-Stavi-
0
 
cmg05Author Commented:
yz,  could you post some example code of using the vfw.h to capture an image? and save to a jpg?

Thanks
0
 
yzCommented:
You can use Video Capture Window to get the current frame, but it's bitmap, but can not compress it to jpeg.

The program below shows how to use Video Capture Window.

I think convert bitmap to jpeg is not hard, you may search it at codeguru, there should be an answer. sorry, I cannot help you any futher.

#include <windows.h>
#include <vfw.h>
#include "resource.h"

HINSTANCE ghInstance;
HWND ghMainWnd, ghCapWnd;
HMENU ghMainMenu;
BOOL fConnected = FALSE;

void ProcessCommand(HWND hwnd, WPARAM wParam, LPARAM lParam)
      {
      BOOL fResult;
      CAPDRIVERCAPS sCaps;

      switch (LOWORD(wParam))
            {
            // Connect to your camera, 0 means the first one,
            // if you have more than one device, change it to correct number.
            case IDM_CONNECT:
                  fResult = capDriverConnect(ghCapWnd, 0);
                  if (0 == fResult)
                        MessageBox(hwnd, "Cannot open video device", "Error", MB_OK);
                  else
                        {
                        fConnected = true;

                        capDriverGetCaps(ghCapWnd, &sCaps, sizeof(sCaps));

                        EnableMenuItem(ghMainMenu, IDM_CONNECT, MF_GRAYED);
                        EnableMenuItem(ghMainMenu, IDM_DISCONNECT, MF_ENABLED);
                        EnableMenuItem(ghMainMenu, IDM_PREVIEW, MF_ENABLED);
                        EnableMenuItem(ghMainMenu, IDM_GRAB, MF_ENABLED);
                        EnableMenuItem(ghMainMenu, IDM_CAPTURE, MF_ENABLED);
                        }
                  break;

            case IDM_DISCONNECT:
                  fResult = capDriverDisconnect(ghCapWnd);

                  EnableMenuItem(ghMainMenu, IDM_CONNECT, MF_ENABLED);
                  EnableMenuItem(ghMainMenu, IDM_DISCONNECT, MF_GRAYED);
                  EnableMenuItem(ghMainMenu, IDM_PREVIEW, MF_GRAYED);
                  EnableMenuItem(ghMainMenu, IDM_GRAB, MF_GRAYED);
                  EnableMenuItem(ghMainMenu, IDM_CAPTURE, MF_GRAYED);

                  fConnected = FALSE;
                  break;

            case IDM_VIDEOSOURCE:
                  fResult = capDlgVideoSource(ghCapWnd);
                  break;

            case IDM_VIDEOFORMAT:
                  fResult = capDlgVideoFormat(ghCapWnd);
                  break;

            case IDM_VIDEODISPLAY:
                  fResult = capDlgVideoDisplay(ghCapWnd);
                  break;

            case IDM_PREVIEW:
                  fResult = capPreviewRate(ghCapWnd, 33);
                  fResult = capPreviewScale(ghCapWnd, TRUE);
                  fResult = capPreview(ghCapWnd, TRUE);
                  break;

            // save current frame to c:\\temp\\test.bmp
            case IDM_GRAB:
                  capFileSaveDIB(ghCapWnd, "c\\test.bmp");
                  break;

            // If capture is in process, press mouse button or key will stop it by default
            case IDM_CAPTURE:
                  fResult = capFileSetCaptureFile(ghCapWnd, "c:\\temp\\test.avi");
                  fResult = capCaptureSequence(ghCapWnd);
                  break;

            case IDM_EXIT:
                  PostMessage(ghMainWnd, WM_CLOSE, 0, 0);
                  break;
            }
      
      return;
      }

LRESULT CALLBACK MainWndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
      {
      switch (iMsg)
            {
            case WM_CREATE:
                  break;

            case WM_COMMAND:
                  ProcessCommand(hwnd, wParam, lParam);
                  break;

            case WM_DESTROY:
                  if (fConnected)
                        {
                        capDriverDisconnect(ghCapWnd);
                        fConnected = FALSE;
                        }
                  PostQuitMessage(0);
                  break;

            default:
                  return DefWindowProc(hwnd, iMsg, wParam, lParam);
            }
      return 0;
      }

BOOL InitApplication (HINSTANCE hInstance)
      {
      WNDCLASSEX wc;
      BOOL fSuccess;

      ghInstance = hInstance;

      wc.cbSize        = sizeof (wc) ;
    wc.style         = 0;
    wc.lpfnWndProc   = MainWndProc ;
    wc.cbClsExtra    = 0 ;
    wc.cbWndExtra    = 0 ;
    wc.hInstance     = hInstance ;
    wc.hIcon           = LoadIcon (NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor (NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MENU1);
    wc.lpszClassName = "TestCapture";
    wc.hIconSm       = LoadIcon (NULL, IDI_APPLICATION);

    fSuccess = RegisterClassEx (&wc);

      return fSuccess;
      }

BOOL InitInstance (HINSTANCE hInstance, int iCmdShow)
      {
      RECT rect;

      ghMainWnd = CreateWindowEx (0, "TestCapture", "Test Capture Application", WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU | WS_VISIBLE | WS_CAPTION,
                  CW_USEDEFAULT, CW_USEDEFAULT, 400, 350, NULL, NULL, hInstance, NULL);

      ghMainMenu = GetMenu(ghMainWnd);

      GetClientRect(ghMainWnd, &rect);

      ghCapWnd = capCreateCaptureWindow("Test Capture Window", WS_CHILD | WS_VISIBLE, rect.left + 5, rect.top + 5, rect.right - 10, rect.bottom - 10, ghMainWnd, 1);

    ShowWindow (ghMainWnd, iCmdShow);
    UpdateWindow (ghMainWnd);

      return true;
      }

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
      {
      MSG msg;

      if (!InitApplication(hInstance) || !InitInstance(hInstance, iCmdShow))
            return false;

      while(GetMessage (&msg, NULL, 0, 0))
        {
            TranslateMessage (&msg) ;
        DispatchMessage (&msg) ;
        }

      return 1;
      }
0
 
mitrakisCommented:
If it's useful to have complete source code regarding capturing, grabbing and usage of VfW in general I'd suggest to visit the following site:

http://i.am/shrinkwrapvb

Although it's VB code I'm sure it can help you solve the problem...somehow.
Not to forget that there's an OCX available to include it into C++ projects.

Best regards
-Stavi-
0
 
cmg05Author Commented:
Awsum!

Thanks man!

You got me out of a Jam.
0
 
mitrakisCommented:
Hi cmg05,

did you solve the JPG saving ?
I would greatly appreciate it if you drop me a note on this.

I would be thankful forever, if you even send me your working project...

mailto: s.mitrakis@caa.de

Best regards
-Stavi-
0
 
anu_g_shakthiCommented:
Hi,
I have a Sony Handycam (DCR-TRV340E); I would like to know how to control/operate this camera through remote. Basically I want to do this through RF. Kindly suggest.

Regards
Ganesh
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now