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

EAN13 Barcode Control

, 3 Aug 2013
Rate this:
Please Sign up or sign in to vote.
Demonstrates creating EAN-13 Barcodes with VB.NET

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

Printing 

In real life you need to print barcode lables to use them on the products and item, I had updated the control to support printing , also you can show/hide bracode value under the barcode bars.

 

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 Smile | :)   

Changelog 

Version 0.1 :

  • Initial release. 

Version 0.2 :  

  • Added  Show barcode number under barcode bars. 
  • Added Barcode printing support. 

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
Syrian Developer work and live in Dubai - UAE, Focusing on .NET technologies and data aware applications.
Follow on   LinkedIn

Comments and Discussions

 
QuestionResizing Printed Barcode PinmemberCurrington19-Mar-14 1:55 
QuestionCreating barcodes of type ean-13 in c# windows forms with sql server 2008 PinmemberMember 1024876813-Feb-14 22:44 
AnswerRe: Creating barcodes of type ean-13 in c# windows forms with sql server 2008 PinprofessionalTammam Koujan22-Feb-14 8:56 
GeneralMy vote of 5 PinmemberPolinia5-Aug-13 1:57 
GeneralRe: My vote of 5 PinprofessionalTammam Koujan17-Feb-14 22:52 
QuestionHow to resize the barcode in image Pinmembernash_ph_4117-Jun-13 21:00 
AnswerRe: How to resize the barcode in image PinmemberTammam Koujan23-Jul-13 0:51 
AnswerRe: How to resize the barcode in image PinprofessionalTammam Koujan3-Aug-13 13:04 
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 
GeneralMy vote of 1 Pinmembercodesoftconsult10-Jun-09 8:16 
GeneralRe: My vote of 1 PinmemberTammam Koujan22-Jul-09 13:24 
you are tracing my articles to give a low rate
Arabic Soundex[^]
 
you can do some positive actions and post your own articles
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.

| Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 3 Aug 2013
Article Copyright 2008 by Tammam Koujan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid