Get Started with OCR in C# and VB.NET

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

IronOCR は、.NET プラットフォーム ソフトウェア開発者が画像や PDF ドキュメントからテキストを認識して読み取ることができる C# ソフトウェア ライブラリです。 これは、最も高度なTesseractエンジンを使用した純粋な.NET OCRライブラリです。

インストール

NuGetパッケージマネージャでインストール

Visual StudioまたはコマンドラインでNuGet Package Managerを使用してIronOCRをインストールします。 Visual Studio で、次のコマンドを使用してコンソールに移動します。

  • ツール ->
  • NuGet パッケージ マネージャー ->
  • パッケージマネージャーコンソール
Install-Package IronOcr

NuGet上のIronOCRを確認して、バージョン更新やインストールについての詳細を確認してください。

さまざまなプラットフォームで利用できる他のIronOCR NuGet パッケージがあります。

Linux および macOS 用の IronOcr.Extensions.AdvancedScan

このパッケージは、LinuxとMacを使用し、IronOCRの高度な機能を利用したいユーザー向けです。

トラブルシューティング

このパッケージの新しいアップデートにより、IronOCR はパッケージ内の OpenCV 依存関係を結合して合理化しますが、現在 OpenCV 依存関係をインポートしている開発者は次のエラーを受け取ります。

The type of namespace name `OpenCvSharp` could not be found(are you missing a using directive or an assembly reference)

OpenCV 名前空間を安全に削除すると、問題は解決されます。

IronOCR .ZIPをダウンロード

代わりに、.ZIP ファイル経由で IronOCR をダウンロードすることもできます。 クリックするとDLL を直接ダウンロードします。 .zip をダウンロードしたら:

.NET Framework 4.0+ のインストール手順:

  • net40フォルダ内のIronOcr.dllをプロジェクトに含めます
  • 次にアセンブリ参照を追加します:

    • システム構成
    • システム.描画
    • システム.Web

.NET Standard、.NET Core 2.0+、.NET 5 の手順

  • netstandard2.0フォルダ内のIronOcr.dllをプロジェクトに含めます
  • 次に、NuGet パッケージ参照を追加します。

    • システム.描画.Common 4.7 以上

IronOCRインストーラーをダウンロードする(Windowsのみ)

別の選択肢として、IronOCRインストーラーをダウンロードし、必要なリソースをインストールします。 このオプションは Windows システム専用であることに注意してください。 インストーラーをダウンロードするにはここをクリックしてください。 .zip をダウンロードしたら:

.NET Framework 4.0+ のインストール手順:

  • net40フォルダ内のIronOcr.dllをプロジェクトに含めます
  • 次にアセンブリ参照を追加します:

    • システム構成
    • システム.描画
    • システム.Web

.NET Standard、.NET Core 2.0+、.NET 5 の手順

  • netstandard2.0フォルダ内のIronOcr.dllをプロジェクトに含めます
  • 次に、NuGet パッケージ参照を追加します。

    • システム.描画.Common 4.7 以上

IronOCRを選ぶ理由

IronOCR は、簡単にインストールでき、完全で、十分に文書化された .NET ソフトウェア ライブラリです。

IronOCR を選択すると、外部 Web サービスを使用したり、継続的な料金を支払ったり、機密文書をインターネット経由で送信したりすることなく、99.8% 以上の OCR 精度を実現できます。

なぜC#開発者が標準のTesseractよりIronOCRを選ぶのか:

  • 単一のDLLまたはNuGetとしてインストール
  • Tesseract 5、4、および 3 エンジンがすぐに使用できます。
  • 精度99.8% は通常の Tesseract を大幅に上回ります。
  • 驚異的なスピードとマルチスレッド
  • MVC、WebApp、デスクトップ、コンソール、サーバーアプリケーションと互換性があります
  • 作業に必要なEXEやC++コードはありません
  • 完全なPDF OCRサポート
  • ほぼすべての画像ファイルやPDFでOCRを実行
  • .NET Core、Standard、Framework の完全サポート
  • Windows、Mac、Linux、Azure、Docker、Lambda、AWS にデプロイ可能
  • バーコードとQRコードを読み取る
  • OCR結果をXHTMLとしてエクスポート
  • OCRを検索可能なPDF文書にエクスポート
  • マルチスレッドサポート
  • 125の国際言語はすべてNuGetまたはOcrDataファイルで管理されます
  • 画像、座標、統計、フォントを抽出します。 テキストだけではありません。
  • 商用および独自のアプリケーション内で Tesseract OCR を再配布するために使用できます。

IronOCR は、現実世界の画像や写真などの不完全な文書、あるいはデジタル ノイズや不完全さがある可能性のある低解像度のスキャンを処理する場合に威力を発揮します。

他の .NET Tesseract API や Web サービスなど、.NET プラットフォーム用の他の無料 OCRライブラリは、これらの実際の使用ケースではそれほど優れたパフォーマンスを発揮しません。

OCR with Tesseract 5 - Start Coding in C

以下のコード サンプルは、C# または VB .NET を使用して画像からテキストを簡単に読み取る方法を示しています。

ワンライナー

:path=/static-assets/ocr/content-code-examples/get-started/get-started-1.cs
string Text = new IronTesseract().Read(@"img\Screenshot.png").Text;
Dim Text As String = (New IronTesseract()).Read("img\Screenshot.png").Text
$vbLabelText   $csharpLabel

設定可能なHello World

:path=/static-assets/ocr/content-code-examples/get-started/get-started-2.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();

// Add multiple images
input.LoadImage("images/sample.jpeg");

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using

' Add multiple images
input.LoadImage("images/sample.jpeg")

Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
$vbLabelText   $csharpLabel

C# PDF OCR

同じアプローチを使用して、任意の PDF ドキュメントからテキストを抽出することもできます。

:path=/static-assets/ocr/content-code-examples/get-started/get-started-3.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();

// We can also select specific PDF page numbers to OCR
input.LoadPdf("example.pdf", Password: "password");

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);

// 1 page for every page of the PDF
Console.WriteLine($"{result.Pages.Length} Pages");
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using

' We can also select specific PDF page numbers to OCR
input.LoadPdf("example.pdf", Password:= "password")

Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)

' 1 page for every page of the PDF
Console.WriteLine($"{result.Pages.Length} Pages")
$vbLabelText   $csharpLabel

マルチページTIFFのOCR

:path=/static-assets/ocr/content-code-examples/get-started/get-started-4.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames("multi-frame.tiff", pageindices);
OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using
Private pageindices = New Integer() { 1, 2 }
input.LoadImageFrames("multi-frame.tiff", pageindices)
Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
$vbLabelText   $csharpLabel

バーコードとQRコード

IronOCRのユニークな機能は、テキストをスキャンしている間にドキュメントからバーコードとQRコードを読み取ることができることです。 OcrResult.OcrBarcodeクラスのインスタンスは、スキャンされた各BARCODEに関する詳細情報を開発者に提供します。

:path=/static-assets/ocr/content-code-examples/get-started/get-started-5.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
ocr.Configuration.ReadBarCodes = true;

using OcrInput input = new OcrInput();
input.LoadImage("img/Barcode.png");

OcrResult Result = ocr.Read(input);
foreach (var Barcode in Result.Barcodes)
{
    // type and location properties also exposed
    Console.WriteLine(Barcode.Value);
}
Imports IronOcr

Private ocr As New IronTesseract()
ocr.Configuration.ReadBarCodes = True

Using input As New OcrInput()
	input.LoadImage("img/Barcode.png")
	
	Dim Result As OcrResult = ocr.Read(input)
	For Each Barcode In Result.Barcodes
		' type and location properties also exposed
		Console.WriteLine(Barcode.Value)
	Next Barcode
End Using
$vbLabelText   $csharpLabel

画像の特定領域のOCR

IronOCR のすべてのスキャンおよび読み取り方法では、ページのどの部分からテキストを読み取りたいかを正確に指定できます。 これは、標準化されたフォームを参照する場合に非常に役立ち、多くの時間を節約して効率を向上させることができます。

クロップ領域を使用するには、System.Drawing へのシステム参照を追加し、System.Drawing.Rectangle オブジェクトを使用できるようにする必要があります。

:path=/static-assets/ocr/content-code-examples/get-started/get-started-6.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();

// Dimensions are in pixel
var contentArea = new System.Drawing.Rectangle() { X = 215, Y = 1250, Height = 280, Width = 1335 };

input.LoadImage("document.png", contentArea);

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using

' Dimensions are in pixel
Private contentArea = New System.Drawing.Rectangle() With {
	.X = 215,
	.Y = 1250,
	.Height = 280,
	.Width = 1335
}

input.LoadImage("document.png", contentArea)

Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
$vbLabelText   $csharpLabel

低品質スキャン用のOCR

IronOCRのOcrInputクラスは、通常のTesseractでは読み取れないスキャンデータを修正することができます。

:path=/static-assets/ocr/content-code-examples/get-started/get-started-7.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames(@"img\Potter.tiff", pageindices);

// fixes digital noise and poor scanning
input.DeNoise();

// fixes rotation and perspective
input.Deskew();

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using
Private pageindices = New Integer() { 1, 2 }
input.LoadImageFrames("img\Potter.tiff", pageindices)

' fixes digital noise and poor scanning
input.DeNoise()

' fixes rotation and perspective
input.Deskew()

Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
$vbLabelText   $csharpLabel

OCR結果を検索可能なPDFとしてエクスポートする

:path=/static-assets/ocr/content-code-examples/get-started/get-started-8.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
input.Title = "Quarterly Report";
input.LoadImage("image1.jpeg");
input.LoadImage("image2.png");
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames("image3.gif", pageindices);

OcrResult result = ocr.Read(input);
result.SaveAsSearchablePdf("searchable.pdf");
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using
input.Title = "Quarterly Report"
input.LoadImage("image1.jpeg")
input.LoadImage("image2.png")
Dim pageindices = New Integer() { 1, 2 }
input.LoadImageFrames("image3.gif", pageindices)

Dim result As OcrResult = ocr.Read(input)
result.SaveAsSearchablePdf("searchable.pdf")
$vbLabelText   $csharpLabel

TIFFから検索可能なPDFへの変換

:path=/static-assets/ocr/content-code-examples/get-started/get-started-9.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames("example.tiff", pageindices);
ocr.Read(input).SaveAsSearchablePdf("searchable.pdf");
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using
Private pageindices = New Integer() { 1, 2 }
input.LoadImageFrames("example.tiff", pageindices)
ocr.Read(input).SaveAsSearchablePdf("searchable.pdf")
$vbLabelText   $csharpLabel

OCR結果をHTMLとしてエクスポート

:path=/static-assets/ocr/content-code-examples/get-started/get-started-10.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
input.Title = "Html Title";
input.LoadImage("image1.jpeg");

OcrResult Result = ocr.Read(input);
Result.SaveAsHocrFile("results.html");
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using
input.Title = "Html Title"
input.LoadImage("image1.jpeg")

Dim Result As OcrResult = ocr.Read(input)
Result.SaveAsHocrFile("results.html")
$vbLabelText   $csharpLabel

OCR画像強化フィルター

IronOCRは、OCRのパフォーマンスを向上させるために、OcrInputオブジェクトに対して独自のフィルターを提供します。

画像強調コード例

:path=/static-assets/ocr/content-code-examples/get-started/get-started-11.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
input.LoadImage("LowQuality.jpeg");

// fixes digital noise and poor scanning
input.DeNoise();

// fixes rotation and perspective
input.Deskew();

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using
input.LoadImage("LowQuality.jpeg")

' fixes digital noise and poor scanning
input.DeNoise()

' fixes rotation and perspective
input.Deskew()

Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
$vbLabelText   $csharpLabel

OCR画像フィルターのリスト

IronOCR に組み込まれている OCR パフォーマンスを向上させる入力フィルターには次のものがあります。

  • OcrInput.Rotate(double degrees) - 画像を時計回りに指定した角度だけ回転させます。反時計回りに回転させる場合は、負の数値を使用してください。
  • OcrInput.Binarize() - このフィルターは、すべてのピクセルを中間色なしで黒または白に変換し、コントラストが非常に低い画像におけるOCRの性能を向上させる可能性があります。
  • OcrInput.ToGrayScale() - すべてのピクセルをグレースケールの色調に変換します。 精度は向上しないかもしれませんが、速度は向上する可能性があります。
  • OcrInput.Contrast() - コントラストを自動的に高め、コントラストの低いスキャンにおいて処理速度と精度を向上させることがよくあります。
  • OcrInput.DeNoise() - デジタルノイズを除去します。ノイズの発生が予想される場合にのみ推奨されます。
  • OcrInput.Invert() - すべての色を反転させます(白が黒になり、その逆も同様です)。
  • OcrInput.Dilate() - モルフォロジを前進させ、オブジェクトの境界にピクセルを追加する。Erodeの逆の操作。
  • OcrInput.Erode() - 形態を前進させ、オブジェクトの境界からピクセルを除去する。Dilateの逆の操作。
  • OcrInput.Deskew() - 画像を回転させて正しい向きにします。 Tesseract のスキュー許容範囲が限られているため便利です。
  • OcrInput.EnhanceResolution - 低品質な画像の解像度を向上させます。 この設定は通常、低 DPI 入力を自動的に管理するために使用されます。
  • EnhanceResolution は、低解像度(275 dpi未満)の画像を検出し、それらをアップスケールしてテキストを鮮明化することで、OCRの結果を向上させます。 時間はかかりますが、全体的な OCR 操作時間が短縮されることが多いです。
  • Language - 22種類の国際言語パックから選択可能です。
  • Strategy - WORD 間の統計的関係に基づいて、高速で精度はやや低い戦略と、AI を使用して精度を高めた高度な戦略のいずれかを選択できます。
  • ColorSpace - グレースケールまたはカラーでOCRを行うかを選択してください; グレースケールは一般的に最適ですが、特定のコントラストのシナリオではカラーの方が適している場合があります。
  • DetectWhiteTextOnDarkBackgrounds - ネガティブ画像に対応し、暗い背景上の白いテキストを自動的に検出して読み取ります。
  • InputImageType - OCRライブラリに、処理対象が文書全体かスニペットかを指定して指示します。
  • RotateAndStraighten - IronOCRが、回転したり遠近法の歪みが生じたりしたドキュメントを適切に処理できるようにします。
  • ReadBarcodes - テキストのスキャンと同時に、大幅な時間増なしにBARCODEやQRコードを自動的に読み取ります。
  • ColorDepth - OCR処理における色深度のピクセルあたりのビット数を決定します。 深度を高くすると品質は向上しますが、処理時間も長くなります。

125 言語パック

IronOCR は、DLL として配布される言語パックを介して125 の国際言語をサポートしており、この Web サイトまたはNuGet パッケージ マネージャーからダウンロードできます。

言語には、ドイツ語、フランス語、英語、中国語、日本語などがあります。 専門的な言語パックはMRZ、MICRチェック、財務データ、ナンバープレート等に存在します。加えて、カスタムのTesseract".traineddata"ファイルを使用できます。

言語の例

// Reference to the path of the source file that demonstrates setting language packs for OCR
:path=/static-assets/ocr/content-code-examples/get-started/get-started-12.cs
// Reference to the path of the source file that demonstrates setting language packs for OCR
using IronOcr;

// PM> Install IronOcr.Languages.Arabic
IronTesseract ocr = new IronTesseract();
ocr.Language = OcrLanguage.Arabic;

using OcrInput input = new OcrInput();

var pageindices = new int[] { 1, 2 };
input.LoadImageFrames("img/arabic.gif", pageindices);
// Add image filters if needed
// In this case, even thought input is very low quality
// IronTesseract can read what conventional Tesseract cannot.
OcrResult result = ocr.Read(input);
// Console can't print Arabic on Windows easily.
// Let's save to disk instead.
result.SaveAsTextFile("arabic.txt");
' Reference to the path of the source file that demonstrates setting language packs for OCR
Imports IronOcr

' PM> Install IronOcr.Languages.Arabic
Private ocr As New IronTesseract()
ocr.Language = OcrLanguage.Arabic

Using input As New OcrInput()
	
	Dim pageindices = New Integer() { 1, 2 }
	input.LoadImageFrames("img/arabic.gif", pageindices)
	' Add image filters if needed
	' In this case, even thought input is very low quality
	' IronTesseract can read what conventional Tesseract cannot.
	Dim result As OcrResult = ocr.Read(input)
	' Console can't print Arabic on Windows easily.
	' Let's save to disk instead.
	result.SaveAsTextFile("arabic.txt")
End Using
$vbLabelText   $csharpLabel

複数言語の例

複数の言語を同時にOCR処理することも可能です。これにより、Unicode文書内の英語のメタデータやURLのOCR処理能力が向上します。

// Reference to the path of the source file that demonstrates multi-language OCR
:path=/static-assets/ocr/content-code-examples/get-started/get-started-13.cs
// Reference to the path of the source file that demonstrates multi-language OCR
using IronOcr;

// PM> Install IronOcr.Languages.ChineseSimplified
IronTesseract ocr = new IronTesseract();
ocr.Language = OcrLanguage.ChineseSimplified;

// We can add any number of languages
ocr.AddSecondaryLanguage(OcrLanguage.English);

using OcrInput input = new OcrInput();
input.LoadPdf("multi-language.pdf");
OcrResult result = ocr.Read(input);
result.SaveAsTextFile("results.txt");
' Reference to the path of the source file that demonstrates multi-language OCR
Imports IronOcr

' PM> Install IronOcr.Languages.ChineseSimplified
Private ocr As New IronTesseract()
ocr.Language = OcrLanguage.ChineseSimplified

' We can add any number of languages
ocr.AddSecondaryLanguage(OcrLanguage.English)

Using input As New OcrInput()
	input.LoadPdf("multi-language.pdf")
	Dim result As OcrResult = ocr.Read(input)
	result.SaveAsTextFile("results.txt")
End Using
$vbLabelText   $csharpLabel

詳細なOCR結果オブジェクト

IronOCR は、各操作に対して OCR 結果オブジェクトを返します。 一般的に、開発者はスキャンされたテキストを取得するために Text プロパティにアクセスします。 ただし、結果オブジェクトにはさらに詳細な情報が含まれています。

// Reference to the path of the source file demonstrating detailed OCR result object usage
:path=/static-assets/ocr/content-code-examples/get-started/get-started-14.cs
// Reference to the path of the source file demonstrating detailed OCR result object usage
using IronOcr;

IronTesseract ocr = new IronTesseract();

// Must be set to true to read barcode
ocr.Configuration.ReadBarCodes = true;
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames(@"img\sample.tiff", pageindices);

OcrResult result = ocr.Read(input);
var pages = result.Pages;
var words = pages[0].Words;
var barcodes = result.Barcodes;
// Explore here to find a massive, detailed API:
// - Pages, Blocks, Paraphaphs, Lines, Words, Chars
// - Image Export, Fonts Coordinates, Statistical Data, Tables
' Reference to the path of the source file demonstrating detailed OCR result object usage
Imports IronOcr

Private ocr As New IronTesseract()

' Must be set to true to read barcode
ocr.Configuration.ReadBarCodes = True
Using input As New OcrInput()
	Dim pageindices = New Integer() { 1, 2 }
	input.LoadImageFrames("img\sample.tiff", pageindices)
	
	Dim result As OcrResult = ocr.Read(input)
	Dim pages = result.Pages
	Dim words = pages(0).Words
	Dim barcodes = result.Barcodes
	' Explore here to find a massive, detailed API:
	' - Pages, Blocks, Paraphaphs, Lines, Words, Chars
	' - Image Export, Fonts Coordinates, Statistical Data, Tables
End Using
$vbLabelText   $csharpLabel

パフォーマンス

IronOCR は、パフォーマンスの調整や画像の変更を必要とせず、すぐに使用できます。

速度は驚異的です:IronOCR.2020+は最大10倍の速さで、以前のビルドよりも250%以上少ないエラーを出します。

もっと詳しく知る

C#、VB、F#、またはその他の .NET 言語での OCR の詳細については、コミュニティ チュートリアルをお読みください。このチュートリアルでは、IronOCR の実際の使用例を示し、ライブラリの最適化のニュアンスを説明しています。

.NET 開発者向けの完全な API リファレンスも利用可能です。

カーティス・チャウ
テクニカルライター

Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。

開発以外にも、CurtisはIoT(Internet of Things)への強い関心を持ち、ハードウェアとソフトウェアの統合方法を模索しています。余暇には、ゲームをしたりDiscordボットを作成したりして、技術に対する愛情と創造性を組み合わせています。

レビュー済み
Jeff Fritz
Jeffrey T. Fritz
プリンシパルプログラムマネージャー - .NETコミュニティチーム
Jeffはまた、.NETとVisual Studioチームのプリンシパルプログラムマネージャーです。彼は.NET Conf仮想会議シリーズのエグゼクティブプロデューサーであり、週に二回放送される開発者向けライブストリーム『Fritz and Friends』のホストを務め、テクノロジーについて話すことや視聴者と一緒にコードを書くことをしています。Jeffはワークショップ、プレゼンテーション、およびMicrosoft Build、Microsoft Ignite、.NET Conf、Microsoft MVPサミットを含む最大のMicrosoft開発者イベントのコンテンツを企画しています。
準備はできましたか?
Nuget ダウンロード 5,896,332 | バージョン: 2026.5 just released
Still Scrolling Icon

まだスクロールしていますか?

すぐに証拠が欲しいですか? PM > Install-Package IronOcr
サンプルを実行 あなたの画像が検索可能なテキストになるのをご覧ください。