كيفية استخراج النص من الصورة في C#

يعد إجراء Optical Character Recognition (OCR) على صورة مهمة معقدة. يشرح هذا الموضوع خطوة بخطوة كيفية استخراج النص من الصورة في C# بسرعة وسهولة. باستخدام Aspose.OCR for .NET يمكنك ببساطة قراءة الأحرف من الصورة في C# في بضع خطوات.

خطوات استخراج النص من الصورة في C#

  1. استخدم حزمة Aspose.OCR for .NET NuGet
  2. قم بتضمين مرجع Aspose.OCR namespace أولاً
  3. استخدم طريقة SetLicense لتطبيق ترخيص Aspose
  4. قم بإنشاء كائن لمثيل AsposeOcr Class
  5. استخدم طريقة RecognizeImage لاستخراج النص من الصورة بتطبيق OCR
  6. احفظ النص المستخرج إلى ملف نصي باستخدام فئات 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();
}
}
}

الأسلوب هو نفسه لأي نوع من تطبيقات .NET إذا كنت تستخدم لغة C#. سواء كنت ترغب في استخراج كل النص من الصورة ، أو قراءة نص سطرًا بسطر من صورة ، يمكن أن يساعدك Aspose.OCR for .NET في القيام بذلك.

 عربي