C# vs2012实现 ——手写识别
大家下午好啊~ 今天给大家带来一个小功能实现----手写识别
感兴趣的帅哥美女们可以看看哦~
欢迎评论区留言~~/花花/
1、 创建 C#桌面应用程序(手写识别主窗体如下)
2、 添加控件:
窗体文本(Text):
PictureBox(name:ink_here):用于设置手写区域
TextBox:显示识别出的文字
Button1:设置手写笔的颜色
Button2:手写完成后点击时,识别文字并显示在文本框中
Button3:清空手写内容,重新书写
Button4:清空文本框的内容
ColorDialog:显示手写笔颜色的控件
3、 添加引用程序集:
引用右键→添加引用→浏览→浏览→选择相应程序集(Microsoft.Ink.dll)
→确定
4、 在代码中,引入命名空间
using Microsoft.Ink;
5、 定义成员变量**
InkCollector ic;//捕获墨迹,捕获鼠标画出的墨迹,使用步骤1、添加外部程序集2、引入命
名空间
RecognizerContext rct;//启用执行墨迹 识别、检索识别结果及检索备选项的功能
string FullCACText;//保存识别的字符串
6、 窗体加载事件
private void Form1_Load(object sender, EventArgs e)
{
//初始化捕获墨迹对象,并制定捕获区域
ic = new InkCollector(ink_here.Handle);
this.ic.Stroke += new InkCollectorStrokeEventHandler(ic_Stroke);//生成事件
ic.Enabled = true;
ink_();//指定捕获后转换成为的语言
this.rct.RecognitionWithAlternates += new
RecognizerContextRecognitionWithAlternatesEventHandler(rct_RecognitionWithAlternate
s);
rct.RecognitionFlags = RecognitionModes.WordMode;
rct.Strokes = ic.Ink.Strokes;
}
this.ic.Stroke的事件
void ic_Stroke(object sender, InkCollectorStrokeEventArgs e)
{
try
{
rct.StopBackgroundRecognition();
rct.Strokes.Add(e.Stroke);
rct.CharacterAutoCompletion = RecognizerCharacterAutoCompletionMode.Full;
rct.BackgroundRecognizeWithAlternates(0);
}catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
ink_();方法
void ink_()
{
Recognizers recos = new Recognizers();//语言识别
Recognizer chineseRecoe = recos.GetDefaultRecognizer();
rct = chineseRecoe.CreateRecognizerContext();
}
this.rct.RecognitionWithAlternates的事件
void rct_RecognitionWithAlternates(object sender,
RecognizerContextRecognitionWithAlternatesEventArgs e)
{
string ResultString = "";
RecognitionAlternates alts;
if (e.RecognitionStatus == RecognitionStatus.NoError)
{
alts = e.Result.GetAlternatesFromSelection();
foreach (RecognitionAlternate alt in alts)
{
ResultString = ResultString + alt.ToString() + " ";
}
}
FullCACText = ResultString.Trim();
Control.CheckForIllegalCrossThreadCalls = false;
textBox1.Text = FullCACText;
返回识别字符处理(FullCACText);
Control.CheckForIllegalCrossThreadCalls = true;
}
返回识别字符处理(FullCACText);
void 返回识别字符处理(string 字符)
{
string[] str_temp = 字符.Split(' ');
string str_temp1 = "shibie_";
string str_temp2 = "";
if (str_temp.Length > 0)
{
for (int i = 0; i < str_temp.Length; i++)
{
str_temp2 = str_temp1 + i.ToString();
Control[] con_temp = Controls.Find(str_temp2, true);
if (con_temp.Length > 0)
{
((Button)con_temp[0]).Text = str_temp[i];
}
}
}
}
7、 Button1(设置手写笔的颜色) 的单击事件的代码
colorDialog1.FullOpen = true;
colorDialog1.ShowDialog();
ic.DefaultDrawingAttributes.Color = colorDialog1.Color;//设置手写笔颜色
8、 Button2(手写完成后点击时,识别文字并显示在文本框中) 的单击事件的代码
textBox1.SelectedText = ic.Ink.Strokes.ToString();
9、 Button3 (清空手写内容,重新书写)的单击事件的代码
if (!ic.CollectingInk)
{
Strokes strokesToDelete = ic.Ink.Strokes;
rct.StopBackgroundRecognition();
ic.Ink.DeleteStrokes(strokesToDelete);
rct.Strokes = ic.Ink.Strokes;
ic.Ink.DeleteStrokes();
ink_here.Refresh();
}
10、 Button4 (清空文本框的内容)的单击事件的代码
this.textBox1.Text = "";
11、运行以及更换水墨颜色(嘿嘿 大家不要嫌弃我字丑)
以上步骤即可完成手写识别功能啦~
欢迎大家评论区留言哦~
最后祝大家新年快乐!!!!万事顺意!!!!