An example of Android producing commodity barcodes

Source: Internet
Author: User
Tags character set

Brief introduction
Commodity barcode, or EAN is the European standard barcode. The barcode is used as the unique identifier of the manufacturer of goods and commodities, and is the superset of the United States standard UPC. The EAN-13 consists of the first 12 digits and the 13th digit as the check digit. The 13th digit is calculated from the first 12 digits according to a certain formula.
Today, all fields have bar code applications. If you are writing code for a living, your program needs to support the bar code is sooner or later. For example, the supermarket member discounts system, the user takes the barcode which the handset obtains as the unique identification, obtains the discount. Another example is the Starbucks payment system in the payment system. In this article, we will discuss how to generate EAN-13 barcodes in an Android system.


EAN-13 Calculation Base

EAN-13 is the use of the commodity code, the 13th bit as a check bit. The check digit is generated with the first 12 digits. The algorithm for generating a check bit is as follows (data is counted from left to right):


P1 = number of even digits and


P2 = number of odd digits and


Z = P1 + 3 * P2


R = take single-digit digits as Z values


Check digit = R-z


Let's take a look at the coding structure of EAN-13:


First not to participate in the calculation


Then six digits encoded according to the first digit


The last six-bit rule conversion of the following columns


First left six-bit right six-bit


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


Bit code:


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


Application


Article tail, I posted a 12-digit number to generate EAN-13 barcode Ean13codebuilder class. This class can replace each of the generated 13-digit digits with a special font that can be displayed to the screen. The font includes special characters, such as $,+,! and the encoded l,r,g of the 10 digits mentioned above. The EAN-13 bar code must be generated in the following way: The start qualifier (depending on the first digit)-left six-bit-middle separator-right six-terminator (! )


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 *!


8 8 I i +!


9 9 J J,!


What we need to do is just generate the correct string with numbers and show it in a special font.


The use of this class is very simple. It passes the original string to the constructor, and then calls the GetCode () method to retrieve the EAN-13 barcode. The source code for this class is as follows:


The code is as follows Copy Code
public class Ean13codebuilder {


Private String Codestringvalue;


Private String Generatedcode;





Public Ean13codebuilder (String codestring)


{


Codestringvalue = codestring;


Parse ();


}





Public String GetCode ()


{


return generatedcode;





}





////////////////////////////////////////////////////////////////


This is generates EAN 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 = ten-(chetval+nechetval)%10;


if (Controlnumber =) 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 encoded string


Algorithm can is 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);





}


}

Code
To generate an online barcode and show it to an Android screen, you might want to generate the barcode yourself and display it on the screen with special characters. In order to set the character set in the TextView part of an Android system, you need to place a file under the/assets of the engineering file, load the typeface, and set typeface to TextView:

The code is as follows Copy Code

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 the activity is a. */


@Override


public void OnCreate (Bundle icicle) {


Super.oncreate (Icicle);


ToDo Add your GUI initialization code here





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 ());


}


}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.