Nadesłany przez Tomasz Lubiński, 27 października 2005 01:00
Kod przedstawiony poniżej przedstawia główną część rozwiązania problemu.Pobierz pełne rozwiązanie.
Jeżeli nie odpowiada Ci sposób formatowania kodu przez autora skorzystaj z pretty printer'a i dostosuj go automatycznie do siebie.
BarCode.java:
/**
* @author Tomasz Lubinski
* (c)2005 algorytm.org
*
* EAN13 Bar Code
*/
public class BarCode {
private static final byte parityTable[][] = {
{0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 1, 1},
{0, 0, 1, 1, 0, 1},
{0, 0, 1, 1, 1, 0},
{0, 1, 0, 0, 1, 1},
{0, 1, 1, 0, 0, 1},
{0, 1, 1, 1, 0, 0},
{0, 1, 0, 1, 0, 1},
{0, 1, 0, 1, 1, 0},
{0, 1, 1, 0, 1, 0}
};
private static final byte right[][] = {
{1, 1, 1, 0, 0, 1, 0},
{1, 1, 0, 0, 1, 1, 0},
{1, 1, 0, 1, 1, 0, 0},
{1, 0, 0, 0, 0, 1, 0},
{1, 0, 1, 1, 1, 0, 0},
{1, 0, 0, 1, 1, 1, 0},
{1, 0, 1, 0, 0, 0, 0},
{1, 0, 0, 0, 1, 0, 0},
{1, 0, 0, 1, 0, 0, 0},
{1, 1, 1, 0, 1, 0, 0}
};
private static final byte left[][][] = {
{
{0, 0, 0, 1, 1, 0, 1},
{0, 0, 1, 1, 0, 0, 1},
{0, 0, 1, 0, 0, 1, 1},
{0, 1, 1, 1, 1, 0, 1},
{0, 1, 0, 0, 0, 1, 1},
{0, 1, 1, 0, 0, 0, 1},
{0, 1, 0, 1, 1, 1, 1},
{0, 1, 1, 1, 0, 1, 1},
{0, 1, 1, 0, 1, 1, 1},
{0, 0, 0, 1, 0, 1, 1}
},
{
{0, 1, 0, 0, 1, 1, 1},
{0, 1, 1, 0, 0, 1, 1},
{0, 0, 1, 1, 0, 1, 1},
{0, 1, 0, 0, 0, 0, 1},
{0, 0, 1, 1, 1, 0, 1},
{0, 1, 1, 1, 0, 0 ,1},
{0, 0, 0, 0, 1, 0, 1},
{0, 0, 1, 0, 0, 0 ,1},
{0, 0, 0, 1, 0, 0, 1},
{0, 0, 1, 0, 1, 1, 1}
}
};
public byte[] convert(byte EAN13[]) {
/* inccorect length or check sum, return null */
if ((EAN13 == null) ||
(EAN13.length != 13) ||
(checkSum(EAN13) == false)) {
return null;
}
byte bars[] = new byte[95];
for (int i=0; i<95; i++) {
bars[i] = 0;
}
bars[0] = 1;
bars[1] = 0;
bars[2] = 1;
for (int i=1; i<7; i++) {
for (int j=0; j<7; j++) {
bars[(i-1)*7 + 3 + j] = left[parityTable[EAN13[0]][i-1]][EAN13[i]][j];
}
}
bars[45] = 0;
bars[46] = 1;
bars[47] = 0;
bars[48] = 1;
bars[49] = 0;
for (int i=7; i<13; i++) {
for (int j=0; j<7; j++) {
bars[(i-7)*7 + 50 + j] = right[EAN13[i]][j];
}
}
bars[92] = 1;
bars[93] = 0;
bars[94] = 1;
return bars;
}
/**
* calculates checksum of EAN13
*/
private boolean checkSum(byte EAN13[]) {
int sum = 1 * EAN13[0] +
3 * EAN13[1] +
1 * EAN13[2] +
3 * EAN13[3] +
1 * EAN13[4] +
3 * EAN13[5] +
1 * EAN13[6] +
3 * EAN13[7] +
1 * EAN13[8] +
3 * EAN13[9] +
1 * EAN13[10] +
3 * EAN13[11];
sum %= 10;
sum = 10 - sum;
sum %= 10;
if (sum == EAN13[12]) {
return true;
}
else {
return false;
}
}
}

