⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imageobjectdlg.cpp.bak

📁 实时视频捕获
💻 BAK
📖 第 1 页 / 共 4 页
字号:
// ImageObjectDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ImageObject.h"
#include "ImageObjectDlg.h"
#include "DSStream.h"
#include "childfrm.h"
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CImageObjectDlg dialog
long RoundFloat(float x)
{
	float intptr ;
	float fractional ;
	fractional=modf(x,&intptr);
	if(fractional<0.5)
	{
		return (long)intptr;
	}else
	{
		return((long)intptr+1);
	}
}
long RoundDouble(double x)
{
	double intptr ;
	double fractional ;
	fractional=modf(x,&intptr);
	if(fractional<0.5)
	{
		return (long)intptr;
	}else
	{
		return((long)intptr+1);
	}
}
CImageObjectDlg::CImageObjectDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CImageObjectDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CImageObjectDlg::OnCancel()
{
	DSStream_GetVideoStream(m_CardID,NULL,0);
	DSStream_DisconnectDevice( m_CardID);
	::DestroyWindow(m_lpShowVideoWnd->m_hWnd);
	::DeleteObject(m_Palette.m_hObject);
	::DeleteObject();
	::DeleteObject();
	::DeleteObject();
	::DeleteObject();
	::DeleteObject();
	::DeleteObject();
	::DeleteObject();
	::DeleteObject();
	::DeleteObject();


	delete []m_bufferCurveBlock;
	delete []m_bufferCurveHist;
	delete m_bufferVideo;
	for(int i=0;i<MAX_DETECT_REGION_NUM;i++)
	{
		delete m_RegionLightHist[i]; 
		delete m_VAverageLight[i];
	}
//	delete m_regionImageData;
	delete m_lpbmiVideo;
	delete m_lpbmiCurve;
	CDialog::OnCancel();

}
void CImageObjectDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_SLIDER_LIGHT, m_ImageLightSlider);
	DDX_Control(pDX, IDC_SLIDER_CONTRAST, m_ImageContrastSlider);
	DDX_Control(pDX, IDC_RE2_DETECT1_LIGHT, m_DetectRegion1Light);
	DDX_Control(pDX, IDC_RE2_DETECT2_LIGHT, m_DetectRegion2Light);
	DDX_Control(pDX, IDC_RE2_DETECT3_LIGHT, m_DetectRegion3Light);
	DDX_Control(pDX, IDC_RE2_DETECT4_LIGHT, m_DetectRegion4Light);
	DDX_Control(pDX, IDC_RE2_DETECT5_LIGHT, m_DetectRegion5Light);
	DDX_Control(pDX, IDC_RE2_DETECT6_LIGHT, m_DetectRegion6Light);
	DDX_Control(pDX, IDC_RE2_DETECT1_THRESHOLD, m_DetectRegion1Threshold);
	DDX_Control(pDX, IDC_RE2_DETECT2_THRESHOLD, m_DetectRegion2Threshold);
	DDX_Control(pDX, IDC_RE2_DETECT3_THRESHOLD, m_DetectRegion3Threshold);
	DDX_Control(pDX, IDC_RE2_DETECT4_THRESHOLD, m_DetectRegion4Threshold);
	DDX_Control(pDX, IDC_RE2_DETECT5_THRESHOLD, m_DetectRegion5Threshold);
	DDX_Control(pDX, IDC_RE2_DETECT6_THRESHOLD, m_DetectRegion6Threshold);
	DDX_Control(pDX, IDC_RE2_COUNTER, m_Counter);
	DDX_Control(pDX, IDC_RE2_REGION_COUNT_SET, m_SetDetectNum);
	DDX_Control(pDX, IDC_COMBO_SELECT_REGION, m_SelectDetectRegion);
	DDX_Control(pDX, IDC_RE2_REGION_HEIGHT, m_SetDetectRegionHeight);
	DDX_Control(pDX, IDC_RE2_REGION_WIDTH, m_SetDetectRegionWidth);
	DDX_Control(pDX, IDC_RE2_REGION_THRESHOLD, m_SetDetectThreshold);
	DDX_Control(pDX, IDC_BTN_SET, m_bntSetDetect);
	DDX_Control(pDX, IDC_SHOW_IMAGE, m_ShowImage);
	DDX_Control(pDX, IDC_SHOW_CURVE, m_CurveWindow);
}

BEGIN_MESSAGE_MAP(CImageObjectDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_EN_CHANGE(IDC_RE2_DETECT6_THRESHOLD, &CImageObjectDlg::OnEnChangeRe2Detect6Threshold)
	ON_BN_CLICKED(IDC_BTN_SET, &CImageObjectDlg::OnBnClickedBtnSet)
	ON_EN_CHANGE(IDC_RE2_REGION_COUNT_SET, &CImageObjectDlg::OnEnChangeRe2RegionCountSet)
	ON_EN_KILLFOCUS(IDC_RE2_REGION_COUNT_SET, &CImageObjectDlg::OnEnKillfocusRe2RegionCountSet)
	ON_CBN_SELCHANGE(IDC_COMBO_SELECT_REGION, &CImageObjectDlg::OnCbnSelchangeComboSelectRegion)
	ON_MESSAGE(UM_PROCESS_IMAGE_DATA,&CImageObjectDlg::ImageProcess)
	ON_WM_HSCROLL()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_EN_KILLFOCUS(IDC_RE2_REGION_HEIGHT, &CImageObjectDlg::OnEnKillfocusRe2RegionHeight)
	ON_EN_KILLFOCUS(IDC_RE2_REGION_WIDTH, &CImageObjectDlg::OnEnKillfocusRe2RegionWidth)
	ON_EN_KILLFOCUS(IDC_RE2_REGION_THRESHOLD, &CImageObjectDlg::OnEnKillfocusRe2RegionThreshold)
END_MESSAGE_MAP()


// CImageObjectDlg message handlers

BOOL CImageObjectDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
/////////////////////////////////////////////////////////////////////////////////
	m_CardID=0;
	m_SetEnable=FALSE;
	m_iDetectCountTotal=0;
	m_iDetectCountTemp=0;
	m_iCurrentDetectRegionID=0;
	m_CurveSpeed=0;
	m_indexofLightHist=0;
	m_RegionPosiAdjust=FALSE;
	{
		this->GetParameter();
		int k=m_SysSetPara.detect_num;
		CString str;
		for(int i=0;i<k;i++)
		{
			str.Format(_T("%d%s"),i+1,_T("号检测区域"));
			this->m_SelectDetectRegion.InsertString(i,(LPCTSTR)str);
		}
		this->m_ImageContrastSlider.SetRange(0,100);
		this->m_ImageContrastSlider.SetPos(m_SysSetPara.image_contrast);
		this->m_ImageLightSlider.SetRange(0,100);
		this->m_ImageLightSlider.SetPos(m_SysSetPara.image_light);

		this->m_SelectDetectRegion.EnableWindow(0);
		this->m_SetDetectNum.EnableWindow(0);
		this->m_SetDetectRegionHeight.EnableWindow(0);
		this->m_SetDetectRegionWidth.EnableWindow(0);
		this->m_SetDetectThreshold.EnableWindow(0);
		this->m_ImageContrastSlider.EnableWindow(0);
		this->m_ImageLightSlider.EnableWindow(0);

		this->m_SelectDetectRegion.ShowWindow(SW_HIDE );
		this->m_SetDetectNum.ShowWindow(SW_HIDE );
		this->m_SetDetectRegionHeight.ShowWindow(SW_HIDE );
		this->m_SetDetectRegionWidth.ShowWindow(SW_HIDE );
		this->m_SetDetectThreshold.ShowWindow(SW_HIDE );
		this->m_ImageContrastSlider.ShowWindow(SW_HIDE );
		this->m_ImageLightSlider.ShowWindow(SW_HIDE );
		this->m_ShowImage.SetWindowPos(NULL,5,5,IMAGE_WIDTH,IMAGE_HEIGHT,SWP_NOMOVE);
	}
/////////////////////////////////////////////////////////////////////////////////	
	
		////////////创建8bit灰阶调色板
		LPLOGPALETTE pLogPal = (LPLOGPALETTE) new char[2 * sizeof(WORD) +
																				256 * sizeof(PALETTEENTRY)];
		// 设置逻辑调色板的信息
		pLogPal->palVersion = 0x300;
		pLogPal->palNumEntries = 256;
		// 拷贝DIB中的颜色表到逻辑调色板
		for(int i = 0; i < 256; i++) {
			pLogPal->palPalEntry[i].peRed = i;
			pLogPal->palPalEntry[i].peGreen = i;
			pLogPal->palPalEntry[i].peBlue = i;
			pLogPal->palPalEntry[i].peFlags = 0;
		}
		// 创建逻辑调色板
		m_Palette.CreatePalette(pLogPal);
		// 删除临时变量并返回TRUE
		delete pLogPal;
		////////////为位图数据分配存储空间
		m_bufferCurveBlock=new BYTE[256*100];
		memset(m_bufferCurveBlock,240,256*100);

		m_bufferCurveHist=new BYTE[256*100];
		memset(m_bufferCurveHist,240,256*100);

		m_bufferVideo=new BYTE[IMAGE_WIDTH*IMAGE_HEIGHT*3];
		////////////为DIB位图信息初始化
		m_lpbmiVideo=(BITMAPINFO*)new BYTE[sizeof(BITMAPINFOHEADER)];
		m_lpbmiCurve=(BITMAPINFO *)new BYTE[sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256];


		BITMAPINFOHEADER bmi_header;
		bmi_header.biSize=40;
		bmi_header.biHeight=256;
		bmi_header.biWidth=100;
		bmi_header.biPlanes=1;
		bmi_header.biBitCount=8;
		bmi_header.biCompression=BI_RGB;
		bmi_header.biSizeImage=25600;
		bmi_header.biClrUsed=0;
		bmi_header.biClrImportant=0;
		bmi_header.biXPelsPerMeter=0;
		bmi_header.biYPelsPerMeter=0;
		
		::memcpy(m_lpbmiCurve,&bmi_header,40);
		bmi_header.biSize=40;
		bmi_header.biHeight=IMAGE_HEIGHT;
		bmi_header.biWidth=IMAGE_WIDTH;
		bmi_header.biPlanes=1;
		bmi_header.biBitCount=24;
		bmi_header.biCompression=BI_RGB;
		bmi_header.biSizeImage=IMAGE_HEIGHT*IMAGE_WIDTH*3;
		bmi_header.biClrUsed=0;
		bmi_header.biClrImportant=0;
		bmi_header.biXPelsPerMeter=0;
		bmi_header.biYPelsPerMeter=0;
		::memcpy(m_lpbmiVideo,&bmi_header,40);
		RGBQUAD *lprgb=m_lpbmiCurve->bmiColors;
		for(int i=0;i<256;i++)
		{
			lprgb[i].rgbBlue=i;
			lprgb[i].rgbGreen =i;
			lprgb[i].rgbRed =i;
			lprgb[i].rgbReserved =i;
		}
//		CDC memDC;
		CClientDC dc(this);
		HBITMAP hnewbmp=CreateDIBSection(
								dc.m_hDC ,                 // handle to DC
								m_lpbmiVideo,  // bitmap data
								 DIB_RGB_COLORS,             // data type indicator
								(void**)&m_bufferVideo,          // bit values
								NULL,         // handle to file mapping object
								0           // offset to bitmap bit values
								);
		
		
		BOOL ret=m_bmpMain.CreateBitmap(IMAGE_WIDTH,IMAGE_HEIGHT,1,24,NULL);
		m_bmpMain.DeleteObject();
		ret=m_bmpMain.Attach((HGDIOBJ)hnewbmp);
		DeleteObject((HGDIOBJ)hnewbmp);
		hnewbmp=CreateDIBSection(	dc.m_hDC ,                 // handle to DC
									m_lpbmiCurve,  // bitmap data
									 DIB_RGB_COLORS,             // data type indicator
									(void**)&m_bufferCurveBlock,          // bit values
									NULL,         // handle to file mapping object
									0           // offset to bitmap bit values
									);

		ret=m_bmpCurve.CreateBitmap(100,256,1,8,NULL);
		m_bmpCurve.DeleteObject();
		ret=m_bmpCurve.Attach((HGDIOBJ)hnewbmp);
		DeleteObject((HGDIOBJ)hnewbmp);
	
////////////////////////////////////////////////////////////////////////////	
	
//		m_regionImageData= new BYTE[MAX_REGION_WIDTH*MAX_REGION_HEIGHT];
		memset(m_regionImageData,0,MAX_REGION_WIDTH*MAX_REGION_HEIGHT);
		for(int i=0;i<MAX_DETECT_REGION_NUM;i++)
		{
			m_CurveAverage[i]=0;
			m_iRegionDetectStatu[i]=NO_OBJECT;
			m_BrightnessAverage[i]=0;
			m_RegionLightHist[i]=new BYTE[100];
				memset(m_RegionLightHist[i],0,100);
			m_VAverageLight[i]=new BYTE[MAX_REGION_WIDTH];
//			m_VAverageLight[i]=	(BYTE*)_malloca(100); 
				memset(m_VAverageLight[i],0,MAX_REGION_WIDTH);
		}
		for(int i=0;i<MAX_DETECT_REGION_NUM;i++)
		{
			CRect rtRegion=m_SysSetPara.dtr[i].region;
			m_ShowImage.ClientToScreen(rtRegion);
			this->ScreenToClient(rtRegion);
			DetectRegion[i]=rtRegion;
		}
	
////////////////////////////////////////////////////////////////////////////
	InitVideoCaptureCard();

/*
	m_lpShowVideoWnd= new CChildFrame;
	m_lpShowVideoWnd->CreateEx(WS_EX_OVERLAPPEDWINDOW ,  
										::AfxRegisterWndClass(0),                                        // standard CMDIChildWnd class
										  _T("VideoShowWnd"),                        // caption of MDI child window
										  WS_CHILD | WS_VISIBLE , // window styles
										  rectDefault ,// );                                 // default rectangle size
										  NULL,
										  1);
	m_lpShowVideoWnd->SetOwner(this);
*/	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CImageObjectDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}else
	{
//		DisplayResult();
		CDialog::OnPaint();
	}

}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CImageObjectDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CImageObjectDlg::OnEnChangeRe2Detect6Threshold()
{
	// TODO:  If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.

	// TODO:  Add your control notification handler code here
}
bool CImageObjectDlg::GetParameter()


{
	FILE * fp;
	char szFileName[20];
	//从文件读入数据
	sprintf(szFileName, "c:\\detectconfig.cfg");
	fp = fopen(szFileName, "rb");
	if(!fp)	return false;
	int sz=fread(&m_SysSetPara, sizeof(SystemSetParameter), 1, fp);
	if(sz==0)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -