获取 Barcode 128 编码的文本。我想使用条形码字体来显示它,而不是生成图像

Get Barcode 128 encoded text. I want to use a Barcode font to show it, not generate images

我想要一个 class 或函数(如果可能的话用 VB.NET 编写),以便将其添加到我的项目中。 此 class 或函数应该能够接收文本并且 return 它作为条形码 128 编码文本。

我找到了很多直接生成条码图像的解决方案,但我需要的只是将文本转换为 Standard Code 128 编码文本的部分,然后我将使用条码字体显示它。

我也找到了一些代码来做到这一点,但是 returned 编码的文本不是预期的,因为我无法用条形码 Reader 读取它,或者有时会出现一个字符无法用条码字体表示。

编码器应该可以转换为:

15868/039

进入这个:

Í/vÈ8/039[Î

因为这个在线转换器可以做到 https://www.bcgen.com/fontencoder/ 当你点击按钮 [Code 128]

我找到了我想要的。我在这里为那些有同样问题的用户分享代码。

我已经从 C# 翻译成 VB 我在互联网上找到的 Class (https://grandzebu.net/informatique/codbar-en/code128.htm)

它有一个“编码”功能,可以接收原始文本和 returns 编码文本。它非常适合用字体 Libre Barcode 128 (https://fonts.google.com/specimen/Libre+Barcode+128+Text)

表示
'
'Auteur:    Joffrey VERDIER
'Date :     08/2006
'Légal:     OpenSource © 2007 AVRANCHES
'
Imports System
Imports System.Collections.Generic
Imports System.Text

Public Class BarCode128

    Public Function Encode(chaine As String) As String

        Dim ind As Integer = 1
        Dim checksum As Integer = 0
        Dim mini As Integer
        Dim dummy As Integer
        Dim tableB As Boolean
        Dim code128 As String
        Dim longueur As Integer

        code128 = ""
        longueur = chaine.Length

        If longueur = 0 Then
            Console.WriteLine("\n chaine vide")
        Else
            For ind = 0 To longueur - 1
                If Asc(chaine(ind)) < 32 Or Asc(chaine(ind)) > 126 Then
                    Console.WriteLine("\n chaine invalide")
                End If
            Next
        End If

        tableB = True
        ind = 0

        While ind < longueur

            If tableB = True Then

                If ind = 0 Or (ind + 3) = longueur - 1 Then
                    mini = 4
                Else
                    mini = 6
                End If
                mini = mini - 1

                If (ind + mini) <= (longueur - 1) Then
                    While mini >= 0

                        If Asc(chaine(ind + mini)) < 48 Or Asc(chaine(ind + mini)) > 57 Then
                            Console.WriteLine("\n exit")
                            Exit While
                        End If
                        mini = mini - 1
                    End While
                End If

                If mini < 0 Then
                    If ind = 0 Then
                        code128 = Char.ToString(Chr(205))
                    Else
                        code128 = code128 & Char.ToString(Chr(199))
                    End If
                    tableB = False
                Else

                    If ind = 0 Then
                        code128 = Char.ToString(Chr(204))
                    End If
                End If

            End If

            If tableB = False Then
                mini = 2
                mini = mini - 1
                If (ind + mini) < longueur Then
                    While mini >= 0
                        If Asc(chaine(ind + mini)) < 48 Or Asc(chaine(ind)) > 57 Then
                            Exit While
                        End If
                        mini = mini - 1
                    End While
                End If
                If mini < 0 Then
                    dummy = Int32.Parse(chaine.Substring(ind, 2))
                    Console.WriteLine("\n  dummy ici : " & dummy)
                    If dummy < 95 Then
                        dummy = dummy + 32
                    Else
                        dummy = dummy + 100
                    End If
                    code128 = code128 & Chr(dummy)
                    ind = ind + 2
                Else
                    code128 = code128 & Char.ToString(Chr(200))
                    tableB = True
                End If
            End If


            If tableB = True Then
                code128 = code128 & chaine(ind)
                ind = ind + 1
            End If

        End While

        For ind = 0 To code128.Length - 1
            dummy = Asc(code128(ind))
            Console.WriteLine("\n  et voila dummy : " & dummy)
            If dummy < 127 Then
                dummy = dummy - 32
            Else
                dummy = dummy - 100
            End If
            If ind = 0 Then
                checksum = dummy
            End If
            checksum = (checksum + (ind) * dummy) Mod 103
        Next

        If checksum < 95 Then
            checksum = checksum + 32
        Else
            checksum = checksum + 100
        End If
        code128 = code128 & Char.ToString(Chr(checksum)) & Char.ToString(Chr(206))

        Return code128

    End Function

End Class