DirectShow实现视频的实时显示并抓图,可以设置视频参数
2021-05-11
577
0
DirectShow抓图其实是使用IBasicVideo(IID_IBasicVideo)接口获取数据源,进行存储即可。
IGraphBuilder* m_pGraphBuild = NULL;
hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC,IID_IGraphBuilder, (void**)&m_pGraphBuild);
完整代码如下:
void CCamera::CapturePicture(char* pFileName)
{
IBasicVideo* pBasicVideo = NULL;
m_pGraphBuild->QueryInterface(IID_IBasicVideo, (void**)&pBasicVideo);
long bitmapSize;
char* buffer;
HRESULT hr = pBasicVideo->GetCurrentImage(&bitmapSize, 0);
if (SUCCEEDED(hr))
{
buffer = new char[bitmapSize];
hr = pBasicVideo->GetCurrentImage(&bitmapSize, (long*)buffer);
BITMAPFILEHEADER hdr; //定义位图变量
LPBITMAPINFOHEADER lpbi; //是个指针,指向BMP头的
lpbi = (LPBITMAPINFOHEADER)buffer;
int nColor = 0;
if (lpbi->biBitCount <= 8)
{
nColor = 1 << lpbi->biBitCount;
}
// Fill in the fields of the file header
hdr.bfType = ((WORD)('M' << 8) | 'B');// 位图类别is always "BM"
hdr.bfSize = bitmapSize + sizeof(hdr); //BMP图像文件的大小
hdr.bfReserved1 = 0; //总为0
hdr.bfReserved2 = 0; //总为0
hdr.bfOffBits = (DWORD)(sizeof(BITMAPFILEHEADER) + //BMP图像数据的地址
lpbi->biSize + nColor * sizeof(RGBQUAD));
FILE* fp = fopen("1.bmp", "wb");
fwrite(&hdr, sizeof(BITMAPFILEHEADER),1,fp);
fwrite(buffer, bitmapSize, 1, fp);
fclose(fp);
}
}
HID人机交互QQ群:564808376
UAC音频QQ群:218581009
UVC相机QQ群:331552032
BOT&UASP大容量存储QQ群:258159197
STC-USB单片机QQ群:315457461
USB技术交流QQ群2:580684376
USB技术交流QQ群:952873936