IronBarcode 无法识别 MSI 条码
Curtis Chau
更新: 2026年7月6日
问题
当使用IronBarcode库创建MSI条码时,有时BarcodeReader.Read方法无法读取生成的MSI条码,导致扫描结果为空且无法获取条码中的预期值。
解决方案
为了确保BarcodeReader.Read能够读取MSI条码,我们必须为Read方法提供第二个可选的BarcodeReaderOptions参数。 在BarcodeReaderOptions中,我们通过指定值ExpectBarcodeTypes明确指定我们试图读取的条码类型。 这样,Read方法能够识别由IronBarcode生成的MSI条码以及任何外部的MSI条码。
下面是如何将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"
Dim myBarcode = BarcodeWriter.CreateBarcode("12345", BarcodeWriterEncoding.MSI)
' Save barcode as image
myBarcode.SaveAsImage("msi.png")
' Reading MSI
Dim barcodeReaderOptions = New BarcodeReaderOptions With {
' Assigning BarcodeEncoding.MSI to ExpectBarcodeType to ensure that IronBarcode expects MSI type barcodes specifically
.ExpectBarcodeTypes = BarcodeEncoding.MSI
}
' Read barcode with additional barcodeReaderOptions from above
Dim barcodeResults = BarcodeReader.Read("msi.png", barcodeReaderOptions)
' Using a For Each loop and print out the result
For Each result As BarcodeResult In barcodeResults
Console.WriteLine(result.Text)
' Output: 12345
Next' 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 = New BarcodeReaderOptions With {
' Assigning BarcodeEncoding.MSI to ExpectBarcodeType to ensure that IronBarcode expects MSI type barcodes specifically
.ExpectBarcodeTypes = BarcodeEncoding.MSI
}
' Read barcode with additional barcodeReaderOptions from above
Dim barcodeResults = BarcodeReader.Read("msi.png", barcodeReaderOptions)
' Using a For Each loop and print out the result
For Each result As BarcodeResult In barcodeResults
Console.WriteLine(result.Text)
' Output: 12345
Next在该示例中,我们首先实例化一个新的BarcodeReaderOptions变量,然后将ExpectBarcodeTypes分配给BarcodeEncoding.MSI枚举,指示IronBarcode改为期望MSI条码。 之后,我们在for循环中打印出条码的值,该值为12345,因为barcodeResults返回一个BarcodeResults数组,并循环遍历每个结果以获取文本值。
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。