ליבה API
ליבת API
דף זה מתאר את מחזור החיים של האובייקט ואת התנהגות החריגות של ארבעת הסוגים הליבתיים: AsposeOcr, OcrInput, OcrOutput ו-RecognitionResult.
מחזור החיים של OcrInput
OcrInput מיישם 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 תיבת גבול לשורה אחת |