First Steps C++

Select a Video Capture Device and display a Live Image

Open the class view in the project explorer and right click on the "CFirstStepView" class. Select "Properties" from the context menu and click on the "Overrides" button. Now add the OnInitialUpdate method.

image

To select a video capture device, use the built-in device selection dialog. This is done in the OnInitialUpdate method of the CFirstStepView class. Change its code as follows:

void CFirstStepView::OnInitialUpdate()
{
    CView::OnInitialUpdate();

    // TODO: Add your specialized code here and/or call the base class
    // Get the grabber from the document class.
    DShowLib::Grabber* pGrabber = GetDocument()->m_pGrabber;

    // Show the device page.
    pGrabber->showDevicePage();
    // Check if there is a valid device.
    if( pGrabber->isDevValid() )
    {
        // Set the window that should display the live video.
        pGrabber->setHWND( m_hWnd );
        // Start the live video.
        pGrabber->startLive();
    }
    else
    {
        AfxMessageBox( TEXT("No device was selected.") );
    }
}

Before the application terminates, the live video should be stopped and the device should be closed. To do so, add the OnCloseDocument method. Open the class view in the project explorer and right click on the "CFirstStepDoc" class. Select "Properties" from the context menu and click on the "Overrides" button. Now add the OnCloseDocument method.

image

Insert the following code into the OnCloseDocument method:

void CFirstStepDoc::OnCloseDocument()
{
    // TODO: Add your specialized code here and/or call the base class
    // Stop live mode.
    m_pGrabber->stopLive();

    // this call will also succeed if no device is open
    m_pGrabber->closeDev();

    CDocument::OnCloseDocument();
}

Now compile and run the project. After a device has been selected in the device selection dialog, a live image will be displayed.