如何從資料流讀取條碼

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

海里海西米·賓·奧馬



MemoryStream 是 .NET 框架中的一個類,提供了一種從存儲在內存中的流中讀取和寫入的方法。它是一種類型的流,可用於操作未存儲在物理文件中的數據,而是存儲在內存中。

除了從圖像文件或 PDF 文件中讀取條形碼外,IronBarcode 還擅長從流中讀取條形碼。作為應用程序中的一個出色 API,IronBarcode 能夠將 PDF 文檔或圖像流作為輸入,輸出流中條形碼的讀取結果。現在讓我們來看看如何實現這一點。

從影像串流讀取條碼

在本節中,我們將向您展示如何使用 IronBarcode 讀取影像串流,以及儲存在多個影像串流中的數據。 清單<>

:path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-streams-1.cs
using IronBarCode;
using IronSoftware.Drawing;
using System;
using System.Collections.Generic;
using System.IO;

List<MemoryStream> list = new List<MemoryStream>();
list.Add(AnyBitmap.FromFile("image1.jpg").ToStream());
list.Add(AnyBitmap.FromFile("image2.jpg").ToStream());
list.Add(AnyBitmap.FromFile("image3.png").ToStream());

var myBarcode = BarcodeReader.Read(list);

foreach (var barcode in myBarcode)
{
    Console.WriteLine(barcode.ToString());
}
Imports IronBarCode
Imports IronSoftware.Drawing
Imports System
Imports System.Collections.Generic
Imports System.IO

Private list As New List(Of MemoryStream)()
list.Add(AnyBitmap.FromFile("image1.jpg").ToStream())
list.Add(AnyBitmap.FromFile("image2.jpg").ToStream())
list.Add(AnyBitmap.FromFile("image3.png").ToStream())

Dim myBarcode = BarcodeReader.Read(list)

For Each barcode In myBarcode
	Console.WriteLine(barcode.ToString())
Next barcode
VB   C#

從上面的代碼片段中,我們可以看到IronBarcode可以接受對象,以及對象列表。 記憶體流 鍵入 BarcodeReader.Read() 方法並讀取流對象。在上面的代碼片段中,我們還介紹了我們的 免費、開源庫,IronDrawing 可用於將圖像轉換為 記憶體流 物件。然而,如果您已經有一張圖片或圖片清單作為<記憶體流>對象,您可以直接將其作為參數使用在 BarcodeReader.Read() to read the barcodes present in the stream.

從 PDF 文件流中讀取條碼

在本節中,我們將展示如何使用 IronBarcode 來讀取 PDF 文件。 記憶體流 物件或一個 記憶體流 PDF文件清單。

:path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-streams-2.cs
using IronBarCode;
using IronPdf;
using System;
using System.IO;

MemoryStream document = PdfDocument.FromFile(@"file_path.pdf").Stream;

var myBarcode = BarcodeReader.ReadPdf(document);

foreach (var value in myBarcode)
{
    Console.WriteLine(value.ToString());
}
Imports IronBarCode
Imports IronPdf
Imports System
Imports System.IO

Private document As MemoryStream = PdfDocument.FromFile("file_path.pdf").Stream

Private myBarcode = BarcodeReader.ReadPdf(document)

For Each value In myBarcode
	Console.WriteLine(value.ToString())
Next value
VB   C#

從上面的代碼片段可以看出,從 PDF 文件中讀取條碼的方式沒有太大差別。 記憶體流 读取條碼對象作為 記憶體流 物件。唯一的區別是從 PDF 文件中讀取條碼所使用的讀取方法,這是 BarcodeReader.ReadPdf() 接受單一 PDF 文件 記憶體流 對象。在上述的代碼片段中,我們也使用了 IronPDF 作為將 PDF 文件轉換為的幫手 記憶體流 物件。如果有多個 PDF 文件作為數據流並希望 IronBarcode 閱讀,建議將所有 PDF 文件合併成一個 PDF 文件流並將其傳入 BarcodeReader.ReadPdf() method. So, feel free to try it and manipulate the library the way you want!!

海里海西米·賓·奧馬

軟體工程師

和所有優秀的工程師一樣,Hairil 是一位熱衷學習的人。他正在精進自己對 C#、Python 和 Java 的知識,利用這些知識為 Iron Software 團隊的成員創造價值。Hairil 從馬來西亞的馬來西亞工藝大學加入了 Iron Software 團隊,他在那裡獲得了化學和過程工程學士學位。