การดำเนินการ Optical Character Recognition (OCR) บนรูปภาพเป็นงานที่ซับซ้อน หัวข้อนี้จะอธิบายทีละขั้นตอนเกี่ยวกับวิธีแยกข้อความออกจากรูปภาพใน C# อย่างรวดเร็วและง่ายดาย เมื่อใช้ Aspose.OCR for .NET คุณสามารถอ่านอักขระจากรูปภาพใน C# ได้ในไม่กี่ขั้นตอน
ขั้นตอนในการดึงข้อความจากรูปภาพใน C#
- ใช้แพ็คเกจ NuGet Aspose.OCR for .NET
- รวมข้อมูลอ้างอิง 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(); | |
} | |
} | |
} |
วิธีการนี้เหมือนกันกับแอปพลิเคชัน .NET ทุกประเภท หากคุณใช้ภาษา C# ไม่ว่าคุณจะต้องการแยกข้อความทั้งหมดออกจากรูปภาพ หรืออ่านข้อความทีละบรรทัดจากรูปภาพ Aspose.OCR สำหรับ .NET สามารถช่วยคุณได้