คอร์ API
API หลัก
หน้านี้ครอบคลุมวงจรชีวิตของอ็อบเจ็กต์และพฤติกรรมการยกเว้นของสี่ประเภทหลัก: AsposeOcr, OcrInput, OcrOutput และ RecognitionResult.
วงจรชีวิตของ OcrInput
OcrInput implements IDisposable และควรสร้างขึ้นในคำสั่ง using ตัวสร้างของมันรับค่า InputType (InputType.SingleImage หรือ InputType.PDF) และ OcrInput.Add(string) จะคิวเส้นทางไฟล์หนึ่งไฟล์ต่อครั้ง — อินสแตนซ์ OcrInput เดียวกันสามารถเก็บไฟล์หลายไฟล์ที่เป็นประเภทอินพุตเดียวกันได้.
using Aspose.OCR;
using var input = new OcrInput(InputType.SingleImage);
input.Add("page-1.jpg");
input.Add("page-2.png");การจดจำและการอ่าน OcrOutput
AsposeOcr.Recognize(OcrInput) คืนค่า OcrOutput ซึ่งเป็น List<RecognitionResult> — สามารถทำดัชนีได้เช่นรายการทั่วไป, โดยมีหนึ่ง RecognitionResult ต่อภาพ (สำหรับ InputType.SingleImage, หนึ่งต่อไฟล์ที่เพิ่ม) หรือต่อหน้า PDF (สำหรับ InputType.PDF).
var api = new AsposeOcr();
OcrOutput output = api.Recognize(input);
RecognitionResult first = output[0];RecognitionResult และประเภท LinesResult ที่ซ้อนกัน
RecognitionResult.LinesResult เป็นคลาสที่ซ้อนกัน — อ้างอิงมันเป็น RecognitionResult.LinesResult, ไม่ใช่เป็นประเภทแยกส่วน แต่ละ RecognitionResult จะเปิดเผย:
RecognitionText— ข้อความที่ถูกจดจำครบถ้วนสำหรับภาพหรือหน้าดังกล่าว โดยบรรทัดจะเชื่อมต่อด้วยการขึ้นบรรทัดใหม่.RecognitionLinesResult— เป็นList<RecognitionResult.LinesResult>หนึ่งรายการต่อแต่ละบรรทัดข้อความที่ตรวจพบ โดยแต่ละรายการมีTextInLine(ข้อความของบรรทัด) และLine(เป็นSystem.Drawing.Rectangleที่ให้พิกเซลพิกัดของบรรทัดนั้นในภาพต้นฉบับ).
RecognitionResult result = output[0];
Console.WriteLine(result.RecognitionText);
foreach (RecognitionResult.LinesResult line in result.RecognitionLinesResult)
{
System.Drawing.Rectangle bounds = line.Line;
Console.WriteLine($"x={bounds.X}, y={bounds.Y}: {line.TextInLine}");
}ข้อยกเว้น
AsposeOcr.Recognize(OcrInput) ตรวจสอบความถูกต้องของข้อมูลเข้า ก่อนดำเนินการจดจำ:
| Condition | Exception |
|---|---|
input คือ null | ArgumentNullException |
input’s InputType ไม่ใช่ SingleImage หรือ PDF | NotSupportedException |
เส้นทางที่เพิ่มผ่าน OcrInput.Add() ไม่มีอยู่บนดิสก์ | FileNotFoundException |
การตรวจสอบเหล่านี้ทำงานก่อนการสรุปผลของโมเดลใด ๆ ดังนั้นข้อมูลเข้าไม่ถูกต้องจะล้มเหลวทันทีแทนที่จะเกิดหลังจากการจดจำบางส่วน.
เคล็ดลับและแนวทางปฏิบัติที่ดีที่สุด
OcrOutputว่างเปล่า (Count == 0) หากOcrInputไม่มีไฟล์ใด ๆ ถูกเพิ่ม — ตรวจสอบCountก่อนทำการจัดทำดัชนี.RecognitionResult.LinesResultอยู่ซ้อนภายในRecognitionResult; ชื่อประเภทระดับบนสุดLinesResultเพียงอย่างเดียวไม่ใช่ประเภทที่สามารถแก้ไขได้นอกเหนือจากการซ้อนนั้น.
ปัญหาทั่วไป
| ปัญหา | สาเหตุ | แก้ไข |
|---|---|---|
ArgumentNullException บน Recognize() | null ถูกส่งแทน OcrInput | ควรสร้าง OcrInput ก่อนเรียก Recognize() เสมอ |
NotSupportedException บน Recognize() | OcrInput ถูกสร้างด้วยประเภทอินพุตที่ไม่ใช่ SingleImage/PDF | เฉพาะ InputType.SingleImage และ InputType.PDF เท่านั้นที่เป็นค่าที่ถูกต้อง |
FileNotFoundException บน Recognize() | เส้นทางที่อยู่ในคิวไม่มีอยู่ | ตรวจสอบความถูกต้องของเส้นทางไฟล์ก่อนเรียก OcrInput.Add() |
FAQ
RecognitionResult.LinesResult เป็นคลาสระดับบนของตัวเองหรือไม่?
ไม่ — มันเป็นส่วนย่อยภายใน RecognitionResult. อ้างอิงเป็น RecognitionResult.LinesResult.
OcrOutput รับประกันลำดับหน้าให้กับ PDF หรือไม่?
ใช่ — สำหรับ InputType.PDF, OcrOutput จะคืนค่า RecognitionResult หนึ่งรายการต่อหน้าในลำดับของแหล่งที่ม.
จะเกิดอะไรขึ้นหากฉันเรียก Recognize() ด้วย OcrInput ว่าง?
มันจะคืนค่า OcrOutput ที่มี Count == 0 แทนที่จะโยนข้อผิดพลาด.
API Reference สรุป
| คลาส/เมธอด | คำอธิบาย |
|---|---|
AsposeOcr.Recognize(OcrInput) | ตรวจสอบและรับรู้; โยน ArgumentNullException, NotSupportedException หรือ FileNotFoundException เมื่ออินพุตไม่ถูกต้อง |
OcrInput.OcrInput(InputType) | สร้างคอนเทนเนอร์อินพุตสำหรับประเภทอินพุตหนึ่งประเภท |
OcrInput.Add(string) | จัดคิวเส้นทางไฟล์ |
OcrInput.Dispose() | ปล่อยทรัพยากร (ผ่าน IDisposable) |
OcrOutput | List<RecognitionResult> ที่ส่งคืนโดย Recognize() |
RecognitionResult.RecognitionText | ข้อความที่จดจำได้ทั้งหมดสำหรับภาพ/หน้าเดียว |
RecognitionResult.RecognitionLinesResult | List<RecognitionResult.LinesResult> |
RecognitionResult.LinesResult.TextInLine | ข้อความที่จดจำได้สำหรับบรรทัดเดียว |
RecognitionResult.LinesResult.Line | System.Drawing.Rectangle กล่องขอบสำหรับบรรทัดเดียว |