close
點選<加入參考>, 第一次執行加入參考, Visual Studio會執行一段時間(正常), 因為會掃描整個系統相容元件的關係
點選.NET物件
C:\Program Files (x86)\Euresys\eVision\NET Components
Visual Studio 2010->偵錯->例外狀況
原廠範例程式,預設值LoaderLock是打勾, 但這個會造成執行階段錯誤!
================================================================
加入menuStrip和openFileDialog
eVision 6.7.1 只支援x86不支援x64
目標Framework不支援.NET Framework 4
支援.NET Framework2, 3, 3.5
開始撰寫程式
using Euresys.eVision;
定義五個region
對應內容如下:
1: #region Image
2: private ImageBW8 m_Source = new ImageBW8();
3: private ROIBW8 m_Roi = new ROIBW8();
4: #endregion
5: #region OCR
6: private Euresys.eVision.Ocr m_Ocr = new Ocr();
7: private string m_OcrText = "";
8: private bool m_bRead = false;
9: private DragHandle m_Handle = DragHandle.NoHandle;
10: #endregion
11: #region Font
12: private Font m_Font = new Font("Arial", 9);
13: #endregion
14: #region Pen
15: private Pen m_RedPen = new Pen(Color.Red);
16: private Pen m_GreenPen = new Pen(Color.FromArgb(0, 255, 0));
17: #endregion
18: #region Brush
19: private Brush m_BlackBrush = new SolidBrush(Color.Black);
20: private Brush m_WhiteBrush = new SolidBrush(Color.White);
21: #endregion
Form1 加入Load Event
定義副程式: Redraw和DrawBackedText
1: #region Subroutine
2: private void Redraw(Graphics g)
3: {
4: if (m_Source.Void) // if (m_Source==null) then m_Source.Void return true
5: return;
6:
7: // Display the source image
8: m_Source.Draw(g);
9:
10: // Draw the roi frame with its handles
11: m_Roi.DrawFrame(g, m_RedPen, FramePosition.On, true);
12:
13: // Draw the character bounding boxes if needed
14: if (m_bRead)
15: {
16: m_Ocr.DrawChars(g, m_GreenPen);
17:
18: // Display recognised text at (20, 20)
19: DrawBackedText(g, m_OcrText, 20, 20);
20: }
21: }
22: #region draw characters on the graphics
23: /* 根據傳入text, 先計算所需的包覆矩形大小size
24: * 白底黑字 FillRectangle & DrawString
25: */
26: private void DrawBackedText(Graphics g, string text, int x, int y)
27: {
28: SizeF size = g.MeasureString(text, m_Font);
29: g.FillRectangle(m_WhiteBrush, x, y, size.Width, size.Height);
30: g.DrawString(text, m_Font, m_BlackBrush, x, y);
31: }
32: #endregion
33:
34: #endregion
載入一張照片, 到目前為止紅色roi並不能移動或縮放…
為了移動roi, Form1 加入mouse down, mouse move, mouse up三個events
1: this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
2: this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
3: this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
mouse down
1: private void Form1_MouseDown(object sender, MouseEventArgs e)
2: {
3: m_Handle = m_Roi.HitTest(e.X, e.Y);
4: }
mouse move
1: private void Form1_MouseMove(object sender, MouseEventArgs e)
2: {
3: if (m_Handle != DragHandle.NoHandle)
4: {
5: m_Roi.Drag(m_Handle, e.X, e.Y);
6: Redraw(CreateGraphics());
7: }
8: }
mouse up
1: private void Form1_MouseUp(object sender, MouseEventArgs e)
2: {
3: m_Handle = DragHandle.NoHandle;
4: }
載入ocr參數檔案
1: openFileDialog1.Filter = "OCR font files (*.ocr)|*.ocr";
2: if (openFileDialog1.ShowDialog() == DialogResult.Cancel)
3: return;
4: try
5: {
6: m_Ocr.Load(openFileDialog1.FileName);
7: }
8: catch( Euresys.eVisionException exc)
9: {
10: MessageBox.Show(exc.Message);
11: }
12:
13: // Refresh form
14: Refresh();
字元辨識
1: private void readToolStripMenuItem_Click(object sender, EventArgs e)
2: {
3: if (m_Source.Void)
4: return;
5: m_Ocr.Threshold = ThresholdMode.MinResidue;
6:
7: try
8: {
9: // Perform character detection
10: m_Ocr.BuildObjects(m_Roi);
11: m_Ocr.MinCharWidth = 28;
12: m_Ocr.FindAllChars(m_Roi);
13:
14: // Perform recognition
15: System.Int32 classes = (int)EasyOcr.Class.AllClasses;
16: m_Ocr.Recognize(m_Roi, 256, classes, ref m_OcrText);
17:
18: // Enable the displaying
19: m_bRead = true;
20: }
21: catch (Euresys.eVisionException exc)
22: {
23: MessageBox.Show(exc.Message);
24: }
25:
26: // Force a redraw
27: Refresh();
28: }
資料來源: eVision OCR sample code
全站熱搜
留言列表