Search code examples
vb.netbarcode

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


I would like to get a class or function, if possible written in VB.NET, to add it to my projects. This class or function should be able to receive a text and return it as Barcode 128 encoded text.

I found lot of solutions for generating the barcode images directly, but what I need it's only the part which converts a text to a Standard Code 128 encoded text, then I will show it using a Barcode font.

I also found some codes to do it, but the returned encoded text is not the expected one, because I cannot read it with the Barcode Reader, or sometimes appears a char which cannot be represented by the barcode font.

The encoder should be able to convert this:

15868/039

Into this:

Í/vÈ8/039[Î

As this online converter can do it https://www.bcgen.com/fontencoder/ when you click on the button [Code 128]


Solution

  • I found what I wanted. I'm sharing here the code for those users who has the same problem.

    I've translated from C# to VB a Class that I found in Internet (https://grandzebu.net/informatique/codbar-en/code128.htm)

    It has an "Encode" function which receives the original text and returns the encoded text. It works perfect to be represented by the font 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