Click here to Skip to main content
15,884,388 members
Articles / Desktop Programming / MFC
Article

Easy Digital Camera Connection

Rate me:
Please Sign up or sign in to vote.
3.19/5 (28 votes)
1 Aug 2005CPOL1 min read 128.9K   9.3K   41   16
How to connect to any digital camera?!

Introduction

If you always wanted to create an application which uses a digital camera, this article is just for you. If you know how to deal with Timers and Bitmaps, this is going to be easy. Digital cameras produce bitmap pictures. There are a few simple functions to use, so let's get started.

First Step:

Add to your project these libraries (project->settings->link): vfw32.lib, user32.lib, winmm.lib.

You must link these .lib files in order to be able to connect to the digital camera driver.

Second Step:

Include in your project, these files:

#include <vfw.h>
#include <winuser.h>
#include <Windows.h>

You must include these files in order to use the *.lib files mentioned above.

Third Step:

Use the following functions:

  • capCreateCaptureWindow and
  • capDriverConnect (hWndC, 0);

Add member variables: HWND hWndC as a capture window handle, and:

BITMAP bm;      //bitmap struct
CBitmap m_bmp; //bitmap object 

//Set a capture window.
hWndC = capCreateCaptureWindow ( "Capture Window", WS_CHILD /*|  
                                 WS_VISIBLE /*| WS_THICKFRAME */|WS_DLGFRAME
   /*|WS_EX_DLGMODALFRAME*/,
   m_rectFrame.TopLeft().x, m_rectFrame.TopLeft().y,
   320, 240, 
   GetSafeHwnd()/*hwndParent*/, 11011);
  
  if(hWndC)
     capDriverConnect (hWndC, 0); // 0 ->means default driver.
  else
  {
     AfxMessageBox("Error Cammera is not connected!");//error message box.
     exit(1);
  }
  SetTimer(1,66,NULL);   //For Preview - we sample a frame(picture) from the
                         //camera every 66 msec.

In order to make a Preview function to view a video stream on a specific CFrame rectFrame in your dialog (for example):

void CYourProject::OnTimer(UINT nIDEvent) 
{
 if(nIDEvent==1)// First Timer
 {
   capGrabFrame(hWndC); // simple macro that sample a single frame from the 
                        // camera.
   capEditCopy(hWndC);  // simple macro that edit a copy of the frame.
   OpenClipboard();     //like virtual memory.
 
   //m_hBmp is a Handle to Bitmap.
   m_hBmp = (HBITMAP)::GetClipboardData(CF_BITMAP); 
   CloseClipboard();
 
   m_bmp.Detach();       //cleaning the bitmap.
   m_bmp.Attach(m_hBmp); //connecting the bitmap throw the handle.

 
   InvalidateRect(m_rectFrame,false); // m_rectFrame is the frame that the 
                                      // video stream will be present in.
   OnPaint();            // calling to paint function to paint the bitmap 
                         // into the frame on the dialog.

 }
 CDialog::OnTimer(nIDEvent);
}

If you don't know how to paint a bitmap into a frame (CFrame), inside the OnPaint() function, you need to do:

void CCameraToolKitDlg::OnPaint() 
{
 CPaintDC dc(this); // device context for painting
 
  //....... else stuff like icon painting ect. 
 
 if(PreViewFlag)//if it is a preview call
 {
  m_bmp.GetBitmap(&bm);

  dcMem.DeleteDC();
  dcMem.CreateCompatibleDC(&dc);
  dcMem.SelectObject(&m_bmp);

  dc.StretchBlt(m_rectFrame.left, m_rectFrame.top, 
                m_rectFrame.Width(),m_rectFrame.Height(),
    &dcMem,0,0,bm.bmWidth,bm.bmHeight, SRCCOPY);
 }

 CDialog::OnPaint();
}

Now you can take a snapshot by using the following function: KillTimer(1);, and you have the picture saved in m_hBmp (handle to your Bitmap picture).

In order to choose your own camera settings: favorite resolution and pixels depth, you need to use the capDlgVideoFormat(hWndC); function like this:

void CYourProject::OnSettingsButton() 
{
   hWndC = capCreateCaptureWindow("Capture Window", 
                     WS_CHILD |/* WS_VISIBLE |*//*WS_THICKFRAME*/WS_DLGFRAME,
   m_rectFrame.TopLeft().x, m_rectFrame.TopLeft().y,
   m_rectFrame.Width(), m_rectFrame.Height(),
   GetSafeHwnd()/*hwndParent*/, 1);//Set a capture window
   capDriverConnect (hWndC, 0);//connect to cammera's driver.
   capDlgVideoFormat(hWndC); //to change the resolution and settings. 
   capDriverDisconnect(hWndC); 
}

For Help

My name is Eyal Zoref, I'm from Israel, I'm a Management and Software Engineer. View my C.V..

For Q. you can reach me at EMail_ME.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
Israel Israel
I'm a Software Engineer.

TEAM - Together Everyone Achieves More

Comments and Discussions

 
QuestioncapDriverDisconnect not working in windows 8 Pin
mekrosekaran7-Jul-14 3:27
professionalmekrosekaran7-Jul-14 3:27 
QuestionNice! Pin
Volynsky Alex8-Jan-14 3:30
professionalVolynsky Alex8-Jan-14 3:30 
QuestionShutter button Pin
genna9916-Jul-07 4:50
genna9916-Jul-07 4:50 
GeneralSweet but Pin
Muammar©17-Mar-07 22:29
Muammar©17-Mar-07 22:29 
GeneralMemory Leak Pin
yefei8-Oct-05 22:09
yefei8-Oct-05 22:09 
Generalsee how I use your code Pin
Georgi Petrov27-Jul-05 8:50
Georgi Petrov27-Jul-05 8:50 
GeneralWork without clipboard Pin
Georgi Petrov2-Jun-05 4:16
Georgi Petrov2-Jun-05 4:16 
Generali try but can not work Pin
risco27-Apr-05 3:06
risco27-Apr-05 3:06 
GeneralSpeed up the frame sampling Pin
Anonymous2-Mar-05 9:49
Anonymous2-Mar-05 9:49 
GeneralSAMPLE CODE Pin
bobkhatami25-Nov-04 7:49
bobkhatami25-Nov-04 7:49 
QuestionWhere is your code friend? Pin
Georgi Petrov15-Oct-04 5:20
Georgi Petrov15-Oct-04 5:20 
AnswerRe: Where is your code friend? Pin
Anonymous22-Oct-04 0:54
Anonymous22-Oct-04 0:54 
Generalvfw Pin
attackweasel4-Aug-04 12:09
attackweasel4-Aug-04 12:09 
Isn't vfw deprecated?
GeneralRe: vfw Pin
eyalzoref4-Aug-04 13:08
eyalzoref4-Aug-04 13:08 
General... Pin
tradnomad4-Aug-04 7:05
tradnomad4-Aug-04 7:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.