📄 formmain.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
namespace OCR
{
struct Suited
{
public string Character; //字符名
public int MaxMatch; //匹配率
public float MaxUseCount; //使用频率
public Suited(string Character,int MaxMatch,float MaxUseCount)
{
this.Character = Character;
this.MaxMatch = MaxMatch;
this.MaxUseCount = MaxUseCount;
}
}
/// <summary>
/// OCR 的摘要说明。
/// </summary>
public class FormMain : System.Windows.Forms.Form
{
private System.Windows.Forms.Label TeachLabelText;
private System.Windows.Forms.TextBox inputLearnCharacter;
private System.Windows.Forms.Button buttonLearnConfirm;
private System.Windows.Forms.Button buttonLearnCancel;
private System.Windows.Forms.Label textResult;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
string strCaption = "";
bool currentlyDrawing = false;
Bitmap userDrawAreaBmp;
Bitmap userTemplateAreaBmp;
ArrayList arrCurrentEigenvalue ;
ArrayList resultList ;
OleDbConnection DbConn ;
OleDbCommand DbCmd ;
public int SplitWidth = 20;
public int SplitHeight = 20;
public Size PointSize = new Size(10,10);
private System.Windows.Forms.GroupBox frameArea1;
private System.Windows.Forms.GroupBox frameArea3;
private System.Windows.Forms.PictureBox userDrawArea;
private System.Windows.Forms.Button buttonRecognise;
private System.Windows.Forms.Button buttonClearScreen;
private System.Windows.Forms.PictureBox userTemplateArea;
private System.Windows.Forms.ListBox ResultList;
private System.Windows.Forms.ProgressBar progressRecognition;
private System.Windows.Forms.Label progressText;
private System.Windows.Forms.Label resultText;
private System.Windows.Forms.TrackBar precisionTrackBar;
private System.Windows.Forms.Button AutoLearn;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.Button btnBreak;
private System.Windows.Forms.Label precisionText;
public FormMain()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.TeachLabelText = new System.Windows.Forms.Label();
this.inputLearnCharacter = new System.Windows.Forms.TextBox();
this.buttonLearnConfirm = new System.Windows.Forms.Button();
this.buttonLearnCancel = new System.Windows.Forms.Button();
this.textResult = new System.Windows.Forms.Label();
this.frameArea1 = new System.Windows.Forms.GroupBox();
this.buttonRecognise = new System.Windows.Forms.Button();
this.buttonClearScreen = new System.Windows.Forms.Button();
this.userDrawArea = new System.Windows.Forms.PictureBox();
this.frameArea3 = new System.Windows.Forms.GroupBox();
this.AutoLearn = new System.Windows.Forms.Button();
this.userTemplateArea = new System.Windows.Forms.PictureBox();
this.ResultList = new System.Windows.Forms.ListBox();
this.progressRecognition = new System.Windows.Forms.ProgressBar();
this.progressText = new System.Windows.Forms.Label();
this.resultText = new System.Windows.Forms.Label();
this.precisionTrackBar = new System.Windows.Forms.TrackBar();
this.precisionText = new System.Windows.Forms.Label();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.btnBreak = new System.Windows.Forms.Button();
this.frameArea1.SuspendLayout();
this.frameArea3.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.precisionTrackBar)).BeginInit();
this.SuspendLayout();
//
// TeachLabelText
//
this.TeachLabelText.AutoSize = true;
this.TeachLabelText.Location = new System.Drawing.Point(8, 280);
this.TeachLabelText.Name = "TeachLabelText";
this.TeachLabelText.Size = new System.Drawing.Size(110, 17);
this.TeachLabelText.TabIndex = 7;
this.TeachLabelText.Text = "请输入学习的字符:";
//
// inputLearnCharacter
//
this.inputLearnCharacter.Location = new System.Drawing.Point(8, 304);
this.inputLearnCharacter.MaxLength = 1;
this.inputLearnCharacter.Name = "inputLearnCharacter";
this.inputLearnCharacter.Size = new System.Drawing.Size(24, 21);
this.inputLearnCharacter.TabIndex = 8;
this.inputLearnCharacter.Text = "";
//
// buttonLearnConfirm
//
this.buttonLearnConfirm.Location = new System.Drawing.Point(40, 304);
this.buttonLearnConfirm.Name = "buttonLearnConfirm";
this.buttonLearnConfirm.Size = new System.Drawing.Size(40, 23);
this.buttonLearnConfirm.TabIndex = 9;
this.buttonLearnConfirm.Text = "确认";
this.buttonLearnConfirm.Click += new System.EventHandler(this.buttonLearnConfirm_Click);
//
// buttonLearnCancel
//
this.buttonLearnCancel.Location = new System.Drawing.Point(80, 304);
this.buttonLearnCancel.Name = "buttonLearnCancel";
this.buttonLearnCancel.Size = new System.Drawing.Size(40, 23);
this.buttonLearnCancel.TabIndex = 10;
this.buttonLearnCancel.Text = "取消";
this.buttonLearnCancel.Click += new System.EventHandler(this.buttonLearnCancel_Click);
//
// textResult
//
this.textResult.AutoSize = true;
this.textResult.Location = new System.Drawing.Point(8, 344);
this.textResult.Name = "textResult";
this.textResult.Size = new System.Drawing.Size(97, 17);
this.textResult.TabIndex = 11;
this.textResult.Text = "字符识别状态...";
//
// frameArea1
//
this.frameArea1.Controls.Add(this.buttonRecognise);
this.frameArea1.Controls.Add(this.buttonClearScreen);
this.frameArea1.Controls.Add(this.userDrawArea);
this.frameArea1.Location = new System.Drawing.Point(8, 8);
this.frameArea1.Name = "frameArea1";
this.frameArea1.Size = new System.Drawing.Size(216, 264);
this.frameArea1.TabIndex = 12;
this.frameArea1.TabStop = false;
this.frameArea1.Text = "用户绘制区域";
//
// buttonRecognise
//
this.buttonRecognise.Location = new System.Drawing.Point(112, 232);
this.buttonRecognise.Name = "buttonRecognise";
this.buttonRecognise.TabIndex = 6;
this.buttonRecognise.Text = "识别";
this.buttonRecognise.Click += new System.EventHandler(this.buttonRecognise_Click);
//
// buttonClearScreen
//
this.buttonClearScreen.Location = new System.Drawing.Point(32, 232);
this.buttonClearScreen.Name = "buttonClearScreen";
this.buttonClearScreen.TabIndex = 5;
this.buttonClearScreen.Text = "清除";
this.buttonClearScreen.Click += new System.EventHandler(this.buttonClearScreen_Click);
//
// userDrawArea
//
this.userDrawArea.BackColor = System.Drawing.Color.Black;
this.userDrawArea.Location = new System.Drawing.Point(8, 24);
this.userDrawArea.Name = "userDrawArea";
this.userDrawArea.Size = new System.Drawing.Size(200, 200);
this.userDrawArea.TabIndex = 1;
this.userDrawArea.TabStop = false;
this.userDrawArea.MouseUp += new System.Windows.Forms.MouseEventHandler(this.userDrawArea_MouseUp);
this.userDrawArea.MouseMove += new System.Windows.Forms.MouseEventHandler(this.userDrawArea_MouseMove);
this.userDrawArea.MouseDown += new System.Windows.Forms.MouseEventHandler(this.userDrawArea_MouseDown);
//
// frameArea3
//
this.frameArea3.Controls.Add(this.btnBreak);
this.frameArea3.Controls.Add(this.AutoLearn);
this.frameArea3.Controls.Add(this.userTemplateArea);
this.frameArea3.Location = new System.Drawing.Point(232, 8);
this.frameArea3.Name = "frameArea3";
this.frameArea3.Size = new System.Drawing.Size(216, 264);
this.frameArea3.TabIndex = 14;
this.frameArea3.TabStop = false;
this.frameArea3.Text = "区域分析";
//
// AutoLearn
//
this.AutoLearn.Location = new System.Drawing.Point(8, 232);
this.AutoLearn.Name = "AutoLearn";
this.AutoLearn.Size = new System.Drawing.Size(96, 23);
this.AutoLearn.TabIndex = 4;
this.AutoLearn.Text = "自动学习";
this.AutoLearn.Visible = false;
this.AutoLearn.Click += new System.EventHandler(this.AutoLearn_Click);
//
// userTemplateArea
//
this.userTemplateArea.BackColor = System.Drawing.Color.Black;
this.userTemplateArea.Location = new System.Drawing.Point(8, 24);
this.userTemplateArea.Name = "userTemplateArea";
this.userTemplateArea.Size = new System.Drawing.Size(200, 200);
this.userTemplateArea.TabIndex = 3;
this.userTemplateArea.TabStop = false;
//
// ResultList
//
this.ResultList.ColumnWidth = 20;
this.ResultList.HorizontalScrollbar = true;
this.ResultList.ItemHeight = 12;
this.ResultList.Location = new System.Drawing.Point(240, 280);
this.ResultList.MultiColumn = true;
this.ResultList.Name = "ResultList";
this.ResultList.Size = new System.Drawing.Size(200, 16);
this.ResultList.TabIndex = 15;
this.ResultList.DoubleClick += new System.EventHandler(this.ResultList_DoubleClick);
//
// progressRecognition
//
this.progressRecognition.Location = new System.Drawing.Point(240, 304);
this.progressRecognition.Name = "progressRecognition";
this.progressRecognition.Size = new System.Drawing.Size(200, 23);
this.progressRecognition.TabIndex = 16;
//
// progressText
//
this.progressText.AutoSize = true;
this.progressText.Location = new System.Drawing.Point(176, 312);
this.progressText.Name = "progressText";
this.progressText.Size = new System.Drawing.Size(60, 17);
this.progressText.TabIndex = 17;
this.progressText.Text = "处理进度:";
//
// resultText
//
this.resultText.AutoSize = true;
this.resultText.Location = new System.Drawing.Point(176, 280);
this.resultText.Name = "resultText";
this.resultText.Size = new System.Drawing.Size(60, 17);
this.resultText.TabIndex = 18;
this.resultText.Text = "可选文字:";
//
// precisionTrackBar
//
this.precisionTrackBar.Location = new System.Drawing.Point(240, 336);
this.precisionTrackBar.Maximum = 50;
this.precisionTrackBar.Minimum = 1;
this.precisionTrackBar.Name = "precisionTrackBar";
this.precisionTrackBar.TabIndex = 100;
this.precisionTrackBar.Value = 10;
//
// precisionText
//
this.precisionText.AutoSize = true;
this.precisionText.Location = new System.Drawing.Point(176, 344);
this.precisionText.Name = "precisionText";
this.precisionText.Size = new System.Drawing.Size(60, 17);
this.precisionText.TabIndex = 20;
this.precisionText.Text = "识别精度:";
//
// openFileDialog1
//
this.openFileDialog1.Filter = "文本文件|*.txt";
this.openFileDialog1.Title = "请选择源文件!用于学习!";
//
// btnBreak
//
this.btnBreak.Location = new System.Drawing.Point(112, 232);
this.btnBreak.Name = "btnBreak";
this.btnBreak.Size = new System.Drawing.Size(96, 23);
this.btnBreak.TabIndex = 5;
this.btnBreak.Text = "中断学习";
this.btnBreak.Visible = false;
this.btnBreak.Click += new System.EventHandler(this.btnBreak_Click);
//
// FormMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(456, 373);
this.Controls.Add(this.precisionText);
this.Controls.Add(this.precisionTrackBar);
this.Controls.Add(this.resultText);
this.Controls.Add(this.progressText);
this.Controls.Add(this.progressRecognition);
this.Controls.Add(this.ResultList);
this.Controls.Add(this.frameArea3);
this.Controls.Add(this.frameArea1);
this.Controls.Add(this.textResult);
this.Controls.Add(this.buttonLearnCancel);
this.Controls.Add(this.buttonLearnConfirm);
this.Controls.Add(this.inputLearnCharacter);
this.Controls.Add(this.TeachLabelText);
this.Name = "FormMain";
this.Text = "OCR 文字识别";
this.Load += new System.EventHandler(this.FormMain_Load);
this.frameArea1.ResumeLayout(false);
this.frameArea3.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.precisionTrackBar)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new FormMain());
}
private void ClearGraphics(Image img,Color clearColor)
{
if(img!=null)
{
Graphics g = Graphics.FromImage(img);
g.FillRectangle(new SolidBrush(clearColor),0,0,img.Width,img.Height);
g.Dispose();
}
userDrawArea.Refresh();
userTemplateArea.Refresh();
}
private void buttonLearnCancel_Click(object sender, System.EventArgs e)
{
this.inputLearnCharacter.Text = "";
}
private void buttonClearScreen_Click(object sender, System.EventArgs e)
{
arrCurrentEigenvalue.Clear();
arrCurrentEigenvalue.TrimToSize();
this.textResult.Text = "";
this.progressRecognition.Value = 0;
ClearGraphics(userDrawArea.Image,Color.Black);
ClearGraphics(userTemplateArea.Image,Color.Black);
}
private void GraspRawData()
{
bool bool1stScan=true ;
int ax,ay,bx,by;
this.ClearGraphics(this.userTemplateArea.Image,Color.Black);
ax = ay = bx = by = 0;
Graphics g = Graphics.FromImage(userTemplateAreaBmp);
int b = 0;
for(int i=0;i<userDrawAreaBmp.Width;i+=(userDrawAreaBmp.Width/this.SplitWidth) / 2)
for(int j=0;j<userDrawAreaBmp.Height;j+=(userDrawAreaBmp.Height/this.SplitHeight) / 2)
{
if(userDrawAreaBmp.GetPixel(i,j).ToArgb()==Color.White.ToArgb())
{
if(!bool1stScan)
{
if(i<ax) ax = i;
if(i>=bx) bx = i;
if(j<ay) ay = j;
if(j>=by) by = j;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -