Function to create barcode (Code 39, output in SVG graphics)

VBobCat

Well-known member
Joined
Sep 6, 2011
Messages
137
Location
S?o Paulo, Brazil
Programming Experience
3-5
Hello People,

I work on an application which generates several custom reports, for which I chose the HTML format for output. Then, I needed to add a barcode identification in some of these reports, which go for printing. My working scenario excludes the possibility of working with TrueType barcode fonts, for my organization wouldn't allow me to install them, and I didn't manage to find in the web some finished and ready solution to my problem. This is partly my own fault, for I only understand code in VB, maybe a little JavaScript but...

So I wrote from scratch, with nothing but the Code 39 Specification itself, the function below. It is supposed to generate an SVG graphic (actually a XML species) which can be embedded in my HTML output. It worked fine and the printed samples were scanned ok.

Here it goes, for your analysis. I hope it becomes handy for someone who was in such a hurry as me. Please show your appreciation by detecting its flaws, improving it and spreading it freely. Thank you very much.

    Friend Function Barcode(ByVal Expr As String)
        Dim Chrs As String = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*"
        Dim Bar1 As String = "01010100101010100100101010010010101001000000"
        Dim Spc1 As String = "00000000000000000000000000000011111111111101"
        Dim Bar2 As String = "00110010010110010010011001001001100100100000"
        Dim Spc2 As String = "11111111110000000000000000000000000000011010"
        Dim Bar3 As String = "10001110000001110001000111000100011100000001"
        Dim Spc3 As String = "00000000001111111111000000000000000000010110"
        Dim Bar4 As String = "10000001110000001111000000111100000011100001"
        Dim Spc4 As String = "00000000000000000000111111111100000000001110"
        Dim Bar5 As String = "01101001001101001000110100100011010010000000"
        Dim Base As Double = 1
        Dim Unit As String = "px"
        Dim Heig As Double = Base * 30
        Dim Qiet As Double = Base * 10
        Dim PosX As Double = 0
        Dim PosY As Double = 0
        Dim Pitc As Double
        Dim StrB As New System.Text.StringBuilder
        StrB.Append("*"c)
        For Each c As Char In Expr.ToUpper.ToCharArray
            If Chrs.Contains(c) And Not c.Equals("*"c) Then StrB.Append(c)
        Next
        StrB.Append("*"c)
        Expr = StrB.ToString
        StrB.Clear()
        StrB.Append("<svg xmlns='http://www.w3.org/2000/svg' version='1.1'>")
        StrB.Append("<rect x='" & PosX.ToString("0.00").Replace(","c, "."c) & Unit &
            "' y='" & PosY.ToString("0.00").Replace(","c, "."c) & Unit &
            "' width='" & Qiet.ToString("0.00").Replace(","c, "."c) & Unit &
            "' height='" & Heig.ToString("0.00").Replace(","c, "."c) & Unit &
            "' style='fill:white' />")
        PosX += Qiet
        For Each c As Char In Expr.ToCharArray
            Pitc = Base * (1 + 1.5 * Val(Bar1.Chars(Chrs.IndexOf(c))))
            StrB.Append("<rect x='" & PosX.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' y='" & PosY.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' width='" & Pitc.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' height='" & Heig.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' style='fill:black' />")
            PosX += Pitc
            Pitc = Base * (1 + 1.5 * Val(Spc1.Chars(Chrs.IndexOf(c))))
            StrB.Append("<rect x='" & PosX.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' y='" & PosY.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' width='" & Pitc.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' height='" & Heig.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' style='fill:white' />")
            PosX += Pitc
            Pitc = Base * (1 + 1.5 * Val(Bar2.Chars(Chrs.IndexOf(c))))
            StrB.Append("<rect x='" & PosX.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' y='" & PosY.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' width='" & Pitc.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' height='" & Heig.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' style='fill:black' />")
            PosX += Pitc
            Pitc = Base * (1 + 1.5 * Val(Spc2.Chars(Chrs.IndexOf(c))))
            StrB.Append("<rect x='" & PosX.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' y='" & PosY.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' width='" & Pitc.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' height='" & Heig.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' style='fill:white' />")
            PosX += Pitc
            Pitc = Base * (1 + 1.5 * Val(Bar3.Chars(Chrs.IndexOf(c))))
            StrB.Append("<rect x='" & PosX.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' y='" & PosY.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' width='" & Pitc.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' height='" & Heig.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' style='fill:black' />")
            PosX += Pitc
            Pitc = Base * (1 + 1.5 * Val(Spc3.Chars(Chrs.IndexOf(c))))
            StrB.Append("<rect x='" & PosX.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' y='" & PosY.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' width='" & Pitc.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' height='" & Heig.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' style='fill:white' />")
            PosX += Pitc
            Pitc = Base * (1 + 1.5 * Val(Bar4.Chars(Chrs.IndexOf(c))))
            StrB.Append("<rect x='" & PosX.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' y='" & PosY.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' width='" & Pitc.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' height='" & Heig.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' style='fill:black' />")
            PosX += Pitc
            Pitc = Base * (1 + 1.5 * Val(Spc4.Chars(Chrs.IndexOf(c))))
            StrB.Append("<rect x='" & PosX.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' y='" & PosY.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' width='" & Pitc.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' height='" & Heig.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' style='fill:white' />")
            PosX += Pitc
            Pitc = Base * (1 + 1.5 * Val(Bar5.Chars(Chrs.IndexOf(c))))
            StrB.Append("<rect x='" & PosX.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' y='" & PosY.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' width='" & Pitc.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' height='" & Heig.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' style='fill:black' />")
            PosX += Pitc
            Pitc = Base
            StrB.Append("<rect x='" & PosX.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' y='" & PosY.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' width='" & Pitc.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' height='" & Heig.ToString("0.00").Replace(","c, "."c) & Unit &
                        "' style='fill:white' />")
            PosX += Pitc
        Next
        StrB.Append("<rect x='" & PosX.ToString("0.00").Replace(","c, "."c) & Unit &
            "' y='" & PosY.ToString("0.00").Replace(","c, "."c) & Unit &
            "' width='" & Qiet.ToString("0.00").Replace(","c, "."c) & Unit &
            "' height='" & Heig.ToString("0.00").Replace(","c, "."c) & Unit &
            "' style='fill:white' />")
        StrB.Append("</svg>")
        Return StrB.ToString
    End Function
 
For that implementation consider using a function with a signature like this:
Function GetRect(x As Double, y As Double, width As Double, height As Double, fill As String) As String
 
Back
Top