关于code128.ttf字体显示条形码

增补:code128A、code128B


说明:

code128A:数字+大写

code128B:数字+大小写

code128C:仅仅数字。(码图最短,A和B一样)


Public Class Form1


    Function ToCode128A(ByVal s As String) As String

        Dim ns As String

        Dim total As Long

        ns = ""

        If Len(s) > 0 Then

            total = 103 ' Start Code

            Dim i As Integer, l As Integer

            l = Len(s)

            For i = 0 To l - 1

                Dim v = Mid(s, i + 1, 1)

                ns = ns + v

                total = total + (i + 1) * (Asc(v) - 32)

            Next

            Dim checkCode As String '生成验证码

            checkCode = "" & (total Mod 103)

            ns = getChar(103) + ns + getChar(checkCode) + getChar(106)

            ToCode128A = ns

        Else

            ToCode128A = ""

        End If

    End Function



    Function ToCode128B(ByVal s As String) As String

        Dim ns As String

        Dim total As Long

        ns = ""

        If Len(s) > 0 Then

            total = 104 ' Start Code

            Dim i As Integer, l As Integer

            l = Len(s)

            For i = 0 To l - 1

                Dim v = Mid(s, i + 1, 1)

                ns = ns + v

                total = total + (i + 1) * (Asc(v) - 32)

            Next

            Dim checkCode As String '生成验证码

            checkCode = "" & (total Mod 103)

            ns = getChar(104) + ns + getChar(checkCode) + getChar(106)

            ToCode128B = ns

        Else

            ToCode128B = ""

        End If

    End Function


    Function ToCode128C(ByVal s As String) As String

        ' zhuyiwen 2010.10.12

        ' 使用code128.ttf字体生成Code128C条码

        Dim ns As String

        Dim total As Long

        ns = ""

        If Len(s) > 0 And IsNumeric(s) Then

            total = 105 ' Start Code

            If Len(s) Mod 2 > 0 Then s = s + "0" ' 不足偶数位补0

            Dim i As Integer, l As Integer

            l = Len(s) \ 2

            For i = 0 To l - 1

                ns = ns + getChar(Mid(s, i * 2 + 1, 2))

                total = total + (i + 1) * Val(Mid(s, i * 2 + 1, 2))

            Next

            Dim checkCode As String '生成验证码

            checkCode = "" & (total Mod 103)

            ns = getChar(105) + ns + getChar(checkCode) + getChar(106)

            ToCode128C = ns

        Else

            ToCode128C = ""

        End If

    End Function


    Function getChar(ByVal s As String) As String

        Dim c As Integer

        c = Val(s)

        If c = 0 Then

            getChar = " "

        ElseIf c < 95 Then

            REM Asc("!") = 33

            getChar = ChrW(32 + c)

        Else

            getChar = ChrW(100 + c)

        End If

    End Function


    


    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        TextBox1.Text = "000040000000000000000008"

        'TextBox1.Text = "95270078"

    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        TextBox2.Text = ToCode128A(TextBox1.Text)

    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click

        TextBox2.Text = ToCode128B(TextBox1.Text)

    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click

        TextBox2.Text = ToCode128C(TextBox1.Text)

    End Sub

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/11273.html