Click here to Skip to main content
Click here to Skip to main content

EAN13 Barcode Control

By , 16 Sep 2008
 

ean13barcode.JPG

Introduction

In 2005, most of my work was barcode related programs, because I was writing programs for hand held devices which came with an integrated barcode reader. It was exciting to know about the barcode technology and how third party tools drew these bars. So I started looking for some resources to understand the algorithm used to generate barcodes. I discovered a lot of barcode types and each type had its own algorithm. My goal was to create my own barcode controls.

In the beginning of 2006, I started with this EAN13 barcode. After that, I paused because I found other exciting technologies. I know there are many articles in CodeProject that talk about the EAN 13 barcode, but I would like to share this with you.

Using the Code

This function initializes the barcode table which we will use to draw the bars:

Public Sub InitEAN13Tables()
    '          Zero
    Tables(0).TableA = "0001101"
    Tables(0).TableB = "0100111"
    Tables(0).TableC = "1110010"
    '          One
    Tables(1).TableA = "0011001"
    Tables(1).TableB = "0110011"
    Tables(1).TableC = "1100110"
    '          Two
    Tables(2).TableA = "0010011"
    Tables(2).TableB = "0011011"
    Tables(2).TableC = "1101100"
    '          Three
    Tables(3).TableA = "0111101"
    Tables(3).TableB = "0100001"
    Tables(3).TableC = "1000010"
    '          Four
    Tables(4).TableA = "0100011"
    Tables(4).TableB = "0011101"
    Tables(4).TableC = "1011100"
    '          Five
    Tables(5).TableA = "0110001"
    Tables(5).TableB = "0111001"
    Tables(5).TableC = "1001110"
    '          Six
    Tables(6).TableA = "0101111"
    Tables(6).TableB = "0000101"
    Tables(6).TableC = "1010000"
    '          Seven
    Tables(7).TableA = "0111011"
    Tables(7).TableB = "0010001"
    Tables(7).TableC = "1000100"
    '          Eight
    Tables(8).TableA = "0110111"
    Tables(8).TableB = "0001001"
    Tables(8).TableC = "1001000"
    '          Nine
    Tables(9).TableA = "0001011"
    Tables(9).TableB = "0010111"
    Tables(9).TableC = "1110100"
End Sub

The function CalculateCheckSum calculates the checksum code for the barcode:

Private Function CalculateCheckSum() As Boolean
    Dim X As Integer = 0
    Dim Y As Integer = 0
    Dim j As Integer = 11
    Try
        For i As Integer = 1 To 12
            If i Mod 2 = 0 Then
                X += Val(m_BarcodeText(j))
            Else
                Y += Val(m_BarcodeText(j))
            End If
            j -= 1
        Next

        Dim Z As Integer = X + (3 * Y)
        'first way
        m_CheckSum = ((10 - (Z Mod 10)) Mod 10)
        'second way
        'Dim M As Integer = Z
        ' Do Until (M Mod 10 = 0)
        'M += 1
        ' Loop
        'm_CheckSum = M - Z

        Return True
    Catch ex As Exception
        MessageBox.Show(ex.Message)
        Return False
    End Try
End Function

CalculateValue is the main function which calculates the barcode values depending on the barcode table:

Private Function CalculateValue() As Boolean
    ' Clear any previous Value
    BarcodeValue = New StringBuilder(95)
    Try
        ' Add The Start Mark
        BarcodeValue.Append(StartMark)
        Select Case m_BarcodeText(0)
            Case "0"
                For i As Integer = 1 To 6
                    BarcodeValue.Append(Tables(Val(m_BarcodeText(i))).TableA)
                Next
            Case "1"
                For i As Integer = 1 To 6
                    If (i = 1) Or (i = 2) Or (i = 4) Then
                        BarcodeValue.Append(Tables(Val(m_BarcodeText(i))).TableA)
                    Else
                        BarcodeValue.Append(Tables(Val(m_BarcodeText(i))).TableB)
                    End If
                Next
            Case "2"
                For i As Integer = 1 To 6
                    If (i = 1) Or (i = 2) Or (i = 5) Then
                        BarcodeValue.Append(Tables(Val(m_BarcodeText(i))).TableA)
                    Else
                        BarcodeValue.Append(Tables(Val(m_BarcodeText(i))).TableB)
                    End If
                Next
            Case "3"
                For i As Integer = 1 To 6
                    If (i = 1) Or (i = 2) Or (i = 6) Then
                        BarcodeValue.Append(Tables(Val(m_BarcodeText(i))).TableA)
                    Else
                        BarcodeValue.Append(Tables(Val(m_BarcodeText(i))).TableB)
                    End If
                Next
            Case "4"
                For i As Integer = 1 To 6
                    If (i = 1) Or (i = 3) Or (i = 4) Then
                        BarcodeValue.Append(Tables(Val(m_BarcodeText(i))).TableA)
                    Else
                        BarcodeValue.Append(Tables(Val(m_BarcodeText(i))).TableB)
                    End If
                Next
            Case "5"
                For i As Integer = 1 To 6
                    If (i = 1) Or (i = 4) Or (i = 5) Then
                        BarcodeValue.Append(Tables(Val(m_BarcodeText(i))).TableA)
                    Else
                        BarcodeValue.Append(Tables(Val(m_BarcodeText(i))).TableB)
                    End If
                Next
            Case "6"
                For i As Integer = 1 To 6
                    If (i = 1) Or (i = 5) Or (i = 6) Then
                        BarcodeValue.Append(Tables(Val(m_BarcodeText(i))).TableA)
                    Else
                        BarcodeValue.Append(Tables(Val(m_BarcodeText(i))).TableB)
                    End If
                Next
            Case "7"
                For i As Integer = 1 To 6
                    If (i = 1) Or (i = 3) Or (i = 5) Then
                        BarcodeValue.Append(Tables(Val(m_BarcodeText(i))).TableA)
                    Else
                        BarcodeValue.Append(Tables(Val(m_BarcodeText(i))).TableB)
                    End If
                Next
            Case "8"
                For i As Integer = 1 To 6
                    If (i = 1) Or (i = 3) Or (i = 6) Then
                        BarcodeValue.Append(Tables(Val(m_BarcodeText(i))).TableA)
                    Else
                        BarcodeValue.Append(Tables(Val(m_BarcodeText(i))).TableB)
                    End If
                Next
            Case "9"
                For i As Integer = 1 To 6
                    If (i = 1) Or (i = 4) Or (i = 6) Then
                        BarcodeValue.Append(Tables(Val(m_BarcodeText(i))).TableA)
                    Else
                        BarcodeValue.Append(Tables(Val(m_BarcodeText(i))).TableB)
                    End If
                Next
        End Select
        ' Add The Splitting Mark
        BarcodeValue.Append(SplittingMark)

        For i As Integer = 7 To (m_BarcodeText.Length - 1)
            BarcodeValue.Append(Tables(Val(m_BarcodeText(i))).TableC)
        Next
        ' Add Checksum
        BarcodeValue.Append(Tables(CheckSum).TableC)
        ' Add The End Mark
        BarcodeValue.Append(EndMark)

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Function

Points of Interest

This barcode control was created just for fun and is not a professional control. We can add many properties to this. I think the bar sizes need some extra handling to enable printing in deferent sizes. Maybe if I have some time, I will write controls for other barcode types like pdf417.

Enjoy :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Tammam Koujan
Software Developer (Senior)
Syrian Arab Republic Syrian Arab Republic
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionWHEN SCAN THE BARCODE ITS RETURN 13 DIGIT ? WHY PinmemberMember 773082111 Jan '13 - 19:12 
AnswerRe: WHEN SCAN THE BARCODE ITS RETURN 13 DIGIT ? WHY PinmemberTammam Koujan12 Jan '13 - 12:39 
Questionfor coding barcode PinmemberVinay Punamiya31 Dec '12 - 6:41 
GeneralGood job. PinmemberDan Maresca19 Dec '12 - 13:58 
GeneralMy vote of 5 PinmemberDan Maresca19 Dec '12 - 13:57 
Good working code, needs a Text field for underneath but you can add that yourself.
 
Good job Tammam
Questiongenerate EAN13 in C#.NET PinmemberKeepDynamic9 Jan '12 - 4:16 
GeneralMy vote of 1 Pinmembercodesoftconsult10 Jun '09 - 8:16 
GeneralRe: My vote of 1 PinmemberTammam Koujan22 Jul '09 - 13:24 
Generalwhy it's limited in 12 digits? how can i use pdf417 code create a pdf417 image?Thank you Pinmemberc_zhi6 Nov '08 - 14:58 
GeneralRe: why it's limited in 12 digits? how can i use pdf417 code create a pdf417 image?Thank you Pinmemberwgodsky28 Oct '09 - 17:19 
JokeMar7aba 1,000,000 PinmemberJamal Alqabandi23 Sep '08 - 0:51 
GeneralRe: Mar7aba 1,000,000 PinmemberTammam Koujan23 Sep '08 - 1:02 
GeneralNo source code PinmemberRai Shahid17 Sep '08 - 17:37 
GeneralRe: No source code PinmemberTammam Koujan17 Sep '08 - 18:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 17 Sep 2008
Article Copyright 2008 by Tammam Koujan
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid