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

news2024/9/24 15:26:38

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
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        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;
        }

        /// <summary>
        /// 显示文本框内容
        /// </summary>
        /// <param name="message"></param>
        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();
        }

        /// <summary>
        /// 显示OCR处理结果
        /// </summary>
        /// <param name="ocrScanner"></param>
        /// <param name="stopwatch"></param>
        /// <param name="result"></param>
        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);
            }));
        }
    }
}

四、测试运行如下:

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/171051.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

AWS实战:S3 Cloud Watch Event 触发Lambda

架构 既然是S3 Cloud Watch Event 触发Lambda&#xff0c;首先就需要三个AWS的service: S3Event BridgeLambda S3有event产生时向Event Bridge发送event&#xff0c;Event Bridge通过event rule的配置过滤event&#xff0c;将符合规则的event发送给lambda进行处理。 S3如何向…

科研论文写作

科研论文写作 文章目录科研论文写作一、论文写作的重要性二、论文写作的总原则二、论文写作的注意事项数学符号上下文要保持一致英文表达存在天然的顺承关系比较级和最高级不可以轻易使用需要有甄别的使用其他论文中的句子数学符号需要有明确定义特定的缩写第一次出现需要指明全…

vue本地案例之记事本

新增 生成列表结构(v-for 数组)获取用户输入(v-model 双向数据绑定)回车&#xff0c;新增数据(v-on .enter添加数据&#xff1a;事件绑定v-on&#xff08;可缩写为后面加事件名&#xff09;&#xff0c;限定回车.enter)删除 点击删除指定内容(v-on splice索引&#xff09;数据…

posix API与网络协议栈

posix API与网络协议栈 scoket socket包含两部分&#xff1a;fd、tcb&#xff08;tcp control block&#xff09; 其中&#xff0c;fd属于文件系统&#xff0c;可在用户态进行操控&#xff1b;而tcb属于内核协议栈 三次握手 服务端API socekt()&#xff1a;创建一个tcb和f…

Linux常用命令——tput命令

在线Linux命令查询工具(http://www.lzltool.com/LinuxCommand) tput 通过terminfo数据库对终端会话进行初始化和操作 补充说明 tput命令将通过 terminfo 数据库对您的终端会话进行初始化和操作。通过使用 tput&#xff0c;您可以更改几项终端功能&#xff0c;如移动或更改光…

关系数据库-2-[mysql8]python3操作mysql

参考文档Python-PyMysql详解 参考文档使用pandas操作MySQL数据库 1 pymysql操作mysql PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库。 PyMySQL 遵循 Python 数据库 API v2.0 规范&#xff0c;并包含了 pure-Python MySQL 客户端库。 pip install PyMySQL1.…

在线教育-谷粒学院学习笔记(八)

文章目录1 内容介绍2 微服务3 微服务实现删除nacos4 删除课程-删除视频5 Hystrix1 内容介绍 Spring Colud 删除小节-删除视频删除课程-删除视频 2 微服务 service 三个服务 service_edu 8001service_oss 8002service_vod 8003 微服务 微服务是架构风格把一个项目拆分成多个…

NUMA介绍

早期CPU访问内存结构 UMA1&#xff08;Uniform Memory Access, 一致性内存访问 &#xff09; 早期的计算机&#xff0c;内存控制器还没有整合进 CPU&#xff0c;所有的内存访问都需要经过北桥芯片来完成。 在 UMA 架构下&#xff0c;CPU 和内存之间的通信全部都要通过前端总线…

【Java|golang】2293. 极大极小游戏

给你一个下标从 0 开始的整数数组 nums &#xff0c;其长度是 2 的幂。 对 nums 执行下述算法&#xff1a; 设 n 等于 nums 的长度&#xff0c;如果 n 1 &#xff0c;终止 算法过程。否则&#xff0c;创建 一个新的整数数组 newNums &#xff0c;新数组长度为 n / 2 &#x…

go语言初识——数据类型

目录 go go与C语言区别 helloworld 数据类型 变量 定义 类型推导 简短声明 : 匿名变量 常量 iota 基本类型 指针 数组 结构体 go Go是2009年开源的编程语言&#xff0c;Go语言具有以下特性&#xff1a;语法简洁、并发编程、编译迅速、数组安全、丰富的内置类型…

如何在 Antd Pro 框架上实现样式自定义?

文章目录一、前言二、实操过程一、前言 Ant Design Pro 是一个企业级中后台前端/设计解决方案&#xff0c;已经有完善的 UI 组件及设计风格&#xff0c;在一些特定项目中&#xff0c;往往涉及到对其调整&#xff0c;来实现独特的 UI 设计&#xff0c;如不同的主题色、布局、卡…

帮助台技术员协助的自助服务

对于帮助台技术员例行电话带来成本高居不下的企业来说&#xff0c;最终用户自助服务是一个伟大的解决方案&#xff0c;允许用户解决自己的IT问题。然而&#xff0c;一些企业仍未部署自助服务。例如&#xff0c;即使在Active Directory中维护每个员工的最新个人资料信息是一件乏…

1. Python3的安装与环境搭建

1. 开发环境&#xff1a;Win10 Python3.10.5 PyCharm 2. 安装Python3 下载地址&#xff1a;https://www.python.org/ 运行刚下载的python-3.10.5-amd64.exe&#xff0c;并做以下操作&#xff1a; 然后Next&#xff0c;并进行以下操作&#xff1a; 最后点击Install&#…

会话跟踪技术:Cookie、Session和Token

会话跟踪技术背景&#xff1a;1. Cookie &#xff08;客户端的会话跟踪技术&#xff09;1.1 原理1.2 基本使用1.2.1 服务器发送Cookie1.2.2 服务器获取Cookie1.3 使用细节1.3.1 存活时间1.3.2 存储中文2. Session&#xff08;服务端的会话跟踪技术&#xff09;2.1 原理2.2 基本…

采用高通Qualcomm处理器的手机进EDL 9008模式的办法

由于我们有很多基于 Qualcomm 的设备&#xff0c;其中一些设备可能会古怪地猜测如何进入 EDL 模式&#xff0c;或者如何正确进入。 例如&#xff0c;对于 Alcatel&#xff0c;您必须先按住两个音量键&#xff0c;然后再按住其中一个&#xff0c;对于 CAT B35&#xff0c;您必须…

Python基础(二十六):模块和包简单介绍

文章目录 模块和包简单介绍 一、模块 1、导入模块 2、制作模块

基于Servlet+jsp+mysql开发javaWeb校园图书管理系统

你知道的越多&#xff0c;你不知道的越多 点赞再看&#xff0c;养成习惯 如果您有疑问或者见解&#xff0c;或者没有积分想获取项目&#xff0c;欢迎指教&#xff1a; 企鹅&#xff1a;869192208 文章目录一、开发背景二、 需求分析三、开发环境四、运行效果五、开发流程工程目…

【Android安全】Google Hardware-backed Keystore | SafetyNet | 远程证明Remote Attestation

Google Hardware-backed KeyStore Attestation 原理及流程 SafetyNet Hardware-backed Attestation SafetyNet Hardware-backed Attestation&#xff1a;使用了Hardware-backed Keystore SafetyNet 支持Software Attestation 和 Hardware-backed Attestation&#xff0c;根据…

五、Web应用开发模式

web应用开发模式 web应用的开发主要有两种模式&#xff1a; 前后端不分离前后端分离 前后端不分离 在互联网早期&#xff0c;web应用开发采用前后端不分离的方式。 它是以后端直接渲染模板完成响应的一种开发模式。 以前后端不分离的方式开发的web应用的架构图如下&#x…

每天15分钟JMeter进阶篇(1):JAVA 取样器的基本使用

每天15分钟JMeter进阶篇&#xff08;1&#xff09;&#xff1a;JAVA 取样器的基本使用前言准备工作创建开发工程POM文件创建根工程创建module开发JAVA取样器构建、部署运行构建JAR包部署和运行写在最后前言 JMETER官方提供了丰富的取样器&#xff0c;可以支持80%的常见测试场景…