C#.NET implements scanning and recognition of text in pictures

This article takes C# and VB.NET code as an example to introduce how to scan and read the text in the picture. The sample code in the article is introduced in detail, which is helpful for us to learn C#. Interested friends can follow the editor to learn together.

( Reprinted from Script House: https://www.jb51.net/article/232085.htm#_label0 )

Environment configuration

This article takes C# and VB.NET code as an example to introduce how to scan and read the text in the picture.

The program environment is as follows:

  • Visual Studio version requirements are not lower than 2017
  • Image scanning tool: Spire.OCR for .NET
  • Image format: png (the image format here supports JPG, PNG, GIF, BMP, TIFF and other formats)
  • Scanned image text: Chinese (English, Japanese, Korean, German, French, etc. can also be supported)
  • .NET Framework 4.6.1

The following are the specific steps and operation methods.

Steps

Step 1. Create a .NET Framework console application in Visual Studio.

Step 2. Right-click the program project properties and set the target platform to x64.

This step is necessary because Spire.OCR for .NET only supports 64-bit operating systems.

 

Step 3. Install Spire.OCR for .NET in the VS program. (★ It is recommended to install through Nuget )

In [Solution Explorer], right-click [References] and select [Manage NuGet Packages]

In the opened interface, click the [Browse] tab, and enter [Spire.OCR] in the search box. Click【Install

 

Step 4. After the installation is complete, find the packages folder generated by default under the installation path, and copy the 6 dll files under the folder path:

F:\VS2017Project\ScanImageAndReadCharacter_OCR\packages\Spire.OCR.1.8.0\runtimes\win-x64\native

Copy it to the running path of the VS program project:

F:\VS2017Project\ScanImageAndReadCharacter_OCR\ScanImageAndReadCharacter_OCR\bin\Debug

Call the API interface to scan and read the text in the picture

After completing the above steps, execute the following program code to read the text on the picture:

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

using Spire.OCR;

using System.IO;

namespace ScanImageAndReadCharacter_OCR

{

    class Program

    {

        static void Main(string[] args)

        {

            //创建一个OcrScanner类的实例

            OcrScanner scanner = new OcrScanner();

            //调用OcrScanner.Scan(string fileName)方法扫描图片上的文字

            scanner.Scan("image.png");

            string text = scanner.Text.ToString();

            //保存扫描获取的文字为.txt文档

            File.WriteAllText("output.txt", text);

            System.Diagnostics.Process.Start("output.txt");

        }

    }

}

VB.NET

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

Imports Spire.OCR

Imports System.IO

Namespace ScanImageAndReadCharacter_OCR

    Class Program

        Private Shared Sub Main(args As String())

            '创建一个OcrScanner类的实例

            Dim scanner As New OcrScanner()

            '调用OcrScanner.Scan(string fileName)方法扫描图片上的文字

            scanner.Scan("image.png")

            Dim text As String = scanner.Text.ToString()

            '保存扫描获取的文字为.txt文档

            File.WriteAllText("output.txt", text)

            System.Diagnostics.Process.Start("output.txt")

        End Sub

    End Class

End Namespace

Image scanning and reading results:

 

Precautions

1. Only supports 64-bit platforms and systems

2. Only 1.8.0 and later versions support .NET Framework 

Reprinted script house: https://www.jb51.net/article/232085.htm#_label0

Guess you like

Origin blog.csdn.net/weixin_43972758/article/details/126937172