You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用VB.NET从GS1 DataMatrix条码提取GTIN、批次号、序列号、有效期

提取GS1 DataMatrix条码数据的VB.NET实现

核心逻辑

GS1 DataMatrix条码由应用标识符(AI)+对应数据段组成,常见AI的规则如下:

  • 01:GTIN,固定14位数据
  • 10:批次号(Lot),可变长度(直到下一个AI出现)
  • 17:有效期,固定6位数据(格式为YYMMDD)
  • 21:序列号(SN),可变长度(直到条码结束)

VB.NET 实现代码

以下是可直接嵌入窗体程序的提取函数:

' 提取GS1 DataMatrix条码中的指定字段
Private Function ExtractGS1Data(barcodeStr As String) As Dictionary(Of String, String)
    Dim result As New Dictionary(Of String, String)()
    Dim currentIndex As Integer = 0

    While currentIndex < barcodeStr.Length
        ' 读取当前应用标识符(AI)
        Dim ai As String = barcodeStr.Substring(currentIndex, 2)
        currentIndex += 2

        Select Case ai
            Case "01" ' GTIN:固定14位
                If currentIndex + 14 <= barcodeStr.Length Then
                    result("GTIN") = barcodeStr.Substring(currentIndex, 14)
                    currentIndex += 14
                End If
            Case "10" ' 批次号:可变长度,直到下一个AI
                Dim lotEndIndex As Integer = currentIndex
                ' 查找下一个合法AI的起始位置
                While lotEndIndex + 2 <= barcodeStr.Length AndAlso Char.IsDigit(barcodeStr(lotEndIndex))
                    Dim nextAi As String = barcodeStr.Substring(lotEndIndex, 2)
                    If {"01", "10", "17", "21"}.Contains(nextAi) Then Exit While
                    lotEndIndex += 1
                End If
                If lotEndIndex > currentIndex Then
                    result("BN") = barcodeStr.Substring(currentIndex, lotEndIndex - currentIndex)
                    currentIndex = lotEndIndex
                End If
            Case "17" ' 有效期:固定6位
                If currentIndex + 6 <= barcodeStr.Length Then
                    result("EXP") = barcodeStr.Substring(currentIndex, 6)
                    currentIndex += 6
                End If
            Case "21" ' 序列号:到条码结束
                If currentIndex < barcodeStr.Length Then
                    result("SN") = barcodeStr.Substring(currentIndex)
                    currentIndex = barcodeStr.Length
                End If
            Case Else ' 未知AI:跳过当前数据段(可根据GS1规范扩展)
                currentIndex += 1
        End Select
    End While

    Return result
End Function

窗体中调用示例

在按钮点击事件中调用函数并展示结果:

Private Sub btnExtractData_Click(sender As Object, e As EventArgs) Handles btnExtractData.Click
    Dim sampleBarcode As String = "010628510100040910220044172502202110002367416670"
    Dim extractedData = ExtractGS1Data(sampleBarcode)

    ' 赋值到窗体控件
    txtGTIN.Text = extractedData("GTIN")
    txtBatch.Text = extractedData("BN")
    txtExpiry.Text = extractedData("EXP")
    txtSerial.Text = extractedData("SN") ' 示例中SN为10002367416670
End Sub

可选扩展

如果需要将有效期转换为标准日期格式,可添加以下代码:

If extractedData.ContainsKey("EXP") Then
    Dim expRaw As String = extractedData("EXP")
    Dim expiryDate As New DateTime(2000 + Integer.Parse(expRaw.Substring(0,2)), 
                                   Integer.Parse(expRaw.Substring(2,2)), 
                                   Integer.Parse(expRaw.Substring(4,2)))
    txtExpiryFormatted.Text = expiryDate.ToString("yyyy-MM-dd")
End If

内容的提问来源于stack exchange,提问作者يحيى حيدر

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 12:52:39