using HightOCRTest.Common; using Newtonsoft.Json; using OpenCvSharp; using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.IO; using System.Text; using System.Windows.Forms; namespace HightOCRTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png"; string image_path = ""; bool isDraw = false; static IntPtr engine; private void button6_Click(object sender, EventArgs e) { //授权校验 初始化引擎 string key = ""; string licenseKeyPath = Application.StartupPath + "\\license\\license.key"; string licenseFile = Application.StartupPath + "\\license\\license.ini"; int res = -1; string ini_path = ""; key = File.ReadAllText(licenseKeyPath); res = Native.init_license(key, licenseFile); if (res != 0) { MessageBox.Show(res.ToString()); return; } engine = Native.create(); if (engine == null) { MessageBox.Show("创建引擎失败!"); return; } ini_path = Application.StartupPath + "\\resource"; res = Native.init(engine, "", 6); if (res != 0) { MessageBox.Show(res.ToString()); return; } MessageBox.Show("初始化成功!"); button1.Enabled = true; button3.Enabled = true; button4.Enabled = true; button6.Enabled = false; } private void Form1_Load(object sender, EventArgs e) { //image_path = Application.StartupPath + "\\images\\1.jpg"; image_path = Application.StartupPath + "\\test2.jpg"; pictureBox1.Image = new Bitmap(image_path); } private void button2_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = fileFilter; if (ofd.ShowDialog() != DialogResult.OK) return; pictureBox1.Image = null; image_path = ofd.FileName; pictureBox1.Image = new Bitmap(image_path); textBox1.Text = ""; } StringBuilder ocr_result_texts = new StringBuilder(1024 * 10); StringBuilder ocr_result_words = new StringBuilder(1024 * 100); private void button1_Click(object sender, EventArgs e) { if (image_path == "") { return; } textBox1.Text = ""; Application.DoEvents(); ocr_result_texts.Clear(); ocr_result_words.Clear(); Mat image = new Mat(image_path); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); int res = Native.ocr(engine, image.CvPtr, ocr_result_texts, ocr_result_words); stopwatch.Stop(); double totalTime = stopwatch.Elapsed.TotalSeconds; textBox1.Text += $"耗时: {totalTime:F2}s"; textBox1.Text += "\r\n-------------------\r\n"; if (res == 0) { textBox1.Text += JsonConvert.SerializeObject(JsonConvert.DeserializeObject(ocr_result_texts.ToString()), Newtonsoft.Json.Formatting.Indented); textBox1.Text += "\r\n-------------------\r\n"; textBox1.Text += JsonConvert.SerializeObject(JsonConvert.DeserializeObject(ocr_result_words.ToString()), Newtonsoft.Json.Formatting.Indented); } else { textBox1.Text = "识别失败"; } } //绘制文字区域 private void button3_Click(object sender, EventArgs e) { if (ocr_result_texts.Length == 0) { return; } Mat image = new Mat(image_path); List<OcrResTexts> lt = JsonConvert.DeserializeObject<List<OcrResTexts>>(ocr_result_texts.ToString()); foreach (OcrResTexts item in lt) { string[] pts = item.coordinator.Split(' '); //多边形的顶点 OpenCvSharp.Point[] points = new OpenCvSharp.Point[] { new OpenCvSharp.Point(Convert.ToDouble( pts[0]), Convert.ToDouble( pts[1])), new OpenCvSharp.Point(Convert.ToDouble( pts[2]), Convert.ToDouble( pts[3])), new OpenCvSharp.Point(Convert.ToDouble( pts[4]), Convert.ToDouble( pts[5])), new OpenCvSharp.Point(Convert.ToDouble( pts[6]), Convert.ToDouble( pts[7])), }; // 绘制多边形 Cv2.Polylines(image, new OpenCvSharp.Point[][] { points }, isClosed: true, color: new Scalar(0, 255, 0), thickness: 2); } if (pictureBox1.Image != null) { pictureBox1.Image.Dispose(); pictureBox1.Image = null; } pictureBox1.Image = new Bitmap(image.ToMemoryStream()); image.Dispose(); } //堆代码 duidaima.com //绘制单字区域 private void button4_Click(object sender, EventArgs e) { if (ocr_result_words.Length == 0) { return; } Mat image = new Mat(image_path); List<OcrResWords> lt = JsonConvert.DeserializeObject<List<OcrResWords>>(ocr_result_words.ToString()); foreach (OcrResWords item in lt) { string[] pts = item.coordinator.Split(' '); //left top width height OpenCvSharp.Rect rect = new Rect((int)Convert.ToDouble(pts[0]), (int)Convert.ToDouble(pts[1]), (int)Convert.ToDouble(pts[2]), (int)Convert.ToDouble(pts[3])); Cv2.Rectangle(image, rect, color: new Scalar(255, 0, 0), thickness: 1); } if (pictureBox1.Image != null) { pictureBox1.Image.Dispose(); pictureBox1.Image = null; } pictureBox1.Image = new Bitmap(image.ToMemoryStream()); image.Dispose(); } //识别小语种→ private void button5_Click(object sender, EventArgs e) { //if (image_path == "") //{ // return; //} //textBox1.Text = ""; //Application.DoEvents(); //ocr_result_texts.Clear(); //ocr_result_words.Clear(); //Mat image = new Mat(image_path); //Stopwatch stopwatch = new Stopwatch(); //stopwatch.Start(); //int res = Native.ocr_other(engine, image.CvPtr, ocr_result_texts, ocr_result_words); //stopwatch.Stop(); //double totalTime = stopwatch.Elapsed.TotalSeconds; //textBox1.Text += $"耗时: {totalTime:F2}s"; //textBox1.Text += "\r\n-------------------\r\n"; //if (res == 0) //{ // textBox1.Text += JsonConvert.SerializeObject(JsonConvert.DeserializeObject(ocr_result_texts.ToString()), Newtonsoft.Json.Formatting.Indented); // textBox1.Text += "\r\n-------------------\r\n"; // textBox1.Text += JsonConvert.SerializeObject(JsonConvert.DeserializeObject(ocr_result_words.ToString()), Newtonsoft.Json.Formatting.Indented); //} //else //{ // textBox1.Text = "识别失败"; //} } } }