画像に対してOptical Character Recognition(OCR)を実行するのは複雑な作業です。このトピックでは、C#の画像からテキストをすばやく簡単に抽出する方法を段階的に説明します。 Aspose.OCR for .NETを使用すると、数ステップでC#の画像から文字を簡単に読み取ることができます。
C#で画像からテキストを抽出する手順
- Aspose.OCR for .NETNuGetパッケージを使用する
- 最初にAspose.OCR namespace参照を含める
- SetLicenseメソッドを使用してAsposeライセンスを適用します
- AsposeOcr Classインスタンスのオブジェクトを作成します
- RecognizeImageメソッドを使用して、OCRを適用して画像からテキストを抽出します
- FileStreamクラスとStreamWriterクラスを使用して、抽出したテキストをテキストファイルに保存します
上記の手順は、C#で画像から文字を読み取るのが非常に簡単であることを示しています。上記の手順のコードを以下に示します。
C#で画像からテキストを抽出するコード
using System; | |
using System.IO; | |
//Add Aspose.OCR for .NET package reference | |
//Use following namespaces to Extract Text from Image | |
using Aspose.OCR; | |
namespace ExtractTextFromImage | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//Set Aspose license before extracting text from image | |
//using Aspose.OCR for .NET | |
Aspose.OCR.License AsposeOCRLicense = new Aspose.OCR.License(); | |
AsposeOCRLicense.SetLicense(@"c:\asposelicense\license.lic"); | |
//Create an instance of AsposeOcr class before you can apply | |
//OCR on an image to extract the text from it | |
AsposeOcr ExtractTextFromImage = new AsposeOcr(); | |
//Read image using RecognizeImage method on which OCR need to be applied for text extraction | |
string TextExtractedFromImage = ExtractTextFromImage.RecognizeImage("ExampleOCRImageToExtractText.jpg"); | |
//Save extracted text to a text file using File Stream and StreamWriter | |
//classes of System.IO | |
FileStream FStream = new FileStream("ExtractTextFromImageUsingOCR.txt", FileMode.Create); | |
StreamWriter SWriter = new StreamWriter(FStream); | |
//Write extracted text to the file | |
SWriter.WriteLine(TextExtractedFromImage); | |
SWriter.Flush(); | |
//Close FileStream and StreamWriter bojects | |
SWriter.Close(); | |
FStream.Close(); | |
} | |
} | |
} |
C#言語を使用している場合、このアプローチはどのタイプの.NETアプリケーションでも同じです。画像からすべてのテキストを抽出する場合でも、画像から1行ずつテキストを読み取る場合でも、Aspose.OCRfor.NETはそのために役立ちます。