9

C# 扫描并读取图片中的文字

 3 years ago
source link: https://www.cnblogs.com/Yesi/p/14912370.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

C# 扫描并读取图片中的文字

本文介绍如何通过C# 程序来扫描并读取图片中的文字,这里以创建一个.Net Core程序为例。下面是具体步骤,供参考。

程序测试环境:

  • Visual Studio版本要求不低于2017
  • 图片扫描工具:Spire.OCR for .NET
  • 图片格式:png(这里的图片格式支持JPG、PNG、GIF、BMP、TIFF等格式)
  • 扫描的图片文字:中文(另可支持中文、英语、日语、韩语、德语、法语等)
  • .Net Core 2.1

详细步骤

1. 创建一个.Net Core控制台应用程序。

2. 通过NuGet添加依赖

(1)在【解决方案资源管理器】中,鼠标右键点击【依赖项】,选择【管理NuGet程序包】

(2)在弹出的界面中,选择【浏览】-在搜索框中输入Spire.OCR,点击“安装”

在依次弹出的2个窗口中选择“确定”和“我接受”

(3)完成后,可查看到已添加的依赖项

3.复制dll

情况1:如果为.net core 3.0及以上版本,则从bin\Debug\netcoreapp3.0\runtimes\win-x64\native文件夹中复制如图中的6个dll文件到程序运行路径bin\Debug\netcoreapp3.0;

情况2如果是.net core 3.0以下版本(如本文本中的测试环境),则需要下载Spire.OCR包,并解压,将该文件路径Spire.OCR\Spire.OCR_Dependency\x64中的6个dll复制到程序运行路径F:\VS2017project\ReadTextFromImg_OCR\ReadTextFromImg_OCR\bin\Debug\netcoreapp2.1

4.完成以上操作后,可参考如下代码内容,读取图片上的文本内容

using Spire.OCR;
using System.IO;

namespace ReadTextFromImg_OCR
{
    class Program
    {
        static void Main(string[] args)
        {
            OcrScanner scanner = new OcrScanner();
            scanner.Scan("image.png");
            File.WriteAllText("output.txt", scanner.Text.ToString());
        }
    }
}

测试图片:

文字读取结果:

—End—


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK