IronBarcode 无法识别 MSI 条码

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

问题

在使用 IronBarcode 库创建 MSI 条形码时,有时 BarcodeReader.Read 方法无法读取生成的 MSI 条形码,导致扫描结果为空,且无法获取条形码中的预期值。

解决方案

为了确保 BarcodeReader.Read 能读取 MSI BARCODE,我们必须为 Read 方法提供可选的辅助参数 BarcodeReaderOptions。 在 BarcodeReaderOptions 中,我们通过赋值 ExpectedBarcodeTypes 来明确指定要读取的 BARCODE 类型。 这样,Read 方法就能识别由 IronBarcode 生成的 MSI BARCODE,以及任何外部 MSI BARCODE。

以下是一个关于如何将 BarcodeReaderOptions 应用于 BarcodeReader.Read 的简短示例。

代码示例

// Creating MSI Barcode with the value "12345"
var myBarcode = BarcodeWriter.CreateBarcode("12345", BarcodeWriterEncoding.MSI);

// Save barcode as image
myBarcode.SaveAsImage("msi.png");

// Reading MSI
var barcodeReaderOptions = new BarcodeReaderOptions
{
     // Assigning BarcodeEncoding.MSI to ExpectBarcodeType to ensure that IronBarcode expects MSI type barcodes specifically 
  ExpectBarcodeTypes = BarcodeEncoding.MSI,
};

// Read barcode with additional barcodeReaderOptions from above
var barcodeResults = BarcodeReader.Read("msi.png", barcodeReaderOptions);

// Using a for loop and print out the result 
foreach (BarcodeResult result in barcodeResults)
{
  Console.WriteLine(result.Text);
    // Output: 12345 
}
// Creating MSI Barcode with the value "12345"
var myBarcode = BarcodeWriter.CreateBarcode("12345", BarcodeWriterEncoding.MSI);

// Save barcode as image
myBarcode.SaveAsImage("msi.png");

// Reading MSI
var barcodeReaderOptions = new BarcodeReaderOptions
{
     // Assigning BarcodeEncoding.MSI to ExpectBarcodeType to ensure that IronBarcode expects MSI type barcodes specifically 
  ExpectBarcodeTypes = BarcodeEncoding.MSI,
};

// Read barcode with additional barcodeReaderOptions from above
var barcodeResults = BarcodeReader.Read("msi.png", barcodeReaderOptions);

// Using a for loop and print out the result 
foreach (BarcodeResult result in barcodeResults)
{
  Console.WriteLine(result.Text);
    // Output: 12345 
}
' Creating MSI Barcode with the value "12345"
Dim myBarcode = BarcodeWriter.CreateBarcode("12345", BarcodeWriterEncoding.MSI)

' Save barcode as image
myBarcode.SaveAsImage("msi.png")

' Reading MSI
Dim barcodeReaderOptions As New BarcodeReaderOptions With {.ExpectBarcodeTypes = BarcodeEncoding.MSI}

' Read barcode with additional barcodeReaderOptions from above
Dim barcodeResults = BarcodeReader.Read("msi.png", barcodeReaderOptions)

' Using a for loop and print out the result 
For Each result As BarcodeResult In barcodeResults
  Console.WriteLine(result.Text)
	' Output: 12345 
Next result
$vbLabelText   $csharpLabel

在示例中,我们首先实例化一个新的 BarcodeReaderOptions 变量,然后将 ExpectedBarcodeTypes 赋值为 BarcodeEncoding.MSI 枚举值,以此指示 IronBarcode 改用 MSI 条形码。 随后,我们在 for 循环中打印 BarCode 的值(即 12345),因为 barcodeResults 返回的是 BarcodeResults 数组,因此需要遍历每个结果以获取文本值。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 2,240,258 | 版本: 2026.5 just released
Still Scrolling Icon

还在滚动吗?

想快速获得证据? PM > Install-Package BarCode
运行示例 观看您的字符串变成 BarCode。