Connect to the camera using the VFW Library in win7

Source: Internet
Author: User

The VFW Library is very useful in XP, but it is abnormal to move to Vista or win7. First, the camera device cannot be connected, and the camera display size cannot be changed.

1. hwnd m_hwndcap = capcreatecapturewindow (name, ws_visible | ws_child, left, top, width, height, hwnd, 1 );

Here, name is the name of the child form, and the second parameter is the window style. Here is the visible + word window. The following four parameters are the display position and size of the window, and hwnd is the parent window handle, the last parameter window ID. the return value is the new form handle.

2. capdriverconnect (m_hwndcap, 0). Here 0 refers to the default camera device, but if you have multiple cameras on your computer, you need to use a loop:

For (INT Index = 0; index <max_vfw_devices; index ++ ){

If (capgetdriverdescription (index, szdevicename, sizeof (szdevicename), szdeviceversion,

Sizeof (szdeviceversion ))){

Try {

If (capdriverconnect (m_hwndvideo, index )){

M_validdriverindex [m_totalvideodrivers] = index;

Capdriverdisconnect (m_hwndvideo );

}...

}

}

Here there are some differences between Windows 7 and XP. On Windows 7, capdriverconnect requires an endless loop, that is, writing capdriverconnect to while.

3. cappreviewrate (m_hwndcap, 50); set the collection frequency and the number of frames per second.

4. cappreview (hcap, true); the data is collected.

5. When the program exits, do not forget to call cappreview (hcap, false );

Capdriverdisconnect (m_hwndcap );

OK.

6. I forgot to mention it. How can we capture the image?

The answer is to use capsetcallbackonvideostream (hwnd, capvideocallback), where hwnd is the sub-form displayed on the screen, and capvideocallback is the processing function address.

Write the processing function as follows:

Lresult callback videostreamcallbackproc (hwnd, lpvideohdr lpvhdr ){

// Add your own processing code here, where the image data is stored in the structure lpvhdr-> lpdata

// While the size is stored in lpvhdr-> dwbytesused
Return true;
}

Other examples:

Connect to the vfw32.lib library and add # include "VFW. H" in the header file of the dialog box"

Double-click the preceding control to generate the corresponding response function and write the following code:

Void cusbcameradlg: onvideo ()
{
// Todo: add your control notification handler code here
// Create a window for capturewindow
Cwnd * mywnd = new cwnd;
Mywnd-> Create (_ T ("static"), "", ws_child | ws_visible, crect (250,250, 1234), this );
Mywnd-> showwindow (sw_show );
Crect rect;
Mywnd-> getwindowrect (rect );
// Create capture window
Ghcapwnd = capcreatecapturewindow ("My own capture window", ws_child | ws_visible, 1235, (rect. right-rect.left), (rect. bottom-rect.top), mywnd-> getsafehwnd );
// Connect the device
Capdriverconnect (ghcapwnd, 0 );
// Obtain parameters
Captureparms capparms;
Capcapturegetsetup (ghcapwnd, & capparms, sizeof (captureparms ));
// Set the number of frames
Capparms. flimitenabled = false;
// Whether to capture audio
Capparms. fcaptureaudio = false;
// Support for MCI Device
Capparms. fmcicontrol = false;
// Set the window. If it is set to false, the captured screen is displayed on the desktop.
Capparms. fyield = true;
// Stop the capture key settings
Capparms. vkeyabort = vk_escape;
Capparms. fabortleftmouse = false;
Capparms. fabortrightmouse = false;
Capcapturegetsetup (ghcapwnd, & capparms, sizeof (captureparms ));
// Set the preview Ratio
Cappreviewscale (ghcapwnd, 66 );
// Whether proportional change is supported
Cappreviewscale (ghcapwnd, false );
// Open Preview
Cappreview (ghcapwnd, 1 );
}

Void cusbcameradlg: oncapture ()
{
// Todo: add your control notification handler code here
Capcapturesequence (ghcapwnd );
}

Void cusbcameradlg: onstopvideo ()
{
// Todo: add your control notification handler code here
Capdriverdisconnect (ghcapwnd );
}

Void cusbcameradlg: onstopcapture ()
{
// Todo: add your control notification handler code here
Capcaptureabort (ghcapwnd );
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.