Microsoft SDK AMCap GetCurrentImage错误

时间:2016-01-02 18:59:33

标签: c++ sdk directshow msdn webcam-capture

我正在尝试修改现有的AmCap应用程序,可通过Microsoft的SDK Direct Show示例获得,以便在按下空格按钮时获取捕获的流的图像。下面是我处理空间keydown的点。

        case WM_KEYDOWN:
        if((GetAsyncKeyState(VK_ESCAPE) & 0x01) && gcap.fCapturing)
        {
            StopCapture();
            if(gcap.fWantPreview)
            {
                BuildPreviewGraph();
                StartPreview();
            }
        }
        else if ((GetAsyncKeyState(VK_SPACE) & 0x01))
        {
            IMediaControl *pMC = NULL;
            HRESULT hr = gcap.pFg->QueryInterface(IID_IMediaControl, (void **)&pMC);

            if (SUCCEEDED(hr)){
                hr=pMC->Pause();
                if (SUCCEEDED(hr)){
                    CaptureImage(TEXT("C:\\output.bmp"));
                    pMC->Run();
                    pMC->Release();
                }else
                    ErrMsg(TEXT("Failed to pause stream!  hr=0x%x"), hr);
            }
            else
                ErrMsg(TEXT("Cannot grab image"));
        }
        break;

以下是CaptureImage函数的内容。

HRESULT hr;

SmartPtr<IBasicVideo> pWC;

// If we got here, gcap.pVW is not NULL 
ASSERT(gcap.pVW != NULL);
hr = gcap.pVW->QueryInterface(IID_IBasicVideo, (void**)&pWC);

if (pWC)
{
    long bufSize;
    long *lpCurrImage = NULL;
    pWC->GetCurrentImage(&bufSize, NULL);

    lpCurrImage = (long *)malloc(bufSize);
    //

    // Read the current video frame into a byte buffer.  The information
    // will be returned in a packed Windows DIB and will be allocated
    // by the VMR.
    hr = pWC->GetCurrentImage(&bufSize, lpCurrImage);
    if (SUCCEEDED(hr))
    {
        HANDLE fh;
        BITMAPFILEHEADER bmphdr;
        BITMAPINFOHEADER bmpinfo;
        DWORD nWritten;

        memset(&bmphdr, 0, sizeof(bmphdr));
        memset(&bmpinfo, 0, sizeof(bmpinfo));

        bmphdr.bfType = ('M' << 8) | 'B';
        bmphdr.bfSize = sizeof(bmphdr) + sizeof(bmpinfo) + bufSize;
        bmphdr.bfOffBits = sizeof(bmphdr) + sizeof(bmpinfo);

        bmpinfo.biSize = sizeof(bmpinfo);
        bmpinfo.biWidth = 640;
        bmpinfo.biHeight = 480;
        bmpinfo.biPlanes = 1;
        bmpinfo.biBitCount = 32;

        fh = CreateFile(TEXT("C:\\Users\\mike\\Desktop\\output.bmp"),
            GENERIC_WRITE, 0, NULL,
            CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
        WriteFile(fh, &bmphdr, sizeof(bmphdr), &nWritten, NULL);
        WriteFile(fh, &bmpinfo, sizeof(bmpinfo), &nWritten, NULL);
        WriteFile(fh, lpCurrImage, bufSize, &nWritten, NULL);
        CloseHandle(fh);

        ErrMsg(TEXT("Captured current image to %s"),  szFile);
        return TRUE;
    }
    else
    {
        ErrMsg(TEXT("Failed to capture image!  hr=0x%x"), hr);
        return FALSE;
    }
}

return FALSE;

问题在于,当我尝试运行应用程序时,我在调用GetCurrentImage函数时收到HRESULT(0x8000ffff)错误。

另一方面,如果我通过VS调试器执行应用程序,代码工作正常。

我试图在流pMC-&gt; Pause()之后添加一个Sleep,假设问题是时间问题但是没有用。

任何想法都会非常有用!

提前谢谢。

0 个答案:

没有答案