Android Development: Generating an EAN13 Barcode

Thursday Feb 24th 2011 by Slava Archibasov
Share:

Read along as we look at the process of generating an EAN13 bar code on the Android screen.

Introduction

The European Article Number, or EAN, is a standard European barcode which is designed to encode the identification of a product and its manufacturer and is a superset of the American standard, UPC. The format of the EAN13 barcode encodes a string of 12 characters followed by a 13th character which acts as the control number and is calculated through a formula based on the first 12 characters.

Nowadays, barcodes are used everywhere. If you earn a living as a programmer, sooner or later the need of support of barcodes in your applications will come up. For example, it may be supermarket bonus system, where user is identified by barcode from his phone to get discount, or such payment systems as Starbucks moble payment network. In this article will be explained how to generate EAN13 barcodes on Android screen.

EAN13 Calculation Basics

EAN13 barcode uses  a 12 digits product code, the thirteenth digit is a control number, which is calculated on the basis of first 12 digits. Calculation of control number ( digits numbered from right to left ) looks like this:

• P1 = the sum of even digits
• P2 = the sum of odd digits
• Z = P1 + 3 * P2
• R = the number divisible by 10 immediately superior to Z
• Control number = R - Z

Next, let's see on EAN13 encoding system:

• The first digit is not encoded
• Each 6 following digit is encoded according to the first digit
• The 6 last digits are converted by the strict rule

 First digit Left 6 digits Right 6 digits 0 LLLLLL RRRRRR 1 LLGLGG RRRRRR 2 LLGGLG RRRRRR 3 LLGGGL RRRRRR 4 LGLLGG RRRRRR 5 LGGLLG RRRRRR 6 LGGGLL RRRRRR 7 LGLGLG RRRRRR 8 LGLGGL RRRRRR 9 LGGLGL RRRRRR

Encoding for the digits:

 Digit L - code G - code R - code 0 0001101 0100111 1110010 1 0011001 0110011 1100110 2 0010011 0011011 1101100 3 0111101 0100001 1000010 4 0100011 0011101 1011100 5 0110001 0111001 1001110 6 0101111 0000101 1010000 7 0111011 0010001 1000100 8 0110111 0001001 1001000 9 0001011 0010111 1110100

Implementation

So, we have the EAN13CodeBuilder, which is a class for encoding sequences of 12 numbers into a string of text in the EAN-13 barcode, which can then be displayed on the screen using a special font, in which each character is replaced by a bar code.This font contain few special characters like \$, +, !, and the sets of the 10 digits for the codes L,R and G, described above. An EAN13 barcode string must be build up in the following way: start delimiter ( depends on first digit ) - left 6 symbols - middle delimiter (-) - right 6 symbols - end delimiter (!)

 Digit L - code G - code R -code Start delimiter 0 0 A a #! 1 1 B b \$! 2 2 C c %! 3 3 D d &! 4 4 E e '! 5 5 F f (! 6 6 G g )! 7 7 H h *! 8 8 I i +! 9 9 J j ,!

We only need to generate proper string from digits and display it with this special font.

The usage of this class is very simple. It delivers the original string into the constructor, after which the method getcode() brings the string back into the EAN-13. The class' source code looks like this:

```/*
* (C) 2011 Slava Archibasov
* http://plaincodesource.blogspot.com
*
*/

public class EAN13CodeBuilder {
private String codeStringValue;
private String generatedCode;

public EAN13CodeBuilder(String codeString)
{
codeStringValue = codeString;
parse();
}

public String getCode()
{
return generatedCode;
}

////////////////////////////////////////////////////////////////
// this method generates EAN 13 control number ans returns full
// string to encode

private String getFullCode()
{

int chetVal = 0, nechetVal = 0;
String codeToParse = codeStringValue;

for( int index = 0;index<6;index++ )
{

chetVal += Integer.valueOf(codeToParse.substring(index*2+1,index*2+2)).intValue();

nechetVal += Integer.valueOf(codeToParse.substring(index*2,index*2+1)).intValue();

}

chetVal *= 3;
int controlNumber = 10 - (chetVal+nechetVal)%10;
if( controlNumber == 10 ) controlNumber  = 0;

codeToParse += String.valueOf(controlNumber);

return codeToParse;
}

private String DigitToUpperCase( String digit)
{

String letters  = "ABCDEFGHIJ";
int position = Integer.valueOf(digit).intValue();

String retVal = letters.substring(position,position+1);

return retVal;
}

private String DigitToLowerCase( String digit)
{
String letters  = "abcdefghij";
int position = Integer.valueOf(digit).intValue();

String retVal = letters.substring(position,position+1);
return retVal;
}

//////////////////////////////////////////////
// this method generates EAN 13 encoded string
// algorithm can be found at http://en.wikipedia.org/wiki/EAN-13

private String createEAN13Code(String rawCode)
{
int firstFlag = Integer.valueOf(
rawCode.substring(0,1)
).intValue();

String leftString = rawCode.substring(1,7);
String rightString = rawCode.substring(7);

String rightCode = "";
String leftCode = "";

for( int i=0;i<6;i++)
{
rightCode += DigitToLowerCase( rightString.substring(i,i+1) );
}

if( firstFlag == 0 )
{

leftCode = "#!"+leftString.substring(0,1)
+leftString.substring(1,2)
+leftString.substring(2,3)
+leftString.substring(3,4)
+leftString.substring(4,5)
+leftString.substring(5);

}

if( firstFlag == 1 )
{

leftCode = "\$!"+leftString.substring(0,1)
+leftString.substring(1,2)
+DigitToUpperCase(leftString.substring(2,3))
+leftString.substring(3,4)
+DigitToUpperCase(leftString.substring(4,5))
+DigitToUpperCase(leftString.substring(5));
}
if( firstFlag == 2 )
{
leftCode = "%!"+leftString.substring(0,1)
+leftString.substring(1,2)
+DigitToUpperCase(leftString.substring(2,3))
+DigitToUpperCase(leftString.substring(3,4))
+leftString.substring(4,5)
+DigitToUpperCase(leftString.substring(5));
}

if( firstFlag == 3 )
{

leftCode = "&!"+leftString.substring(0,1)
+leftString.substring(1,2)
+DigitToUpperCase(leftString.substring(2,3))
+DigitToUpperCase(leftString.substring(3,4))
+DigitToUpperCase(leftString.substring(4,5))
+leftString.substring(5);
}

if( firstFlag == 4 )

{

leftCode = "'!"+leftString.substring(0,1)
+DigitToUpperCase(leftString.substring(1,2))
+leftString.substring(2,3)
+leftString.substring(3,4)
+DigitToUpperCase(leftString.substring(4,5))
+DigitToUpperCase(leftString.substring(5));
}

if( firstFlag == 5 )
{

leftCode = "(!"+leftString.substring(0,1)
+DigitToUpperCase(leftString.substring(1,2))
+DigitToUpperCase(leftString.substring(2,3))
+leftString.substring(3,4)
+leftString.substring(4,5)
+DigitToUpperCase(leftString.substring(5));

}

if( firstFlag == 6 )
{

leftCode = ")!"+leftString.substring(0,1)
+DigitToUpperCase(leftString.substring(1,2))
+DigitToUpperCase(leftString.substring(2,3))
+DigitToUpperCase(leftString.substring(3,4))
+leftString.substring(4,5)
+leftString.substring(5);

}

if( firstFlag == 7 )
{
leftCode = "*!"+leftString.substring(0,1)
+DigitToUpperCase(leftString.substring(1,2))
+leftString.substring(2,3)
+DigitToUpperCase(leftString.substring(3,4))
+leftString.substring(4,5)
+DigitToUpperCase(leftString.substring(5));
}

if( firstFlag == 8 )
{

leftCode = "+!"+leftString.substring(0,1)
+DigitToUpperCase(leftString.substring(1,2))
+leftString.substring(2,3)
+DigitToUpperCase(leftString.substring(3,4))
+DigitToUpperCase(leftString.substring(4,5))
+leftString.substring(5);
}

if( firstFlag == 9 )
{

leftCode = ",!"+leftString.substring(0,1)
+DigitToUpperCase(leftString.substring(1,2))
+DigitToUpperCase(leftString.substring(2,3))
+leftString.substring(3,4)
+DigitToUpperCase(leftString.substring(4,5))
+leftString.substring(5);
}

String retVal = leftCode + "-" + rightCode + "!";
return retVal;
}

private void parse()
{
String fullString = getFullCode();
System.out.println( "Full code: " + fullString );
generatedCode = createEAN13Code(fullString);
System.out.println( "Generated code: " + generatedCode );
}
}

```

Using the Code

To generate a bar code line and bring it to the Android screen, you should generate barcode string itself and show it on the screen with one of barcode fonts. To set font to TextView widget in Android, place ttf file in /assets project folder,  load  Typeface , and set this Typeface to TextView.

You can use the following code, for example:

```import android.app.Activity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import 	android.graphics.Typeface;

public class AndroidEAN13Activity extends Activity {

/** Called when the activity is first created. */

@Override
public void onCreate(Bundle icicle) {

super.onCreate(icicle);
this.setContentView(R.layout.main);
TextView t = (TextView)findViewById(R.id.barcode);

// set barcode font for TextView. ttf file must be placed is assets/fonts
Typeface font = Typeface.createFromAsset(this.getAssets(), "fonts/EanP72Tt Normal.Ttf");

t.setTypeface(font);
// generate barcode string

EAN13CodeBuilder bb = new EAN13CodeBuilder("124958761310");

t.setText(bb.getCode());

}

}
```