在vs2010的MFC中打开OpenCV的摄像头,并显示在picture control控件中,可是显示不了,摄像头没问题
这是代码//VideoMFCDlg.cpp:实现文件//#include"opencv2/objdetect/objdetect.hpp"#include"opencv2...
这是代码
// VideoMFCDlg.cpp : 实现文件
//#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
……
CvCapture* capture;
CRect rect;
CDC *pDC;
HDC hDC;
CWnd *pwnd;
……
BOOL CVideoMFCDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
……
// TODO: 在此添加额外的初始化代码
pwnd = GetDlgItem(IDC_STATIC);
pDC = pwnd->GetDC();
hDC = pDC->GetSafeHdc();
pwnd->GetClientRect(&rect);
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
……
void CVideoMFCDlg::OnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
if(!capture)
{
//int count = 0;
//while(count<10) count++;
capture = cvCaptureFromCAM( 0 );
if(capture) AfxMessageBox(_T("ok"));
}
if(!capture)
{
AfxMessageBox(_T("无法打开摄像头"));
return;
}
//测试..
IplImage* m_Frame;
m_Frame = cvQueryFrame( capture );
CvvImage m_CvvImage;
m_CvvImage.CopyOf(m_Frame,1);
if(true)
{
m_CvvImage.DrawToHDC(hDC,&rect);
}
//设置计时器,每10ms触发一次事件..
SetTimer(1,10,NULL);
}
void CVideoMFCDlg::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
IplImage* m_Frame;
m_Frame = cvQueryFrame(capture);
CvvImage m_CvvImage;
m_CvvImage.CopyOf(m_Frame,1);
if(true)
{
m_CvvImage.DrawToHDC(hDC,&rect);
}
CDialogEx::OnTimer(nIDEvent);
}
……
void CVideoMFCDlg::OnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
cvReleaseCapture(&capture);
CDC MemDC;
CBitmap m_Bitmap1;
m_Bitmap1.LoadBitmap(IDB_BITMAP1);
MemDC.CreateCompatibleDC(NULL);
MemDC.SelectObject(&m_Bitmap1);
pDC->StretchBlt(rect.left,rect.top,rect.Width(),rect.Height(),&MemDC,0,0,48,48,SRCCOPY);
} 展开
// VideoMFCDlg.cpp : 实现文件
//#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
……
CvCapture* capture;
CRect rect;
CDC *pDC;
HDC hDC;
CWnd *pwnd;
……
BOOL CVideoMFCDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
……
// TODO: 在此添加额外的初始化代码
pwnd = GetDlgItem(IDC_STATIC);
pDC = pwnd->GetDC();
hDC = pDC->GetSafeHdc();
pwnd->GetClientRect(&rect);
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
……
void CVideoMFCDlg::OnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
if(!capture)
{
//int count = 0;
//while(count<10) count++;
capture = cvCaptureFromCAM( 0 );
if(capture) AfxMessageBox(_T("ok"));
}
if(!capture)
{
AfxMessageBox(_T("无法打开摄像头"));
return;
}
//测试..
IplImage* m_Frame;
m_Frame = cvQueryFrame( capture );
CvvImage m_CvvImage;
m_CvvImage.CopyOf(m_Frame,1);
if(true)
{
m_CvvImage.DrawToHDC(hDC,&rect);
}
//设置计时器,每10ms触发一次事件..
SetTimer(1,10,NULL);
}
void CVideoMFCDlg::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
IplImage* m_Frame;
m_Frame = cvQueryFrame(capture);
CvvImage m_CvvImage;
m_CvvImage.CopyOf(m_Frame,1);
if(true)
{
m_CvvImage.DrawToHDC(hDC,&rect);
}
CDialogEx::OnTimer(nIDEvent);
}
……
void CVideoMFCDlg::OnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
cvReleaseCapture(&capture);
CDC MemDC;
CBitmap m_Bitmap1;
m_Bitmap1.LoadBitmap(IDB_BITMAP1);
MemDC.CreateCompatibleDC(NULL);
MemDC.SelectObject(&m_Bitmap1);
pDC->StretchBlt(rect.left,rect.top,rect.Width(),rect.Height(),&MemDC,0,0,48,48,SRCCOPY);
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询