Results 1 to 12 of 12

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

Threaded View

  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.

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