Click here to Skip to main content
11,706,455 members (36,666 online)
Click here to Skip to main content

EAN13 Barcode Control

, 3 Aug 2013 CPOL 44K 4.2K 37
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)

Share

About the Author

Tammam Koujan
Software Developer (Senior)
Syrian Arab Republic Syrian Arab Republic
010011000110100101101011011001010010000001000011011011110110010001101001011011100110011100100001

You may also be interested in...

Comments and Discussions

 
QuestionEAN 8 Pin
Stanislaw Smolen30-Dec-14 11:55
memberStanislaw Smolen30-Dec-14 11:55 
AnswerRe: EAN 8 Pin
Tammam Koujan3-Jan-15 22:07
professionalTammam Koujan3-Jan-15 22:07 
SuggestionExport to JPG and make the EAN 13 class as independent in the same solution in Vb NET Pin
exodus8216-Jul-14 10:48
memberexodus8216-Jul-14 10:48 
GeneralRe: Export to JPG and make the EAN 13 class as independent in the same solution in Vb NET Pin
Tammam Koujan16-Jul-14 14:43
professionalTammam Koujan16-Jul-14 14:43 
QuestionResizing Printed Barcode Pin
Currington19-Mar-14 1:55
memberCurrington19-Mar-14 1:55 
AnswerRe: Resizing Printed Barcode Pin
Tammam Koujan16-Jul-14 14:47
professionalTammam Koujan16-Jul-14 14:47 
QuestionCreating barcodes of type ean-13 in c# windows forms with sql server 2008 Pin
Member 1024876813-Feb-14 22:44
memberMember 1024876813-Feb-14 22:44 
AnswerRe: Creating barcodes of type ean-13 in c# windows forms with sql server 2008 Pin
Tammam Koujan22-Feb-14 8:56
professionalTammam Koujan22-Feb-14 8:56 
GeneralMy vote of 5 Pin
Polinia5-Aug-13 1:57
memberPolinia5-Aug-13 1:57 
GeneralRe: My vote of 5 Pin
Tammam Koujan17-Feb-14 22:52
professionalTammam Koujan17-Feb-14 22:52 
QuestionHow to resize the barcode in image Pin
nash_ph_4117-Jun-13 21:00
membernash_ph_4117-Jun-13 21:00 
AnswerRe: How to resize the barcode in image Pin
Tammam Koujan23-Jul-13 0:51
memberTammam Koujan23-Jul-13 0:51 
AnswerRe: How to resize the barcode in image Pin
Tammam Koujan3-Aug-13 13:04
professionalTammam Koujan3-Aug-13 13:04 
QuestionWHEN SCAN THE BARCODE ITS RETURN 13 DIGIT ? WHY Pin
Member 773082111-Jan-13 19:12
memberMember 773082111-Jan-13 19:12 
AnswerRe: WHEN SCAN THE BARCODE ITS RETURN 13 DIGIT ? WHY Pin
Tammam Koujan12-Jan-13 12:39
memberTammam Koujan12-Jan-13 12:39 
Questionfor coding barcode Pin
Vinay Punamiya31-Dec-12 6:41
memberVinay Punamiya31-Dec-12 6:41 
GeneralGood job. Pin
Dan Maresca19-Dec-12 13:58
memberDan Maresca19-Dec-12 13:58 
GeneralMy vote of 5 Pin
Dan Maresca19-Dec-12 13:57
memberDan Maresca19-Dec-12 13:57 
GeneralMy vote of 1 Pin
codesoftconsult10-Jun-09 8:16
membercodesoftconsult10-Jun-09 8:16 
GeneralRe: My vote of 1 Pin
Tammam Koujan22-Jul-09 13:24
memberTammam Koujan22-Jul-09 13:24 
JokeMar7aba 1,000,000 Pin
Jamal Alqabandi23-Sep-08 0:51
memberJamal Alqabandi23-Sep-08 0:51 
GeneralRe: Mar7aba 1,000,000 Pin
Tammam Koujan23-Sep-08 1:02
memberTammam Koujan23-Sep-08 1:02 
GeneralNo source code Pin
Rai Shahid17-Sep-08 17:37
memberRai Shahid17-Sep-08 17:37 
GeneralRe: No source code Pin
Tammam Koujan17-Sep-08 18:42
memberTammam 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 | Terms of Use | Mobile
Web03 | 2.8.150819.1 | Last Updated 3 Aug 2013
Article Copyright 2008 by Tammam Koujan
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid