131上位机VC MFC实现识别手写数字功能
131上位机VC MFC实现识别手写数字功能 功能展示 触摸屏手机基本都带手写输入功能,手写功能的实现感觉十分的高大上,我们当前例程就来实现识别手写数字功能,了解下实现手写功能的大概原理,效果如图; 要点提示 手写文字的识别可以通过模板匹配的方法,也可以记录笔划顺序进行识别的方法,方法很多,我们当前例程简单的记录用户手写文字时的笔划顺序,再对比实现数字的笔划顺序进行识别数字; 实现功能 1.新建基于对话框的应用程序 2.拖拽一图片控件用作输入面板,修改ID为IDC_PANEL,类型为矩形,关联变量Cstatic m_Panel; 拖拽一编辑框ID为IDC_EDIT1,用于显示识别结果; 拖拽一按钮关联函数OnButton2()用于执行识别功能; 添加些变量用于记录手写过程的信息 private:CPen m_ThickPen; //用粗画笔写数字 CPoint m_PrePoint;//之前的坐标点 Digit m_Figure; //数字对象 int m_CurPens; //当前笔数 3.添加鼠标点击,移动,放开的函数及识别用的自定义函数Recognize() 下面来函数体 - void CGkbc8Dlg::OnLButtonUp(UINT nFlags, CPoint point)
- {m_CurPens ++; CDialog::OnLButtonUp(nFlags, point);}
- void CGkbc8Dlg::OnLButtonDown(UINT nFlags, CPoint point)
- {
- m_PrePoint = point;
- CRect rc;
- m_Panel.GetClientRect(rc);
- if(rc.PtInRect(point))
- m_Figure.DotCount += 1;
- CDialog::OnLButtonDown(nFlags, point);
- }
复制代码- void CGkbc8Dlg::OnMouseMove(UINT nFlags, CPoint point)
- {
- CRect rc;
- m_Panel.GetClientRect(rc);
- if(nFlags&MK_LBUTTON && rc.PtInRect(point)) //如果鼠标左键按着写字时
- {
- CDC* pDC = m_Panel.GetDC();
- pDC->SelectObject(&m_ThickPen);
- pDC->MoveTo(point);
- pDC->LineTo(CPoint(point.x+1,point.y+1));
-
- if (m_CurPens>15)
- return;
- if (point.x>m_PrePoint.x+30) //向右
- {
- if (m_Figure.Direction[m_CurPens]==none)
- m_Figure.Direction[m_CurPens] = right;
- else if (m_Figure.Direction[m_CurPens] != right)
- {
- m_CurPens+=1;
- m_Figure.Direction[m_CurPens] = right;
- }
- m_PrePoint = point;
- }
- else if (point.y>m_PrePoint.y+30)
- {
- if (m_Figure.Direction[m_CurPens]==none)
- m_Figure.Direction[m_CurPens] = down;
- else if (m_Figure.Direction[m_CurPens] != down)
- {
- m_CurPens+=1;
- m_Figure.Direction[m_CurPens] = down;
- }
- m_PrePoint = point;
- }
复制代码- else if (point.x<m_PrePoint.x-30)
- {
- if (m_Figure.Direction[m_CurPens]==none)
- m_Figure.Direction[m_CurPens] = left;
- else if (m_Figure.Direction[m_CurPens] != left)
- {
- m_CurPens+=1;
- m_Figure.Direction[m_CurPens] = left;
- }
- m_PrePoint = point;
- }
- else if (point.y< m_PrePoint.y-30)
- {
- if (m_Figure.Direction[m_CurPens]==none)
- m_Figure.Direction[m_CurPens]= up;
- else if (m_Figure.Direction[m_CurPens] != up)
- {
- m_CurPens+=1;
- m_Figure.Direction[m_CurPens] = up;
- }
- m_PrePoint = point;
- }
- }
- CDialog::OnMouseMove(nFlags, point);
- }
复制代码最后用按钮调用自定义的函数Recognize();实现识别数字功能
我们来演示下功能实现的整个过程
|