如何從流中讀取
串流數據是指連續的二進位資訊流,可以進行讀取或寫入。 在程式設計和數據處理的背景下,資料流被用來有效處理可能太大而無法完全適合內存的數據。 串流允許以較小、可管理的塊進行數據讀寫。
IronOcr的導入方法也接受要導入和讀取的圖像數據流。 這可以通過將流數據傳入其中一個導入方法來完成。 此方法將處理導入影像所需的所有步驟。
開始使用IronOCR
立即在您的專案中使用IronOCR,並享受免費試用。
如何從流中讀取
- 下載一個用於從流中讀取的C#庫
- 獲取並準備圖像流數據
- 將影像流傳遞給 OcrImageInput 構造函數導入圖像
- 使用
讀取
執行 OCR 的方法 - 通過指定裁剪區域來定義閱讀區域
讀取串流範例
首先,實例化 IronTesseract 類以執行 OCR。 使用 AnyBitmap 的 FromFile
方法來導入圖像文件。這個 AnyBitmap 物件將能夠將圖像數據轉換成流。 接下來,使用 'using' 語句通過 AnyBitmap 物件的 GetStream
方法傳遞圖像流來創建 OcrImageInput 物件。 最後使用 Read
方法來執行 OCR。
:path=/static-assets/ocr/content-code-examples/how-to/input-streams-read-streams.cs
using IronOcr;
using IronSoftware.Drawing;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Read image file to AnyBitmap
AnyBitmap anyBitmap = AnyBitmap.FromFile("Potter.tiff");
// Import image stream
using var imageInput = new OcrImageInput(anyBitmap.GetStream());
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);
Imports IronOcr
Imports IronSoftware.Drawing
' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()
' Read image file to AnyBitmap
Private anyBitmap As AnyBitmap = AnyBitmap.FromFile("Potter.tiff")
' Import image stream
Private imageInput = New OcrImageInput(anyBitmap.GetStream())
' Perform OCR
Private ocrResult As OcrResult = ocrTesseract.Read(imageInput)
指定掃描區域
要在大型圖像上提升效能並從特定區域獲得特定讀數,您可以使用 CropRectangle 類別。 OcrImageInput 建構子接受 CropRectangle 物件作為第二個參數。 這允許您指定應讀取影像文件的哪個區域。 在下面的程式碼範例中,我指定只讀取章節號碼和標題區域。
:path=/static-assets/ocr/content-code-examples/how-to/input-streams-read-specific-region.cs
using IronOcr;
using IronSoftware.Drawing;
using System;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Read image file to AnyBitmap
AnyBitmap anyBitmap = AnyBitmap.FromFile("Potter.tiff");
// Specify crop region
Rectangle scanRegion = new Rectangle(800, 200, 900, 400);
// Add image
using var imageInput = new OcrImageInput(anyBitmap.GetStream(), ContentArea: scanRegion);
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);
// Output the result to console
Console.WriteLine(ocrResult.Text);
Imports IronOcr
Imports IronSoftware.Drawing
Imports System
' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()
' Read image file to AnyBitmap
Private anyBitmap As AnyBitmap = AnyBitmap.FromFile("Potter.tiff")
' Specify crop region
Private scanRegion As New Rectangle(800, 200, 900, 400)
' Add image
Private imageInput = New OcrImageInput(anyBitmap.GetStream(), ContentArea:= scanRegion)
' Perform OCR
Private ocrResult As OcrResult = ocrTesseract.Read(imageInput)
' Output the result to console
Console.WriteLine(ocrResult.Text)