加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

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

(2013-05-06 17:44:09)
标签:

条形码

code128

it

分类: IT

增补: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

    
End Class

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有