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;
CBitmap m_bmp;
hWndC = capCreateCaptureWindow ( "Capture Window", WS_CHILD |WS_DLGFRAME
,
m_rectFrame.TopLeft().x, m_rectFrame.TopLeft().y,
320, 240,
GetSafeHwnd(), 11011);
if(hWndC)
capDriverConnect (hWndC, 0);
else
{
AfxMessageBox("Error Cammera is not connected!");
exit(1);
}
SetTimer(1,66,NULL);
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)
{
capGrabFrame(hWndC);
capEditCopy(hWndC);
OpenClipboard();
m_hBmp = (HBITMAP)::GetClipboardData(CF_BITMAP);
CloseClipboard();
m_bmp.Detach();
m_bmp.Attach(m_hBmp);
InvalidateRect(m_rectFrame,false);
OnPaint();
}
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);
if(PreViewFlag)
{
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_DLGFRAME,
m_rectFrame.TopLeft().x, m_rectFrame.TopLeft().y,
m_rectFrame.Width(), m_rectFrame.Height(),
GetSafeHwnd(), 1);
capDriverConnect (hWndC, 0);
capDlgVideoFormat(hWndC);
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.