انجام Optical Character Recognition (OCR) روی یک تصویر یک کار پیچیده است. این مبحث قدم به قدم توضیح می دهد که چگونه متن را از تصویر در سی شارپ به سرعت و به راحتی استخراج کنیم. با استفاده از Aspose.OCR for .NET میتوانید به سادگی کاراکترهای تصویر را در سی شارپ در چند مرحله بخوانید.
مراحل استخراج متن از تصویر در سی شارپ
- از بسته NuGet Aspose.OCR for .NET استفاده کنید
- ابتدا مرجع Aspose.OCR namespace را وارد کنید
- برای اعمال مجوز Aspose از روش SetLicense استفاده کنید
- یک شی از نمونه AsposeOcr Class ایجاد کنید
- از روش RecognizeImage برای استخراج متن از تصویر با اعمال OCR استفاده کنید.
- متن استخراج شده را با استفاده از کلاس های FileStream و StreamWriter در فایل متنی ذخیره کنید
مراحل بالا به شما نشان می دهد که خواندن کاراکترهای تصویر در سی شارپ بسیار آسان است. کد مراحل فوق در زیر آورده شده است.
کد برای استخراج متن از تصویر در سی شارپ
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(); | |
} | |
} | |
} |
اگر از زبان سی شارپ استفاده می کنید، رویکرد برای هر نوع برنامه دات نت یکسان است. چه بخواهید تمام متن را از تصویر استخراج کنید یا متن را خط به خط از یک تصویر بخوانید، Aspose.OCR برای دات نت می تواند به شما در انجام این کار کمک کند.