天天看点

VB.NET 纯代码生成code39条形码

ode39是条形码的一种,也被称为3 of 9 code、USD-3或者LOGMARS,由于编制简单、能够对任意长度的数据进行编码、支持设备广泛等特性而被广泛采用。

VB.NET 纯代码生成code39条形码

下面是code39编码规则

39码编码对映表(英文字母部分)

字元 逻辑型态 字元 逻辑型态
A 110101001011 N 101011010011
B 101101001011 O 110101101001
C 110110100101 P 101101101001
D 101011001011 Q 101010110011
E 110101100101 R 110101011001
F 101101100101 S 101101011001
G 101010011011 T 101011011001
H 110101001101 U 110010101011
I 101101001101 V 100110101011
J 101011001101 W 110011010101
K 110101010011 X 100101101011
L 101101010011 Y 110010110101
M 110110101001

39码编码对映表(数字与特殊符号部分)

字元 逻辑型态 字元 逻辑型态
101001101101 100101001001
1 110100101011 100101011011
2 101100101011 100101101101
3 110110010101 100100101001
4 101001101011 101001001001
5 110100110101 100100100101
6 101100110101 110010101101
7 101001011011 空白 100110101101
8 110100101101
9 101100101101
Public Class CD39

    Public Shared Function CODE39(ByVal str As String, ByVal W As Integer, ByVal H As Integer) As String
        '"CODE39码 12位 编码规则"
        Dim HBL As New Hashtable From {
            {"A"c, "110101001011"},
            {"B"c, "101101001011"},
            {"C"c, "110110100101"},
            {"D"c, "101011001011"},
            {"E"c, "110101100101"},
            {"F"c, "101101100101"},
            {"G"c, "101010011011"},
            {"H"c, "110101001101"},
            {"I"c, "101101001101"},
            {"J"c, "101011001101"},
            {"K"c, "110101010011"},
            {"L"c, "101101010011"},
            {"M"c, "110110101001"},
            {"N"c, "101011010011"},
            {"O"c, "110101101001"},
            {"P"c, "101101101001"},
            {"Q"c, "101010110011"},
            {"R"c, "110101011001"},
            {"S"c, "101101011001"},
            {"T"c, "101011011001"},
            {"U"c, "110010101011"},
            {"V"c, "100110101011"},
            {"W"c, "110011010101"},
            {"X"c, "100101101011"},
            {"Y"c, "110010110101"},
            {"Z"c, "100110110101"},
            {"0"c, "101001101101"},
            {"1"c, "110100101011"},
            {"2"c, "101100101011"},
            {"3"c, "110110010101"},
            {"4"c, "101001101011"},
            {"5"c, "110100110101"},
            {"6"c, "101100110101"},
            {"7"c, "101001011011"},
            {"8"c, "110100101101"},
            {"9"c, "101100101101"},
            {"+"c, "100101001001"},
            {"-"c, "100101011011"},
            {"*"c, "100101101101"},
            {"/"c, "100100101001"},
            {"%"c, "101001001001"},
            {"$"c, "100100100101"},
            {"."c, "110010101101"},
            {" "c, "100110101101"}
        }

        ''"CODE39码 9位 编码规则"
        'Dim HBL As New Hashtable From {
        '    {"0"c, "000110100"},
        '    {"1"c, "100100001"},
        '    {"2"c, "001100001"},
        '    {"3"c, "101100000"},
        '    {"4"c, "000110001"},
        '    {"5"c, "100110000"},
        '    {"6"c, "001110000"},
        '    {"7"c, "000100101"},
        '    {"8"c, "100100100"},
        '    {"9"c, "001100100"},
        '    {"A"c, "100001001"},
        '    {"B"c, "001001001"},
        '    {"C"c, "101001000"},
        '    {"D"c, "000011001"},
        '    {"E"c, "100011000"},
        '    {"F"c, "001011000"},
        '    {"G"c, "000001101"},
        '    {"H"c, "100001100"},
        '    {"I"c, "001001100"},
        '    {"J"c, "000011100"},
        '    {"K"c, "100000011"},
        '    {"L"c, "001000011"},
        '    {"M"c, "101000010"},
        '    {"N"c, "000010011"},
        '    {"O"c, "100010010"},
        '    {"P"c, "001010010"},
        '    {"Q"c, "000000111"},
        '    {"R"c, "100000110"},
        '    {"S"c, "001000110"},
        '    {"T"c, "000010110"},
        '    {"U"c, "110000001"},
        '    {"V"c, "011000001"},
        '    {"W"c, "111000000"},
        '    {"X"c, "010010001"},
        '    {"Y"c, "110010000"},
        '    {"Z"c, "011010000"},
        '    {"-"c, "010000101"},
        '    {"."c, "110000100"},
        '    {" "c, "011000100"},
        '    {"*"c, "010010100"},
        '    {"$"c, "010101000"},
        '    {"/"c, "010100010"},
        '    {"+"c, "010001010"},
        '    {"%"c, "000101010"}
        '}

        str = "*" & str.ToUpper() & "*" ''显示的内容

        Dim RES As String = ""
        '二进制串
        Try
            For Each ch As Char In str
                RES += HBL(ch).ToString()
                '间隔,与一个单位的线条宽度相等
                RES &= "0"
            Next
        Catch
            Return "错误:不能存在中文或不予许的字符"
        End Try

        Dim RS As String = ""
        '颜色
        For Each c As Char In RES
            'HTML代码
            Dim color As String = If(c = "0"c, "#FFFFFF", "#000000")
            RS &= "<div style=""width:" & W & "px;height:" & H & "px;float:left;background:" & color & ";""></div>"
        Next
        RS &= "<div style=""clear:both""></div>"

        Dim len As Integer = HBL("*"c).ToString().Length
        For Each c As Char In str
            RS &= "<div style=""width:" & (W * (len + 1)) & "px;float:left;color:#000000;text-align:center;"">" & c + "</div>"
        Next
        Return "<div style=""background:#FFFFFF;padding:5px;font-size:" & (W * 10) & "px;font-family:'楷体';"">" & RS & "</div>"
    End Function


End Class           

复制

使用方法:直接生成到WebBrowser容器上

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        WebBrowser1.DocumentText = CD39.CODE39(TextBox1.Text, 2, 1)

    End Sub

End Class           

复制

谢谢关注与支持!