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

📄 formmain.cs

📁 ocr samples for use with .net c#
💻 CS
📖 第 1 页 / 共 2 页
字号:
						else
						{
							bool1stScan = false;
							ax = i;
							bx = i;
							ay = j;
							by = j;
						}
					}
					b ++;
				}                               			
			
			this.arrCurrentEigenvalue.Clear();
			this.arrCurrentEigenvalue.TrimToSize();
			
			if((bx-ax)!=0 && (by-ay)!=0 && (((bx-ax)>(userDrawAreaBmp.Width/this.SplitWidth)/2) || ((by-ay)>(userDrawAreaBmp.Height/this.SplitHeight)/2)))
			{
				this.ClearGraphics(this.userTemplateArea.Image,Color.Black);

				Bitmap memBmp = new Bitmap(userTemplateAreaBmp.Width,userTemplateAreaBmp.Height,System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
				Graphics memGp = Graphics.FromImage(memBmp);
				memGp.DrawImage(userDrawAreaBmp,new Rectangle(0,0,memBmp.Width-1,memBmp.Height-1),ax,ay,bx-ax,by-ay,GraphicsUnit.Pixel);

				int pw = (userDrawAreaBmp.Width/this.SplitWidth) ;
				int ph = (userDrawAreaBmp.Height/this.SplitHeight);

				for(int i=0;i<memBmp.Width;i+=(memBmp.Width/this.SplitWidth))
					for(int j=0;j<memBmp.Height;j+=(memBmp.Height/this.SplitHeight))
					{
						if(memBmp.GetPixel(i,j).ToArgb()==Color.White.ToArgb())
						{
							g.FillRectangle(new SolidBrush(Color.Red),i-(pw /2),j-(ph /2),pw,ph);
							g.DrawRectangle(new Pen(Color.Yellow,1f),i-(pw /2),j-(ph /2),pw,ph);
							arrCurrentEigenvalue.Add("1");
						}
						else
						{
							arrCurrentEigenvalue.Add("0");
						}
					}
				memGp.Dispose();
				memBmp.Dispose();
			}
			//textResult.Text = this.arrCurrentEigenvalue.Count.ToString();
			g.Dispose();
			userTemplateArea.Refresh();
		}

		/// <summary>
		/// 保存学习结果
		/// </summary>
		private void SaveLearn(string LearnCharacter)
		{
			if(arrCurrentEigenvalue.Count<=0)
			{
				return;
			}

			//建立特征码
			string Eigenvalue = "";
			foreach(string ce in this.arrCurrentEigenvalue)
				Eigenvalue += ce;

			try
			{
				DbCmd.CommandText = string.Format("select Count(*) as exp1 from tbLearn where LearnCharacter = '{0}'",LearnCharacter.Replace("'","''"));
				OleDbDataReader ddr = DbCmd.ExecuteReader();

				bool isUpdate = false;
				while(ddr.Read())
				{
					if(ddr.GetInt32(0)!=0)
					{
						//更新
						isUpdate = true;
					}
				}
				ddr.Close();

				if(isUpdate)
					DbCmd.CommandText = string.Format("update tbLearn set Eigenvalue='{0}' where LearnCharacter = '{1}'",Eigenvalue,LearnCharacter.Replace("'","''"));
				else
					DbCmd.CommandText = string.Format("insert into tbLearn (LearnCharacter,Eigenvalue,Frequency)values('{0}','{1}',0)",LearnCharacter.Replace("'","''"),Eigenvalue);

				DbCmd.ExecuteNonQuery();
			}
			catch(Exception ex)
			{
				this.strCaption = ex.Message;
			}
			finally
			{
				this.textResult.Text = strCaption; 
			}
		}

		private void buttonLearnConfirm_Click(object sender, System.EventArgs e)
		{
			if(this.inputLearnCharacter.Text.Length==0)
				return;
			SaveLearn(this.inputLearnCharacter.Text);
		}

		private void userDrawArea_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			this.currentlyDrawing = true;
			userDrawArea_MouseMove(sender,e);
		}

		private void userDrawArea_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if(this.currentlyDrawing)
			{
				Graphics g = Graphics.FromImage(userDrawAreaBmp);
				
				g.FillPie(new SolidBrush(Color.White),e.X-(PointSize.Width/2),e.Y-(PointSize.Height/2),PointSize.Width,PointSize.Height,0f,360f);

				g.Dispose();

				userDrawArea.Refresh();
			}
		}

		private void FormMain_Load(object sender, System.EventArgs e)
		{
			Image userDrawAreaImage = userDrawAreaBmp = new Bitmap(200,200,System.Drawing.Imaging.PixelFormat.Format24bppRgb);
			Image userTemplateAreaImage = userTemplateAreaBmp = new Bitmap(200,200,System.Drawing.Imaging.PixelFormat.Format24bppRgb);
			
			this.userDrawArea.Image = userDrawAreaImage;
			this.userTemplateArea.Image = userTemplateAreaImage;

			//PointSize = new Size(10,10);

			arrCurrentEigenvalue = new ArrayList(this.SplitWidth * this.SplitHeight);
			resultList = new ArrayList();

			DbConn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=" + Application.ExecutablePath.Substring(0,Application.ExecutablePath.LastIndexOf("\\")) + @"\Learn.mdb;");
			DbCmd = new OleDbCommand();

			try
			{
				DbConn.Open();
				DbCmd.Connection = this.DbConn;
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message);
				Application.Exit();
			}
		}

		private void userDrawArea_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			this.currentlyDrawing = false;
			this.GraspRawData();
		}

		private void buttonRecognise_Click(object sender, System.EventArgs e)
		{
			//识别
			DbCmd.CommandText = "select AutoId,LearnCharacter,Eigenvalue,Frequency from tbLearn order by Frequency";
			OleDbDataReader ddr = DbCmd.ExecuteReader();

			int maxMatch = 0;


			progressRecognition.Value = 0;
			progressRecognition.Maximum = ddr.GetSchemaTable().Rows.Count;
			progressRecognition.Minimum = 0;
			
			resultList.Clear();
			resultList.TrimToSize();

			while(ddr.Read())
			{
				//比较
				if(!ddr.IsDBNull(2))
					maxMatch = Match(ddr.GetString(2));

				//MessageBox.Show (maxMatch.ToString());
				//保留匹配率字符的精度,建议10%
				if(maxMatch>=precisionTrackBar.Value)
				{
					Suited suited = new Suited(ddr.GetString(1),maxMatch,ddr.GetFloat(3));
					resultList.Add(suited);
				}

				//找出匹配率最高的
				resultList.Sort(0,resultList.Count,new MaxMatchSort());
				//保存100个字符
//				while(resultList.Count>(this.SplitWidth * this.SplitHeight))
//				{
//					resultList.RemoveAt(resultList.Count-1);
//				}
				
				if(progressRecognition.Value<progressRecognition.Maximum)
					progressRecognition.Value += 1;
			}
			ddr.Close();

			//分离结果

			//找出频率最高的字符
			resultList.Sort(0,resultList.Count,new MaxUseCountSort());
			//保存匹配率最高的10个字符
			while(resultList.Count>10)
			{
				resultList.RemoveAt(resultList.Count-1);
			}

			//找出匹配率最高的
			resultList.Sort(0,resultList.Count,new MaxMatchSort());

			ResultList.Items.Clear();
			foreach(Suited s in resultList)
			{
				ResultList.Items.Add(s.Character);
			}

			resultList.Clear();
			resultList.TrimToSize();

			progressRecognition.Value = progressRecognition.Maximum;

		}

		private int Match(string Eigenvalue)
		{
			int maxMatch = 0;
			string c = "";
			string e = "";
			int d = 0;

			if(Eigenvalue.Length<(this.SplitWidth * this.SplitHeight))
				return 0;

			for(int i=0;i<this.SplitWidth;i++)  
			{
				for(int j=0;j<this.SplitHeight;j++)
				{
					if(d>=this.arrCurrentEigenvalue.Count)
						return maxMatch;

					c = Eigenvalue.Substring(d,1);
					e = (string)this.arrCurrentEigenvalue[d];
					if(c=="1")
					{
						if(e=="1")
							maxMatch ++;
						else
							maxMatch --;
					}
					else
					{
						if(e=="0")
							maxMatch ++;
						else
							maxMatch --;
					}
					d++;
				}
			}
			
			return maxMatch;
		}

		private void ResultList_DoubleClick(object sender, System.EventArgs e)
		{
			string LearnCharacter = (string)ResultList.SelectedItem + "";
			if(LearnCharacter!="")
			{
				DbCmd.CommandText = string.Format("update tbLearn set Frequency=Frequency + 0.01 where LearnCharacter = '{0}'",LearnCharacter.Replace("'","''"));
				DbCmd.ExecuteNonQuery();
				MessageBox.Show(LearnCharacter);
			}
		}

		private void AutoLearn_Click(object sender, System.EventArgs e)
		{
			if(openFileDialog1.ShowDialog(this)==DialogResult.OK)
			{
				//开始分析
				System.IO.StreamReader sr = new System.IO.StreamReader(openFileDialog1.FileName,System.Text.Encoding.Default);
				string allText = sr.ReadToEnd();
				string c = "";
				sr.Close();

				allText = allText.Replace(" ","");
				allText = allText.Replace("\r","");
				allText = allText.Replace("\n","");
				allText = allText.Replace("\t","");


				Bitmap memBmp = new Bitmap(this.userDrawAreaBmp.Width,this.userDrawAreaBmp.Height,System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
				Graphics memGp = Graphics.FromImage(memBmp);
				Graphics uGp = Graphics.FromImage(userDrawAreaBmp);

				progressRecognition.Value = 0;
				progressRecognition.Minimum = 0;
				progressRecognition.Maximum = allText.Length-1;
				btnBreak.Tag = 0;
				for(int i=0;i<allText.Length;i++)
				{
					c = allText.Substring(i,1);
					memGp.FillRectangle(new SolidBrush(Color.Black),0,0,memBmp.Width,memBmp.Height);
					memGp.DrawString(c,new Font("楷体_GB2312",100f,FontStyle.Bold),new SolidBrush(Color.White),0,0);
					uGp.FillRectangle(new SolidBrush(Color.Black),0,0,userDrawAreaBmp.Width,userDrawAreaBmp.Height);
					uGp.DrawImage(memBmp,new Rectangle(0,0,userDrawAreaBmp.Width,userDrawAreaBmp.Height),0,0,memBmp.Width,memBmp.Height,System.Drawing.GraphicsUnit.Pixel);
					//分析特征码
					GraspRawData();
					//保存特征码
					SaveLearn(c);

					textResult.Text = "正在自学习:" + c;
					try
					{
						textResult.Refresh();
					}
					catch{;}

					if(progressRecognition.Value<progressRecognition.Maximum)
						progressRecognition.Value += 1;
					
					if((int)btnBreak.Tag==1)
						break;

					Application.DoEvents();
				}
				uGp.Dispose();
				memGp.Dispose();
				memBmp.Dispose();

				MessageBox.Show("自学习完毕!");
			}
		}

		private void btnBreak_Click(object sender, System.EventArgs e)
		{
			btnBreak.Tag = 1;
		}

		//		private int BinToDec(string strBin)
		//		{
		//			int i = strBin.Length;
		//			int btd = 0;
		//			while(i>0)
		//			{
		//				if(strBin.Substring(i,1)=="1")
		//					btd = btd + (int)Math.Pow(2,strBin.Length-i);
		//				i -- ;
		//			}
		//			return btd;
		//		}
		//
		//		private string DecToBin(int intDec,int intDigit)
		//		{
		//			int intTemp = 0;
		//			string dtb = "";
		//			while(intDec>0 && intDigit>0)
		//			{
		//				intDigit--;
		//				intTemp = intDec % 2;
		//				if(intTemp!=0)
		//				{
		//					dtb = "1" + dtb;
		//					intDec = (intDec-1) /2;
		//				}
		//				else
		//				{
		//					dtb = "0" + dtb;
		//					intDec = intDec / 2;
		//				}
		//			}
		//
		//			while(intDigit!=0)
		//			{
		//				intDigit --;
		//				dtb = "0" + dtb;
		//			}
		//			return dtb;
		//		}
	}

	public class MaxMatchSort : IComparer  
	{
		int IComparer.Compare( Object x, Object y )  
		{
			Suited sx = (Suited)x;
			Suited sy = (Suited)y;
			CaseInsensitiveComparer cic = new CaseInsensitiveComparer();
			return cic.Compare(sy.MaxMatch , sx.MaxMatch);
		}
	}

	public class MaxUseCountSort : IComparer  
	{
		int IComparer.Compare( Object x, Object y )  
		{
			Suited sx = (Suited)x;
			Suited sy = (Suited)y;
			CaseInsensitiveComparer cic = new CaseInsensitiveComparer();
			return cic.Compare(sy.MaxUseCount , sx.MaxUseCount);
		}
	}
}

⌨️ 快捷键说明

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