Best DirectShow way to capture image from web cam preview ? SampleGrabber is deprecated

后端 未结 3 479
礼貌的吻别
礼貌的吻别 2020-12-14 05:03

I have developed DirectShow C++ app which successfully previews web cam view into provided window. Now I want to capture image from this live web cam preview. I have used gr

3条回答
  •  -上瘾入骨i
    2020-12-14 05:49

    Listed on Microsoft's website is an example of how to capture a frame using IVMRWindowlessControl9::GetCurrentImage ... Here's one way of doing it:

    IBaseFilter*            vmr9ptr; // I'm assuming that you got this pointer already
    IVMRWindowlessControl9* controlPtr = NULL;
    
    vmr9ptr->QueryInterface(IID_IVMRWindowlessControl9, (void**)controlPtr);
    assert ( controlPtr != NULL );
    
    // Get the current frame
    BYTE*   lpDib = NULL;
    hr = controlPtr->GetCurrentImage(&lpDib);
    
    // If everything is okay, we can create a BMP
    if (SUCCEEDED(hr))
    {
        BITMAPINFOHEADER*   pBMIH = (BITMAPINFOHEADER*) lpDib;
        DWORD               bufSize = pBMIH->biSizeImage;
    
        // Let's create a bmp
        BITMAPFILEHEADER    bmpHdr;
        BITMAPINFOHEADER    bmpInfo;
        size_t              hdrSize     = sizeof(bmpHdr);
        size_t              infSize     = sizeof(bmpInfo);
    
        memset(&bmpHdr, 0, hdrSize);
        bmpHdr.bfType                   = ('M' << 8) | 'B';
        bmpHdr.bfOffBits                = static_cast(hdrSize + infSize);
        bmpHdr.bfSize                   = bmpHdr.bfOffBits + bufSize;
    
        // Builder the bit map info.
        memset(&bmpInfo, 0, infSize);
        bmpInfo.biSize                  = static_cast(infSize);
        bmpInfo.biWidth                 = pBMIH->biWidth;
        bmpInfo.biHeight                = pBMIH->biHeight;
        bmpInfo.biPlanes                = pBMIH->biPlanes;
        bmpInfo.biBitCount              = pBMIH->biBitCount;
    
        // boost::shared_arrays are awesome!
        boost::shared_array buf(new BYTE[bmpHdr.bfSize]);//(lpDib);
        memcpy(buf.get(),                       &bmpHdr,    hdrSize); // copy the header
        memcpy(buf.get() + hdrSize,             &bmpInfo,   infSize); // now copy the info block
        memcpy(buf.get() + bmpHdr.bfOffBits,    lpDib,      bufSize);
    
        // Do something with your image data ... seriously...
        CoTaskMemFree(lpDib);
    
    } // All done!
    

提交回复
热议问题