Solved

WEB CAMERAS

Posted on 2000-05-11
11
5,248 Views
Last Modified: 2008-07-03
does anyone know how to operate web cameras from C code? Working Souce code would be excelent!

0
Comment
Question by:cmg05
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 1

Expert Comment

by:yz
ID: 2803227
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
 
LVL 1

Expert Comment

by:yz
ID: 2803239
Are you working under Windows platform ad using VC?

http://msdn.microsoft.com/library/psdk/multimed/avicap_8xyd.htm
0
 

Author Comment

by:cmg05
ID: 2804050
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
 
LVL 1

Expert Comment

by:yz
ID: 2804561
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
 
LVL 3

Expert Comment

by:mitrakis
ID: 2805869
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:cmg05
ID: 2807940
yz,  could you post some example code of using the vfw.h to capture an image? and save to a jpg?

Thanks
0
 
LVL 1

Accepted Solution

by:
yz earned 100 total points
ID: 2810147
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
 
LVL 3

Expert Comment

by:mitrakis
ID: 2811227
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
 

Author Comment

by:cmg05
ID: 2815203
Awsum!

Thanks man!

You got me out of a Jam.
0
 
LVL 3

Expert Comment

by:mitrakis
ID: 2829264
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
 

Expert Comment

by:anu_g_shakthi
ID: 13761544
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now