如何從流中讀取

This article was translated from English: Does it need improvement?
Translated
View the article in English

查克尼思·賓

串流數據是指連續的二進位資訊流,可以進行讀取或寫入。 在程式設計和數據處理的背景下,資料流被用來有效處理可能太大而無法完全適合內存的數據。 串流允許以較小、可管理的塊進行數據讀寫。

IronOcr的導入方法也接受要導入和讀取的圖像數據流。 這可以通過將流數據傳入其中一個導入方法來完成。 此方法將處理導入影像所需的所有步驟。

開始使用IronOCR

立即在您的專案中使用IronOCR,並享受免費試用。

第一步:
green arrow pointer



讀取串流範例

首先,實例化 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)
VB   C#

指定掃描區域

要在大型圖像上提升效能並從特定區域獲得特定讀數,您可以使用 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)
VB   C#

OCR結果

讀取特定區域
Chaknith related to OCR結果

查克尼思·賓

軟體工程師

Chaknith 是開發者界的夏洛克福爾摩斯。他第一次意識到自己可能有個軟體工程的未來,是在他為了娛樂而參加程式挑戰的時候。他的重點是 IronXL 和 IronBarcode,但他也引以為豪的是,他幫助客戶解決所有產品的問題。Chaknith 利用他與客戶直接對話中獲得的知識,以進一步改進產品。他的實際反饋超越了 Jira 工單,並支持產品開發、文件撰寫和行銷,以提升客戶的整體體驗。不在公司時,他通常在學習機器學習、寫程式和徒步旅行。