codice:
package barcode;

import java.awt.*;

public class Ean extends java.applet.Applet {

  // codifiche binarie delle cifre decimali
  String[][] barcode = {
    {"0001101", "0011001", "0010011", "0111101", "0100011", 
     "0110001", "0101111", "0111011", "0110111", "0001011"}, // left hand A
    {"0100111", "0110011", "0011011", "0100001", "0011101", 
     "0111001", "0000101", "0010001", "0001001", "0010111"}, // left hand B
    {"1110010", "1100110", "1101100", "1000010", "1011100", 
     "1001110", "1010000", "1000100", "1001000", "1110100"}, // right hand
    {"101", "01010"}                                         // guide (laterale-centrale)
  };
  
  // masking del primo byte del flag sui 4 vettori barcode: 
  // 0=left hand a - 1=left hand b - 2=right hand - 3=barra guida
  int[][] maskMatrix = {
    {3,0,0,0,0,0,0,3,2,2,2,2,2,2,3}, //0
    {3,0,0,1,0,1,1,3,2,2,2,2,2,2,3}, //1
    {3,0,0,1,1,0,1,3,2,2,2,2,2,2,3}, //2
    {3,0,0,1,1,1,0,3,2,2,2,2,2,2,3}, //3
    {3,0,1,0,0,1,1,3,2,2,2,2,2,2,3}, //4
    {3,0,1,1,0,0,1,3,2,2,2,2,2,2,3}, //5
    {3,0,1,1,1,0,0,3,2,2,2,2,2,2,3}, //6
    {3,0,1,0,1,0,1,3,2,2,2,2,2,2,3}, //7
    {3,0,1,0,1,1,0,3,2,2,2,2,2,2,3}, //8
    {3,0,1,1,0,1,0,3,2,2,2,2,2,2,3}, //9
    {3,0,0,0,0,3,2,2,2,2,3} // ecco la compatibilita' ean8
  };
  
  String input;
  String output;
  String ean;
  String fgcolor;
  String bgcolor;
  int width;
  int height;
  boolean isValid = true;
  boolean hideMistakes = false;

  public void init() {
    input = getParameter("barcode");
    try {
      height = Integer.parseInt(getParameter("height"));
    } catch(Exception ex) {height = 40;}
    try {
      width = Integer.parseInt(getParameter("width"));
    } catch(Exception ex) {width = 100;}

    fgcolor = getParameter("color");
    if (fgcolor == null)
      fgcolor = getParameter("fgcolor");
    if (fgcolor == null)
      fgcolor = "#000000";

    bgcolor = getParameter("bgcolor");
    if (bgcolor == null)
      bgcolor = "#FFFFFF";
    if (("true".equals(getParameter("hidemistakes"))) ||
     ("true".equals(getParameter("hideMistakes"))))
      hideMistakes = true;

    output = input;
    createEAN();
    repaint();
  }

  public void paint(Graphics g) {
    g.setColor(Color.decode(bgcolor)); // background
    g.fillRect(0, 0, width, height);

    // le barre
    g.setColor(Color.decode(fgcolor));
    if (isValid)
      for (int i = 0, j = ean.length(); i < j; i++)
        if (ean.charAt(i) == '1')
          g.fillRect(i, 0, 1, height-12);

    // le cifre
    if (isValid || !hideMistakes) {
      g.setFont(new Font("Courier", Font.PLAIN, 12));
      g.drawString(output, 3, height-2);
    }
  }

  public void createEAN() {
    // valore di default come primo indice 
    // sulla matrice di masking nel caso di ean8
    int flagByte = 10;
    try {
      long test = Long.parseLong(input);
    } catch(NumberFormatException nfx) {
      isValid = false;
    }
    if (isValid) {
      if (input.length() == 13) {
	     // pesco il primo byte di flag, quello indicante 
	     // il primo indice sulla matrice di masking
	     flagByte = Integer.parseInt((new Character(input.charAt(0))).toString());
	     
        // ...e lo tolgo dalla stringa di input
        input = input.substring(1);
      }

      // inserisco le barre guida - 0=laterale - 1=centrale
      input = "0" + input.substring(0, input.length()/2) + "1" +
       input.substring(input.length()/2) + "0";

      ean = "";
      for (int i = 0; i < input.length(); i ++) {
	      // primo indice: pesco il byte indicante 
	      // che vettore interrogare per codificare la cifra corrente
	      int i1 = maskMatrix[flagByte][i];

	      // secondo indice: pesco la cifra corrente dall'input string
	      int i2 = Integer.parseInt((new Character(input.charAt(i))).toString());

	      ean += barcode[i1][i2];
      }
    }
  }
}