【MFC两种视频图像采集方法】DirectShow与Opencv

时间:2021-06-25 21:52:14

效果图:

【MFC两种视频图像采集方法】DirectShow与Opencv

DirectShow采集核心代码:

创建线程调用该函数,采集图像通过x264解码封装rtmp协议包,推送至FMS服务器,可实现视频直播
UINT __stdcall StartVideo(void *c)
{
CVideoConf *conf = (CVideoConf *)c;
int vid = conf->code;
CString strurl(conf->rtmpurl);
CWnd *pW = conf->pW;

CCameraDS camera;
if (!camera.OpenCamera(0,false));
{
TRACE("OpenCamera error.....");
}

while(true)
{
CvvImage cimg;
CRect rect;

IplImage *m_Frame ;
BYTE *data1 = camera.QueryFrame();
m_Frame = cvCreateImageHeader(cvSize(320,240),IPL_DEPTH_8U,3);
m_Frame->origin = 1;
cvSetData(m_Frame,data1,320*3);

CDC *pDC = pW->GetDC();
HDC hDC = pDC->GetSafeHdc();
pW->GetClientRect(&rect);
cimg.CopyOf((IplImage *)m_Frame); //复制图片
cimg.DrawToHDC(hDC, &rect); //显示指定区域
HWND hWnd = pW->GetSafeHwnd();
ReleaseDC(hWnd,hDC);

}


return 0;
}

Opencv采集核心代码:

UINT __stdcall StartVideo(void *c)
{
CVideoConf *conf = (CVideoConf *)c;
int vid = conf->code;
CString strurl(conf->rtmpurl);
CWnd *pW = conf->pW;

CCameraDS camera;
if (!camera.OpenCamera(0,false));
{
TRACE("OpenCamera error.....");
}
CvCapture* pCapture = cvCreateCameraCapture(0); //opencv打开视频
while(true)
{
CvvImage cimg;
CRect rect;

IplImage *m_Frame=cvQueryFrame( pCapture );//opencv捕获帧

CDC *pDC = pW->GetDC();
HDC hDC = pDC->GetSafeHdc();
pW->GetClientRect(&rect);
cimg.CopyOf((IplImage *)m_Frame); //复制图片
cimg.DrawToHDC(hDC, &rect); //显示指定区域
HWND hWnd = pW->GetSafeHwnd();
ReleaseDC(hWnd,hDC);

}