Japanese OCR in C# and .NET

This article was translated from English: Does it need improvement?
Translated
View the article in English

**此文件的其他版本:日本語で

IronOCR 是一个 C# 软件组件,允许 .NET 程序员从图像和 PDF 文档中读取 126 种语言的文本,包括日语。 它是 Tesseract 的一個高級分支,專為 .NET 開發人員構建,在速度和準確性上經常優於其他 Tesseract 引擎。

它已在许多不同的硬件平台上进行测试,并且软件库已更新至 .NET 的最新版本。 这是需要在应用或项目中执行 OCR 的开发人员的好选择。 IronOCR 为应用程序开发人员提供了一个易于使用的 API,可与多种语言配合使用,并可以通过多种方式集成到应用程序中。 IronOCR 支持 125 种 OCR 语言。 这是一个了不起的 OCR 工具。

IronOcr.Languages.Japanese 内容

IronOCR 日文包执行多种操作。 此包包含几个用于 .NET 的 OCR 语言:

  • JapaneseAlphabet
  • JapaneseAlphabetBest
  • JapaneseAlphabetFast
  • JapaneseVerticalAlphabet
  • JapaneseVerticalAlphabetBest
  • JapaneseVerticalAlphabetFast
  • Japanese
  • JapaneseBest
  • JapaneseFast
  • JapaneseVertical
  • JapaneseVerticalBest
  • JapaneseVerticalFast

下載

您可以从以下链接下载 IronOCR 日文语言包 [日本語 (にほんご)]:

  • 下载为 [Zip](Zip)
  • 通过 [NuGet](NuGet) 安装

我们将在下面的部分中查看 IronOCR 的安装。

使用 IronOCR 进行日语语言识别

創建或打開一個C#項目

让我们从创建一个 C# 项目开始。 我们使用 Visual Studio 2022 创建 C# 项目 — 您可以根据自己的偏好选择任何版本。 建议使用 Visual Studio 的最新版本。 按照以下步骤创建 C# 项目:

  • 打開Visual Studio 2022。
  • 點擊"創建新項目"按鈕。
class="content-img-align-center">
class="center-image-wrapper"> Chinese Ocr 1 related to 創建或打開一個C#項目
  • 在搜索栏中输入 “Windows”,从搜索结果中选择 “Windows Form” 应用程序并点击“下一步”按钮。
class="content-img-align-center">
class="center-image-wrapper"> Chinese Ocr 2 related to 創建或打開一個C#項目
  • 為項目命名。 我已将项目命名为“JapaneseOCR”。命名后,点击“下一步”按钮。
class="content-img-align-center">
class="center-image-wrapper"> Japanese Ocr 1 related to 創建或打開一個C#項目
  • 在下一屏选择 .NET 框架。 根据您的项目需要选择 .NET 框架。 我們在本教程中選擇.NET 5.0版本。
class="content-img-align-center">
class="center-image-wrapper"> Chinese Ocr 4 related to 創建或打開一個C#項目
  • 選擇後,點擊"創建"按鈕。 它将很容易在 Visual Studio 中创建 C# Windows 窗体项目。

项目已创建! 我们现在可以在我们的 IronOCR 库中使用它。 我们也可以在现有的 C# 项目中使用它。 打开项目并开始安装 IronOCR 库。 以下部分說明如何在C#項目中安裝IronOCR庫。

安裝

现在是时候在我们的项目中安装 IronOCR 库了。 IronOCR 库可以通过两种不同的方式安装。 我们可以使用包管理器控制台和 NuGet 包管理器安装 IronOCR。 让我们看看这两种方法。

使用NuGet包管理器

要安裝IronOCR庫,我們必須打開NuGet Package Manager接口。 按照以下步骤安装 IronOCR 库:

  • 从主菜单点击“工具”,从下拉菜单中,悬停在“NuGet 包管理器”上,然后选择“管理解决方案的 NuGet 包”。
class="content-img-align-center">
class="center-image-wrapper"> Chinese Ocr 5 related to 使用NuGet包管理器
  • 這將打開NuGet包管理器界面。 转到浏览选项卡,在搜索栏中输入“IronOCR Japanese”。 从搜索结果中选择日本包并点击“安装”按钮以开始安装。
class="content-img-align-center">
class="center-image-wrapper"> Japanese Ocr 2 related to 使用NuGet包管理器
  • 它将开始安装库。 安裝後,您將能夠在項目中使用IronOCR庫。

使用包管理器控制台

我们将使用包管理器控制台安装 IronOCR 库。 通过控制台安装库非常简单。 让我们看看如何使用控制台安装 IronOCR 库:

  • 打开项目并转到 Visual Studio 中的包管理器控制台。 通常在 Visual Studio 的底部可以找到。
  • 在控制台中输入以下命令来安装 IronOCR Japanese OCR 语言:

    PM> Install-Package IronOCR.Languages.Japanese
    PM> Install-Package IronOCR.Languages.Japanese
    SHELL
  • 安装将开始,您将看到控制台中的进度。 安装后,您将在解决方案资源管理器的“依赖项”部分看到 IronOCR 依赖项。

安装后,您将能够在没有任何第三方软件的情况下使用该库。 接下来是设置我们程序的前端的时候了。

代码示例:用于 OCR 的日语语言

现在是时候编写用于实现日语语言的 IronOCR 库的代码了。 首先,我们必须开发选择图像文件的前端。让我们看看该如何做。

前端开发

对于前端开发,我们将利用 Visual Studio 中的“工具箱”。 它有许多预制的元素,我们可以用来设计我们的前端。 我们将使用图片框,一个富文本框,一个按钮和两个用于识别的标签。 我们将根据需要给这些元素一个合适的形状和良好的 UI。 您可以根据自己的选择更改元素的大小和属性。我们使输出文本框不可编辑,图片框的图片属性为“Zoom”,以便每张图片都适合图片框。 我们的前端将如下所示:

class="content-img-align-center">
class="center-image-wrapper"> Japanese Ocr 3 related to 前端开发

IronOCR 的后端代码

我们的前端准备好了。 现在是时候让它上线了。但是首先,我们必须导入 IronOCR 命名空间以便在代码中使用 IronOCR。 在文件顶部写下以下行:

using IronOCR;
using IronOCR;
Imports IronOCR
$vbLabelText   $csharpLabel

“选择图像”按钮将用于选择日文图像。 当我们选择图像时,它会自动加载到图片框中并可见。 同时,IronOCR 将开始识别上传图像中的日文单词。 处理完成后,输出将显示在富文本框中。 通过双击按钮,我们来为其添加这些功能。 以下是按钮功能的代码示例。 它还会将输出文本保存到一个 “txt” 文件中。

private void btn_image_Click(object sender, EventArgs e)
{
    OpenFileDialog open = new OpenFileDialog();

    // Display the image selection dialog, and upon successful image selection, proceed
    if (open.ShowDialog() == DialogResult.OK)
    {
        // Display selected image in the picture box
        img_image.Image = new Bitmap(open.FileName);

        // Initialize IronTesseract for OCR processing
        var Ocr = new IronTesseract
        {
            // Set the OCR language to JapaneseBest for optimal accuracy
            Language = OcrLanguage.JapaneseBest
        };

        // Using 'OcrInput' to process the selected image file
        using (var Input = new OcrInput(open.FileName))
        {
            // Perform OCR to extract text from the image
            var Result = Ocr.Read(Input);

            // Display recognized text in the rich text box
            txt_output.Text = Result.Text;

            // Save the output as a text file named "JapaneseText.txt"
            Result.SaveAsTextFile("JapaneseText.txt");
        }
    }
}
private void btn_image_Click(object sender, EventArgs e)
{
    OpenFileDialog open = new OpenFileDialog();

    // Display the image selection dialog, and upon successful image selection, proceed
    if (open.ShowDialog() == DialogResult.OK)
    {
        // Display selected image in the picture box
        img_image.Image = new Bitmap(open.FileName);

        // Initialize IronTesseract for OCR processing
        var Ocr = new IronTesseract
        {
            // Set the OCR language to JapaneseBest for optimal accuracy
            Language = OcrLanguage.JapaneseBest
        };

        // Using 'OcrInput' to process the selected image file
        using (var Input = new OcrInput(open.FileName))
        {
            // Perform OCR to extract text from the image
            var Result = Ocr.Read(Input);

            // Display recognized text in the rich text box
            txt_output.Text = Result.Text;

            // Save the output as a text file named "JapaneseText.txt"
            Result.SaveAsTextFile("JapaneseText.txt");
        }
    }
}
Private Sub btn_image_Click(ByVal sender As Object, ByVal e As EventArgs)
	Dim open As New OpenFileDialog()

	' Display the image selection dialog, and upon successful image selection, proceed
	If open.ShowDialog() = DialogResult.OK Then
		' Display selected image in the picture box
		img_image.Image = New Bitmap(open.FileName)

		' Initialize IronTesseract for OCR processing
		Dim Ocr = New IronTesseract With {.Language = OcrLanguage.JapaneseBest}

		' Using 'OcrInput' to process the selected image file
		Using Input = New OcrInput(open.FileName)
			' Perform OCR to extract text from the image
			Dim Result = Ocr.Read(Input)

			' Display recognized text in the rich text box
			txt_output.Text = Result.Text

			' Save the output as a text file named "JapaneseText.txt"
			Result.SaveAsTextFile("JapaneseText.txt")
		End Using
	End If
End Sub
$vbLabelText   $csharpLabel

在此功能中,当用户点击按钮时,将出现一个选择对话框,提示用户选择一个包含日文文本的图像。 当用户选择图像时,Bitmap 函数将获取图像路径并将其加载到图片框中。 加载后,我们初始化 IronOCR 库并将语言设置为日语。 IronOCR 将图像路径作为输入并开始扫描。 扫描后,它将输出文本存储在 Result 变量中,并在富文本框中显示为输出。 最后,它将输出文件保存为名为 “JapaneseText” 的 “txt” 文件。

運行項目

我们已经设计了代码并实现了后端。 现在是时候运行程序以检查功能是否正常工作。

  • 点击绿色播放按钮以运行项目。 我们将在操作系统屏幕中央看到这个界面。
class="content-img-align-center">
class="center-image-wrapper"> Japanese Ocr 4 related to 運行項目
  • 点击“选择图像”按钮并选择包含日文文本的图像。
class="content-img-align-center">
class="center-image-wrapper"> Japanese Ocr 5 related to 運行項目
  • 您将在富文本框中看到输出的图像文本。
class="content-img-align-center">
class="center-image-wrapper"> Japanese Ocr 6 related to 運行項目
  • OCR 结果的文本文件将以“JapaneseText”保存。
class="content-img-align-center">
class="center-image-wrapper"> Japanese Ocr 7 related to 運行項目

IronOCR 的 OCR 准确性非常好。

總結

在本教程中,我们学习了如何使用 IronOCR 库进行日文文本识别。 如果您想了解更多关于 IronOCR 的信息,请点击这个 链接

有关 Iron Software 的更多信息,请点击这个链接。 如果您想尝试 IronOCR 库,则可以无须支付任何费用即可激活免费试用。 Iron Software 当前提供一项特别优惠,您可以以两款软件的价格购买到一套五款软件产品。 如需更多信息,请点击此处