C#使用Spire.OCR框架识别图片中的字母,数字,文字等 您所在的位置:网站首页 文字设计框架图片 C#使用Spire.OCR框架识别图片中的字母,数字,文字等

C#使用Spire.OCR框架识别图片中的字母,数字,文字等

2024-02-10 15:08| 来源: 网络整理| 查看: 265

OCR

OCR(optical character recognition),光学字符识别。

      OCR文字识别是指电子设备(例如扫描仪或数码相机)检查纸上打印的字符,然后用字符识别方法将形状翻译成计算机文字的过程;即,对文本资料进行扫描,然后对图像文件进行分析处理,获取文字及版面信息的过程。       如何除错或利用辅助信息提高识别正确率,是OCR最重要的课题。       衡量一个OCR系统性能好坏的主要指标有:拒识率、误识率、识别速度、用户界面的友好性,产品的稳定性,易用性及可行性等。       OCR技术的实现,总体上可以分为五步:预处理图片、切割字符、识别字符、恢复版面、后处理文字。中间的三步是核心,头尾两步最难。

C#中使用Spire.OCR框架可以识别图片中的字母、文字、数字等。 一、Demo测试

VS2019中新建窗体应用程序ImageRecognitionTextDemo,将默认的Form1重命名为FormImageRecognitionText。

右键项目属性

选择目标平台为X64 

右键项目 ImageRecognitionTextDemo,选择【管理NuGet程序包】 ,搜索Spire.OCR

安装Nuget包完成

将下载如下非托管dll放在应用程序的debug下,或者 复制到项目根目录下,选择始终复制。

 注意托管Spire.OCR.dll需要自动引用如上六个框架包,因此Spire.OCR.dll和六个框架包必须在同一路径下。

二、窗体FormImageRecognitionText设计器代码如下

文件 FormImageRecognitionText.Designer.cs源程序如下:

namespace ImageRecognitionTextDemo { partial class FormImageRecognitionText { /// /// 必需的设计器变量。 /// private System.ComponentModel.IContainer components = null; /// /// 清理所有正在使用的资源。 /// /// 如果应释放托管资源,为 true;否则为 false。 protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// /// 设计器支持所需的方法 - 不要修改 /// 使用代码编辑器修改此方法的内容。 /// private void InitializeComponent() { this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.rtxbDisplay = new System.Windows.Forms.RichTextBox(); this.btnOpen = new System.Windows.Forms.Button(); this.btnRecognize = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); this.SuspendLayout(); // // pictureBox1 // this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.pictureBox1.Location = new System.Drawing.Point(12, 12); this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Size = new System.Drawing.Size(400, 400); this.pictureBox1.TabIndex = 0; this.pictureBox1.TabStop = false; // // rtxbDisplay // this.rtxbDisplay.Location = new System.Drawing.Point(418, 12); this.rtxbDisplay.Name = "rtxbDisplay"; this.rtxbDisplay.ReadOnly = true; this.rtxbDisplay.Size = new System.Drawing.Size(562, 715); this.rtxbDisplay.TabIndex = 1; this.rtxbDisplay.Text = ""; // // btnOpen // this.btnOpen.Font = new System.Drawing.Font("宋体", 13F, System.Drawing.FontStyle.Bold); this.btnOpen.Location = new System.Drawing.Point(51, 439); this.btnOpen.Name = "btnOpen"; this.btnOpen.Size = new System.Drawing.Size(104, 56); this.btnOpen.TabIndex = 2; this.btnOpen.Text = "打开图片"; this.btnOpen.UseVisualStyleBackColor = true; this.btnOpen.Click += new System.EventHandler(this.btnOpen_Click); // // btnRecognize // this.btnRecognize.Font = new System.Drawing.Font("宋体", 13F, System.Drawing.FontStyle.Bold); this.btnRecognize.Location = new System.Drawing.Point(217, 439); this.btnRecognize.Name = "btnRecognize"; this.btnRecognize.Size = new System.Drawing.Size(104, 56); this.btnRecognize.TabIndex = 3; this.btnRecognize.Text = "识别图片"; this.btnRecognize.UseVisualStyleBackColor = true; this.btnRecognize.Click += new System.EventHandler(this.btnRecognize_Click); // // FormImageRecognitionText // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(982, 739); this.Controls.Add(this.btnRecognize); this.Controls.Add(this.btnOpen); this.Controls.Add(this.rtxbDisplay); this.Controls.Add(this.pictureBox1); this.Name = "FormImageRecognitionText"; this.Text = "图像识别文本框架OCR"; this.Load += new System.EventHandler(this.FormImageRecognitionText_Load); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); this.ResumeLayout(false); } #endregion private System.Windows.Forms.PictureBox pictureBox1; private System.Windows.Forms.RichTextBox rtxbDisplay; private System.Windows.Forms.Button btnOpen; private System.Windows.Forms.Button btnRecognize; } } 三、FormImageRecognitionText窗体源程序如下:

程序文件FormImageRecognitionText.cs

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Spire.OCR; namespace ImageRecognitionTextDemo { public partial class FormImageRecognitionText : Form { public FormImageRecognitionText() { InitializeComponent(); } private void FormImageRecognitionText_Load(object sender, EventArgs e) { //C# .NET实现扫描识别图片中的文字 Spire.OCR //OCR:光学字符识别。 /* * OCR(optical character recognition)文字识别是指电子设备(例如扫描仪或数码相机)检查纸上打印的字符, * 然后用字符识别方法将形状翻译成计算机文字的过程;即,对文本资料进行扫描,然后对图像文件进行分析处理,获取文字及版面信息的过程。 * 如何除错或利用辅助信息提高识别正确率,是OCR最重要的课题。 * 衡量一个OCR系统性能好坏的主要指标有:拒识率、误识率、识别速度、用户界面的友好性,产品的稳定性,易用性及可行性等。 * OCR技术的实现,总体上可以分为五步:预处理图片、切割字符、识别字符、恢复版面、后处理文字。中间的三步是核心,头尾两步最难。 * 百家号技术: * https://baijiahao.baidu.com/s?id=1744946979174786023&wfr=spider&for=pc */ pictureBox1.BackgroundImageLayout = ImageLayout.Zoom; } /// /// 显示文本框内容 /// /// private void DisplayContent(string message) { this.BeginInvoke(new Action(() => { if (rtxbDisplay.TextLength > 10240) { rtxbDisplay.Clear(); } rtxbDisplay.AppendText($"{DateTime.Now.ToString("HH:mm:ss.fff")}->{message}\n"); rtxbDisplay.ScrollToCaret(); })); Application.DoEvents(); } /// /// 显示OCR处理结果 /// /// /// /// private void DisplayProcessResultOCR(OcrScanner ocrScanner, System.Diagnostics.Stopwatch stopwatch, bool result) { IOCRText text = ocrScanner.Text; IOCRTextBlock[] ocrTextBlocks = text.Blocks; stopwatch.Stop(); DisplayContent($"识别图片完成,耗时【{stopwatch.Elapsed.TotalMilliseconds}】ms:识别结果【{result}】,文本块个数【{ocrTextBlocks.Length}】"); for (int i = 0; i < ocrTextBlocks.Length; i++) { DisplayContent($"第【{(i + 1).ToString("D2")}】个文本块:"); DisplayContent($" Text:【{ocrTextBlocks[i].Text}】"); DisplayContent($" Confidence:【{ocrTextBlocks[i].Confidence}】"); DisplayContent($" Level:【{ocrTextBlocks[i].Level}】"); DisplayContent($" IsTruncated:【{ocrTextBlocks[i].IsTruncated}】"); DisplayContent($" Box:【{ocrTextBlocks[i].Box}】"); //DisplayContent($" TextBlock:【{ocrTextBlocks[i].TextBlock == null}】"); } MessageBox.Show(text.ToString()); } private async void btnOpen_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "jpeg|*.jpg|bmp|*.bmp|gif|*.gif|png|*.png|tiff|*.tiff|All|*.*"; if (openFileDialog.ShowDialog() == DialogResult.OK) { string fileName = openFileDialog.FileName; pictureBox1.BackgroundImage = Image.FromFile(fileName); await Task.Run(new Action(() => { OcrScanner ocrScanner = new OcrScanner(); System.Diagnostics.Stopwatch stopwatch = System.Diagnostics.Stopwatch.StartNew(); DisplayContent($"开始识别图片:【{fileName}】"); bool result = ocrScanner.Scan(fileName); DisplayProcessResultOCR(ocrScanner, stopwatch, result); })); } } private async void btnRecognize_Click(object sender, EventArgs e) { MemoryStream memoryStream = new MemoryStream(); pictureBox1.BackgroundImage.Save(memoryStream, pictureBox1.BackgroundImage.RawFormat); await Task.Run(new Action(() => { OcrScanner ocrScanner = new OcrScanner(); System.Diagnostics.Stopwatch stopwatch = System.Diagnostics.Stopwatch.StartNew(); DisplayContent($"开始识别图片:图片大小【{memoryStream.Length / 1024}】KB"); OCRImageFormat imageFormat; Enum.TryParse(pictureBox1.BackgroundImage.RawFormat.ToString(), true, out imageFormat); bool result = ocrScanner.Scan(memoryStream, imageFormat); DisplayProcessResultOCR(ocrScanner, stopwatch, result); })); } } } 四、测试运行如下:

 

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有