如何读取数据流
流数据指的是可以从中读取或写入的连续二进制信息流。 在编程和数据处理的背景下,流被用来高效处理可能太大而无法完全放入内存的数据。 流允许以较小、易于管理的块读写数据。
IronOcr的导入方法也接受要导入并读取的图像数据流。 这可以通过简单地将流数据传递给其中一个导入方法来完成。 该方法将处理导入图像所需的所有步骤。
开始使用IronOCR
立即在您的项目中开始使用IronOCR,并享受免费试用。
如何读取数据流
- 下载用于从流中读取的C#库
- 获取并准备图像流数据
- 将图像流传递给OcrImageInput 构造函数以导入图像
- 使用
Read
方法执行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)
OCR结果
