Results 1 to 12 of 12

Thread: EAN-13,CODE39,UPC,CODE128 Code Bar Generator

  1. #1

    Thread Starter
    Lively Member matt3011's Avatar
    Join Date
    May 2002
    Location
    France
    Posts
    82

    EAN-13,CODE39,UPC,CODE128 Code Bar Generator

    Here is a code to produce a bar code which complies with EAN13 specifications. I adapted It from a JS Script by Ghislain Lavoie I found on this web site.

    VB Code:
    1. Public Class EAN13
    2.         Private bitsCode As ArrayList
    3.  
    4.         Public Sub New()
    5.             bitsCode = New ArrayList
    6.             bitsCode.Add(New String(3) {"0001101", "0100111", "1110010", "000000"})
    7.             bitsCode.Add(New String(3) {"0011001", "0110011", "1100110", "001011"})
    8.             bitsCode.Add(New String(3) {"0010011", "0011011", "1101100", "001101"})
    9.             bitsCode.Add(New String(3) {"0111101", "0100001", "1000010", "001110"})
    10.             bitsCode.Add(New String(3) {"0100011", "0011101", "1011100", "010011"})
    11.             bitsCode.Add(New String(3) {"0110001", "0111001", "1001110", "011001"})
    12.             bitsCode.Add(New String(3) {"0101111", "0000101", "1010000", "011100"})
    13.             bitsCode.Add(New String(3) {"0111011", "0010001", "1000100", "010101"})
    14.             bitsCode.Add(New String(3) {"0110111", "0001001", "1001000", "010110"})
    15.             bitsCode.Add(New String(3) {"0001011", "0010111", "1110100", "011010"})
    16.         End Sub
    17.  
    18.         Public Function Generate(ByVal Code As String) As Image
    19.             Dim a As Integer = 0
    20.             Dim b As Integer = 0
    21.             Dim imgCode As Image
    22.             Dim g As Graphics
    23.             Dim i As Integer
    24.             Dim bCode As Byte()
    25.             Dim bitCode As Byte()
    26.             Dim tmpFont As Font
    27.  
    28.             If Code.Length <> 12 Or Not IsNumeric(Code.Replace(".", "_").Replace(",", "_")) Then Throw New Exception("Le code doit être composé de 12 chiffres")
    29.  
    30.             ReDim bCode(12)
    31.             For i = 0 To 11
    32.                 bCode(i) = CInt(Code.Substring(i, 1))
    33.                 If (i Mod 2) = 1 Then
    34.                     b += bCode(i)
    35.                 Else
    36.                     a += bCode(i)
    37.                 End If
    38.             Next
    39.  
    40.             i = (a + (b * 3)) Mod 10
    41.             If i = 0 Then
    42.                 bCode(12) = 0
    43.             Else
    44.                 bCode(12) = 10 - i
    45.             End If
    46.             bitCode = getBits(bCode)
    47.  
    48.             tmpFont = New Font("times new roman", 14, FontStyle.Regular, GraphicsUnit.Pixel)
    49.             imgCode = New Bitmap(110, 50)
    50.             g = Graphics.FromImage(imgCode)
    51.             g.Clear(Color.White)
    52.  
    53.             g.DrawString(Code.Substring(0, 1), tmpFont, Brushes.Black, 2, 30)
    54.             a = g.MeasureString(Code.Substring(0, 1), tmpFont).Width
    55.  
    56.             For i = 0 To bitCode.Length - 1
    57.                 If i = 2 Then
    58.                     g.DrawString(Code.Substring(1, 6), tmpFont, Brushes.Black, a, 30)
    59.                 ElseIf i = 48 Then
    60.                     g.DrawString(Code.Substring(7, 5) & bCode(12).ToString, tmpFont, Brushes.Black, a, 30)
    61.                 End If
    62.  
    63.                 If i = 0 Or i = 2 Or i = 46 Or i = 48 Or i = 92 Or i = 94 Then
    64.                     If bitCode(i) = 1 Then 'noir
    65.                         g.DrawLine(Pens.Black, a, 0, a, 40)
    66.                         a += 1
    67.                     End If
    68.                 Else
    69.                     If bitCode(i) = 1 Then 'noir
    70.                         g.DrawLine(Pens.Black, a, 0, a, 30)
    71.                         a += 1
    72.                     Else 'blanc
    73.                         a += 1
    74.                     End If
    75.                 End If
    76.             Next
    77.             g.Flush()
    78.             Return imgCode
    79.         End Function
    80.  
    81.         Private Function getBits(ByVal bCode As Byte()) As Byte()
    82.             Dim i As Integer
    83.             Dim res As Byte()
    84.             Dim bits As String = "101"
    85.             Dim cle As String = bitsCode(bCode(0))(3)
    86.             For i = 1 To 6
    87.                 bits &= bitsCode(bCode(i))(CInt(cle.Substring(i - 1, 1)))
    88.             Next
    89.             bits &= "01010"
    90.             For i = 7 To 12
    91.                 bits &= bitsCode(bCode(i))(2)
    92.             Next
    93.             bits += "101"
    94.             ReDim res(bits.Length - 1)
    95.             For i = 0 To bits.Length - 1
    96.                 res(i) = Asc(bits.Chars(i)) - 48
    97.             Next
    98.             Return res
    99.         End Function
    100.  
    101.     End Class

    I didn't actually checked if it was really compliant with EAN13 but I trust Ghislain Lavoie when he says it is, I tested It now and it works.
    For UPC, it's just a EAN13 code bar starting with a 0
    Last edited by matt3011; Aug 5th, 2005 at 03:51 AM.

  2. #2

    Thread Starter
    Lively Member matt3011's Avatar
    Join Date
    May 2002
    Location
    France
    Posts
    82

    Re: EAN-13 Code Bar Generator

    I added 2 classes : 1 for Code39 And 1 for Code128 (CharacterSetB)

    I found Code39 on a French VB Forum and I updated It

    I coded Code128 from the specifications which can be found there

    Here is the code :
    VB Code:
    1. Public Class Code39
    2.         Public Function Generate(ByVal Code As String) As Image
    3.             Dim bmp As Image
    4.             Dim ValidInput As String = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%"
    5.             Dim ValidCodes As String = "4191459566472786097041902596264733841710595784729059950476626106644590602984801043246599624767444602600464775861090446866032248034439186013047842447705803036526582823575858090365863556658042365383495434978353624150635770"
    6.             Dim i As Integer
    7.  
    8.             If Code = "" Then Throw New Exception("Le code est incorrect")
    9.             For i = 0 To Code.Length - 1
    10.                 If ValidInput.IndexOf(Code.Substring(i, 1)) = -1 Then Throw New Exception("Le code est incorrect")
    11.             Next
    12.             Code = "*" & Code & "*"
    13.             ValidInput &= "*"
    14.  
    15.             bmp = New Bitmap(Code.Length * 16, 58)
    16.             Dim g As Graphics = Graphics.FromImage(bmp)
    17.             g.FillRectangle(New SolidBrush(Color.White), 0, 0, Code.Length * 16, 58)
    18.             Dim p As New Pen(Color.Black, 1)
    19.             Dim BarValue, BarX As Integer
    20.             Dim BarSlice As Short
    21.  
    22.             ' Create font and brush.
    23.             Dim drawFont As New Font("Arial", 8)
    24.             Dim drawBrush As New SolidBrush(Color.Black)
    25.             ' Create point for upper-left corner of drawing.
    26.             Dim x As Single = (Code.Length * 16) / 3
    27.             Dim y As Single = 42
    28.             ' Set format of string.
    29.             Dim drawFormat As New StringFormat
    30.             drawFormat.FormatFlags = StringFormatFlags.NoWrap
    31.  
    32.             For i = 0 To Code.Length - 1
    33.                 Try
    34.                     BarValue = Val(ValidCodes.Substring(ValidInput.IndexOf(Code.Substring(i, 1)) * 5, 5))
    35.                     If BarValue = 0 Then BarValue = 36538
    36.                     For BarSlice = 15 To 0 Step -1
    37.                         If BarValue >= 2 ^ BarSlice Then
    38.                             g.DrawLine(p, BarX, 0, BarX, 40)
    39.                             BarValue = BarValue - (2 ^ BarSlice)
    40.                         End If
    41.                         BarX += 1
    42.                     Next
    43.                 Catch
    44.                 End Try
    45.             Next
    46.             g.DrawString(Code, drawFont, drawBrush, x, y, drawFormat)
    47.  
    48.             Return bmp
    49.         End Function
    50.     End Class
    51.  
    52.     Public Class Code128_CharacterSetB
    53.         Private bitsCode As ArrayList
    54.  
    55.         Public Sub New()
    56.             bitsCode = New ArrayList
    57.             bitsCode.Add("11011001100")
    58.             bitsCode.Add("11001101100")
    59.             bitsCode.Add("11001100110")
    60.             bitsCode.Add("10010011000")
    61.             bitsCode.Add("10010001100")
    62.             bitsCode.Add("10001001100")
    63.             bitsCode.Add("10011001000")
    64.             bitsCode.Add("10011000100")
    65.             bitsCode.Add("10001100100")
    66.             bitsCode.Add("11001001000")
    67.             bitsCode.Add("11001000100")
    68.             bitsCode.Add("11000100100")
    69.             bitsCode.Add("10110011100")
    70.             bitsCode.Add("10011011100")
    71.             bitsCode.Add("10011001110")
    72.             bitsCode.Add("10111001100")
    73.             bitsCode.Add("10011101100")
    74.             bitsCode.Add("10011100110")
    75.             bitsCode.Add("11001110010")
    76.             bitsCode.Add("11001011100")
    77.             bitsCode.Add("11001001110")
    78.             bitsCode.Add("11011100100")
    79.             bitsCode.Add("11001110100")
    80.             bitsCode.Add("11101101110")
    81.             bitsCode.Add("11101001100")
    82.             bitsCode.Add("11100101100")
    83.             bitsCode.Add("11100100110")
    84.             bitsCode.Add("11101100100")
    85.             bitsCode.Add("11100110100")
    86.             bitsCode.Add("11100110010")
    87.             bitsCode.Add("11011011000")
    88.             bitsCode.Add("11011000110")
    89.             bitsCode.Add("11000110110")
    90.             bitsCode.Add("10100011000")
    91.             bitsCode.Add("10001011000")
    92.             bitsCode.Add("10001000110")
    93.             bitsCode.Add("10110001000")
    94.             bitsCode.Add("10001101000")
    95.             bitsCode.Add("10001100010")
    96.             bitsCode.Add("11010001000")
    97.             bitsCode.Add("11000101000")
    98.             bitsCode.Add("11000100010")
    99.             bitsCode.Add("10110111000")
    100.             bitsCode.Add("10110001110")
    101.             bitsCode.Add("10001101110")
    102.             bitsCode.Add("10111011000")
    103.             bitsCode.Add("10111000110")
    104.             bitsCode.Add("10001110110")
    105.             bitsCode.Add("11101110110")
    106.             bitsCode.Add("11010001110")
    107.             bitsCode.Add("11000101110")
    108.             bitsCode.Add("11011101000")
    109.             bitsCode.Add("11011100011")
    110.             bitsCode.Add("11011101110")
    111.             bitsCode.Add("11101011000")
    112.             bitsCode.Add("11101000110")
    113.             bitsCode.Add("11100010110")
    114.             bitsCode.Add("11101101000")
    115.             bitsCode.Add("11101100010")
    116.             bitsCode.Add("11100011010")
    117.             bitsCode.Add("11101111010")
    118.             bitsCode.Add("11001000010")
    119.             bitsCode.Add("11110001010")
    120.             bitsCode.Add("10100110000")
    121.             bitsCode.Add("10100001100")
    122.             bitsCode.Add("10010110000")
    123.             bitsCode.Add("10010000110")
    124.             bitsCode.Add("10000101100")
    125.             bitsCode.Add("10000100110")
    126.             bitsCode.Add("10110010000")
    127.             bitsCode.Add("10110000100")
    128.             bitsCode.Add("10011010000")
    129.             bitsCode.Add("10011000010")
    130.             bitsCode.Add("10000110100")
    131.             bitsCode.Add("10000110010")
    132.             bitsCode.Add("11000010010")
    133.             bitsCode.Add("11001010000")
    134.             bitsCode.Add("11110111010")
    135.             bitsCode.Add("11000010100")
    136.             bitsCode.Add("10001111010")
    137.             bitsCode.Add("10100111100")
    138.             bitsCode.Add("10010111100")
    139.             bitsCode.Add("10010011110")
    140.             bitsCode.Add("10111100100")
    141.             bitsCode.Add("10011110100")
    142.             bitsCode.Add("10011110010")
    143.             bitsCode.Add("11110100100")
    144.             bitsCode.Add("11110010100")
    145.             bitsCode.Add("11110010010")
    146.             bitsCode.Add("11011011110")
    147.             bitsCode.Add("11011110110")
    148.             bitsCode.Add("11110110110")
    149.             bitsCode.Add("10101111000")
    150.             bitsCode.Add("10100011110")
    151.             bitsCode.Add("10001011110")
    152.             bitsCode.Add("10111101000")
    153.             bitsCode.Add("10111100010")
    154.             bitsCode.Add("11110101000")
    155.             bitsCode.Add("11110100010")
    156.             bitsCode.Add("10111011110")
    157.             bitsCode.Add("10111101110")
    158.             bitsCode.Add("11101011110")
    159.             bitsCode.Add("11110101110")
    160.             bitsCode.Add("11010000100")
    161.             bitsCode.Add("11010010000")
    162.             bitsCode.Add("11010011100")
    163.             bitsCode.Add("1100011101011")
    164.         End Sub
    165.         Public Function Generate(ByVal Code As String) As Image
    166.             Dim bmp As Image
    167.             Dim ValidInput As String = " !""#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
    168.             Dim CheckSum As Integer
    169.             Dim BarCode As String
    170.             Dim i As Integer
    171.  
    172.             If Code = "" Then Throw New Exception("Le code est incorrect")
    173.             BarCode = bitsCode(104)
    174.             For i = 0 To Code.Length - 1
    175.                 If ValidInput.IndexOf(Code.Substring(i, 1)) = -1 Then Throw New Exception("Le code est incorrect")
    176.                 CheckSum += ((i + 1) * ValidInput.IndexOf(Code.Substring(i, 1)))
    177.                 BarCode &= bitsCode(ValidInput.IndexOf(Code.Substring(i, 1)))
    178.             Next
    179.             CheckSum += 104 'Start B
    180.             CheckSum = CheckSum Mod 103
    181.             BarCode &= bitsCode(CheckSum)
    182.             BarCode &= bitsCode(106) 'Stop symbol
    183.  
    184.             bmp = New Bitmap(BarCode.Length, 58)
    185.             Dim g As Graphics = Graphics.FromImage(bmp)
    186.             g.FillRectangle(New SolidBrush(Color.White), 0, 0, BarCode.Length, 58)
    187.             Dim p As New Pen(Color.Black, 1)
    188.             Dim BarX As Integer
    189.  
    190.             ' Create font and brush.
    191.             Dim drawFont As New Font("Arial", 8)
    192.             Dim drawBrush As New SolidBrush(Color.Black)
    193.             ' Create point for upper-left corner of drawing.
    194.             Dim y As Single = 42
    195.             ' Set format of string.
    196.             Dim drawFormat As New StringFormat
    197.             drawFormat.FormatFlags = StringFormatFlags.NoWrap
    198.             drawFormat.Alignment = StringAlignment.Center
    199.  
    200.             For i = 0 To BarCode.Length - 1
    201.                 Try
    202.                     If BarCode.Chars(i) = "1" Then g.DrawLine(p, BarX, 0, BarX, 40)
    203.                     BarX += 1
    204.                 Catch
    205.                 End Try
    206.             Next
    207.             g.DrawString(Code, drawFont, drawBrush, New RectangleF(0, y, BarCode.Length, 16), drawFormat)
    208.  
    209.             Return bmp
    210.         End Function
    211.     End Class

    Note that Code128 is CharacterSetB but you can easily update it so it uses Character Set A or C, all bit patterns are included in bitsCode.

  3. #3
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: EAN-13,CODE39,UPC,CODE128 Code Bar Generator

    Very Nice ! Thanks for putting this in.

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  4. #4
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    376

    Re: EAN-13,CODE39,UPC,CODE128 Code Bar Generator

    Could you please Zip it up and post an example project using VB2005 so we can see it in action? I'm very much interested in this.

    If it is not too much to ask of course.

    If you don't have the time at least give me the steps so I can do it myself.

    Thanks A LOT.....

  5. #5
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: EAN-13,CODE39,UPC,CODE128 Code Bar Generator

    You are awsome, this helped me so much.

  6. #6
    Member
    Join Date
    May 2010
    Location
    Abu Dhabi
    Posts
    60

    Re: EAN-13,CODE39,UPC,CODE128 Code Bar Generator

    Looks good... This might solve my most bar code related issues... Let's see...

  7. #7
    Lively Member polecat's Avatar
    Join Date
    Jul 2005
    Location
    Wolverhampton
    Posts
    83

    Re: EAN-13,CODE39,UPC,CODE128 Code Bar Generator

    piscis

    Re: EAN-13,CODE39,UPC,CODE128 Code Bar Generator
    Could you please Zip it up and post an example project using VB2005 so we can see it in action? I'm very much interested in this.

    If it is not too much to ask of course.

    If you don't have the time at least give me the steps so I can do it myself.

    Thanks A LOT.....

    I know this thread is old but I am trying to add the other Code 128 A and C any pointers? Here is the test app all original code from above.Code Bar Generator.zip

    Do I Just change this line
    Code:
    BarCode = bitsCode(104) 'Charset B
    to
    Code:
    BarCode = bitsCode(103) 'Charset A
    or
    Code:
    BarCode = bitsCode(105) 'Charset C
    Last edited by polecat; Feb 13th, 2013 at 10:58 AM. Reason: Added guess at change

  8. #8
    Lively Member
    Join Date
    Oct 2011
    Posts
    76

    Re: EAN-13,CODE39,UPC,CODE128 Code Bar Generator

    Dear All,

    Can any one tell me how to convert numbers to EAN-13 bar codes using the below code, usually i got series of numbers in notepad file and i need to convert those numbers to EAN-13 and show output in crystal report.
    I am using VB.net 2010.

    Code:
        Public Class EAN13
                Private bitsCode As ArrayList
         
                Public Sub New()
                    bitsCode = New ArrayList
                    bitsCode.Add(New String(3) {"0001101", "0100111", "1110010", "000000"})
                    bitsCode.Add(New String(3) {"0011001", "0110011", "1100110", "001011"})
                    bitsCode.Add(New String(3) {"0010011", "0011011", "1101100", "001101"})
                    bitsCode.Add(New String(3) {"0111101", "0100001", "1000010", "001110"})
                    bitsCode.Add(New String(3) {"0100011", "0011101", "1011100", "010011"})
                    bitsCode.Add(New String(3) {"0110001", "0111001", "1001110", "011001"})
                    bitsCode.Add(New String(3) {"0101111", "0000101", "1010000", "011100"})
                    bitsCode.Add(New String(3) {"0111011", "0010001", "1000100", "010101"})
                    bitsCode.Add(New String(3) {"0110111", "0001001", "1001000", "010110"})
                    bitsCode.Add(New String(3) {"0001011", "0010111", "1110100", "011010"})
                End Sub
         
                Public Function Generate(ByVal Code As String) As Image
                    Dim a As Integer = 0
                    Dim b As Integer = 0
                    Dim imgCode As Image
                    Dim g As Graphics
                    Dim i As Integer
                    Dim bCode As Byte()
                    Dim bitCode As Byte()
                    Dim tmpFont As Font
         
                    If Code.Length <> 12 Or Not IsNumeric(Code.Replace(".", "_").Replace(",", "_")) Then Throw New Exception("Le code doit être composé de 12 chiffres")
         
                    ReDim bCode(12)
                    For i = 0 To 11
                        bCode(i) = CInt(Code.Substring(i, 1))
                        If (i Mod 2) = 1 Then
                            b += bCode(i)
                        Else
                            a += bCode(i)
                        End If
                    Next
         
                    i = (a + (b * 3)) Mod 10
                    If i = 0 Then
                        bCode(12) = 0
                    Else
                        bCode(12) = 10 - i
                    End If
                    bitCode = getBits(bCode)
         
                    tmpFont = New Font("times new roman", 14, FontStyle.Regular, GraphicsUnit.Pixel)
                    imgCode = New Bitmap(110, 50)
                    g = Graphics.FromImage(imgCode)
                    g.Clear(Color.White)
         
                    g.DrawString(Code.Substring(0, 1), tmpFont, Brushes.Black, 2, 30)
                    a = g.MeasureString(Code.Substring(0, 1), tmpFont).Width
         
                    For i = 0 To bitCode.Length - 1
                        If i = 2 Then
                            g.DrawString(Code.Substring(1, 6), tmpFont, Brushes.Black, a, 30)
                        ElseIf i = 48 Then
                            g.DrawString(Code.Substring(7, 5) & bCode(12).ToString, tmpFont, Brushes.Black, a, 30)
                        End If
         
                        If i = 0 Or i = 2 Or i = 46 Or i = 48 Or i = 92 Or i = 94 Then
                            If bitCode(i) = 1 Then 'noir
                                g.DrawLine(Pens.Black, a, 0, a, 40)
                                a += 1
                            End If
                        Else
                            If bitCode(i) = 1 Then 'noir
                                g.DrawLine(Pens.Black, a, 0, a, 30)
                                a += 1
                            Else 'blanc
                                a += 1
                            End If
                        End If
                    Next
                    g.Flush()
                    Return imgCode
                End Function
         
                Private Function getBits(ByVal bCode As Byte()) As Byte()
                    Dim i As Integer
                    Dim res As Byte()
                    Dim bits As String = "101"
                    Dim cle As String = bitsCode(bCode(0))(3)
                    For i = 1 To 6
                        bits &= bitsCode(bCode(i))(CInt(cle.Substring(i - 1, 1)))
                    Next
                    bits &= "01010"
                    For i = 7 To 12
                        bits &= bitsCode(bCode(i))(2)
                    Next
                    bits += "101"
                    ReDim res(bits.Length - 1)
                    For i = 0 To bits.Length - 1
                        res(i) = Asc(bits.Chars(i)) - 48
                    Next
                    Return res
                End Function
         
            End Class

  9. #9
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: EAN-13,CODE39,UPC,CODE128 Code Bar Generator

    I found that it's way easier to download a font, rather than trying to draw bar codes. Do a search for EAN-13 fonts, there should be a free one, then install it and start using it.
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  10. #10
    Lively Member
    Join Date
    Oct 2011
    Posts
    76

    Re: EAN-13,CODE39,UPC,CODE128 Code Bar Generator

    Quote Originally Posted by wild_bill View Post
    I found that it's way easier to download a font, rather than trying to draw bar codes. Do a search for EAN-13 fonts, there should be a free one, then install it and start using it.
    wild_bill - Thanks for your reply...

    I tried your suggestion before posting this thread, when i convert the bar code its like (A) but i need as (B) please refer below images (A) and (B)

    Name:  Untitled.png
Views: 3984
Size:  8.9 KB

  11. #11
    Frenzied Member
    Join Date
    Jan 2010
    Posts
    1,103

    Re: EAN-13,CODE39,UPC,CODE128 Code Bar Generator

    Quote Originally Posted by tnncprojects View Post
    wild_bill - Thanks for your reply...

    I tried your suggestion before posting this thread, when i convert the bar code its like (A) but i need as (B) please refer below images (A) and (B)

    Name:  Untitled.png
Views: 3984
Size:  8.9 KB
    If Code.Length <> 12 in EAN13.vb???

    so called EAN13 means 13 numbers.

  12. #12
    New Member
    Join Date
    Oct 2014
    Posts
    3

    Re: EAN-13,CODE39,UPC,CODE128 Code Bar Generator

    Quote Originally Posted by tnncprojects View Post
    Dear All,

    Can any one tell me how to convert numbers to EAN-13 bar codes using the below code, usually i got series of numbers in notepad file and i need to convert those numbers to EAN-13 and show output in crystal report.
    I am using VB.net 2010.

    Code:
        Public Class EAN13
                Private bitsCode As ArrayList
         
                Public Sub New()
                    bitsCode = New ArrayList
                    bitsCode.Add(New String(3) {"0001101", "0100111", "1110010", "000000"})
                    bitsCode.Add(New String(3) {"0011001", "0110011", "1100110", "001011"})
                    bitsCode.Add(New String(3) {"0010011", "0011011", "1101100", "001101"})
                    bitsCode.Add(New String(3) {"0111101", "0100001", "1000010", "001110"})
                    bitsCode.Add(New String(3) {"0100011", "0011101", "1011100", "010011"})
                    bitsCode.Add(New String(3) {"0110001", "0111001", "1001110", "011001"})
                    bitsCode.Add(New String(3) {"0101111", "0000101", "1010000", "011100"})
                    bitsCode.Add(New String(3) {"0111011", "0010001", "1000100", "010101"})
                    bitsCode.Add(New String(3) {"0110111", "0001001", "1001000", "010110"})
                    bitsCode.Add(New String(3) {"0001011", "0010111", "1110100", "011010"})
                End Sub
         
                Public Function Generate(ByVal Code As String) As Image
                    Dim a As Integer = 0
                    Dim b As Integer = 0
                    Dim imgCode As Image
                    Dim g As Graphics
                    Dim i As Integer
                    Dim bCode As Byte()
                    Dim bitCode As Byte()
                    Dim tmpFont As Font
         
                    If Code.Length <> 12 Or Not IsNumeric(Code.Replace(".", "_").Replace(",", "_")) Then Throw New Exception("Le code doit être composé de 12 chiffres")
         
                    ReDim bCode(12)
                    For i = 0 To 11
                        bCode(i) = CInt(Code.Substring(i, 1))
                        If (i Mod 2) = 1 Then
                            b += bCode(i)
                        Else
                            a += bCode(i)
                        End If
                    Next
         
                    i = (a + (b * 3)) Mod 10
                    If i = 0 Then
                        bCode(12) = 0
                    Else
                        bCode(12) = 10 - i
                    End If
                    bitCode = getBits(bCode)
         
                    tmpFont = New Font("times new roman", 14, FontStyle.Regular, GraphicsUnit.Pixel)
                    imgCode = New Bitmap(110, 50)
                    g = Graphics.FromImage(imgCode)
                    g.Clear(Color.White)
         
                    g.DrawString(Code.Substring(0, 1), tmpFont, Brushes.Black, 2, 30)
                    a = g.MeasureString(Code.Substring(0, 1), tmpFont).Width
         
                    For i = 0 To bitCode.Length - 1
                        If i = 2 Then
                            g.DrawString(Code.Substring(1, 6), tmpFont, Brushes.Black, a, 30)
                        ElseIf i = 48 Then
                            g.DrawString(Code.Substring(7, 5) & bCode(12).ToString, tmpFont, Brushes.Black, a, 30)
                        End If
         
                        If i = 0 Or i = 2 Or i = 46 Or i = 48 Or i = 92 Or i = 94 Then
                            If bitCode(i) = 1 Then 'noir
                                g.DrawLine(Pens.Black, a, 0, a, 40)
                                a += 1
                            End If
                        Else
                            If bitCode(i) = 1 Then 'noir
                                g.DrawLine(Pens.Black, a, 0, a, 30)
                                a += 1
                            Else 'blanc
                                a += 1
                            End If
                        End If
                    Next
                    g.Flush()
                    Return imgCode
                End Function
         
                Private Function getBits(ByVal bCode As Byte()) As Byte()
                    Dim i As Integer
                    Dim res As Byte()
                    Dim bits As String = "101"
                    Dim cle As String = bitsCode(bCode(0))(3)
                    For i = 1 To 6
                        bits &= bitsCode(bCode(i))(CInt(cle.Substring(i - 1, 1)))
                    Next
                    bits &= "01010"
                    For i = 7 To 12
                        bits &= bitsCode(bCode(i))(2)
                    Next
                    bits += "101"
                    ReDim res(bits.Length - 1)
                    For i = 0 To bits.Length - 1
                        res(i) = Asc(bits.Chars(i)) - 48
                    Next
                    Return res
                End Function
         
            End Class
    You can use the ASCII Chart to convert numbers to EAN-13 barcodes,because every code corresponding to a number.This can be used not only EAN-13,but also code128,code39 and so on.This is the chart: http://www.ascii-code.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width