Nadesłany przez Tomasz Lubiński, 08 grudnia 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
*
* EAN8 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}
};
public byte[] convert(byte EAN8[]) {
/* inccorect length or check sum, return null */
if ((EAN8 == null) ||
(EAN8.length != 8) ||
(checkSum(EAN8) == false)) {
return null;
}
byte bars[] = new byte[67];
for (int i=0; i<67; i++) {
bars[i] = 0;
}
bars[0] = 1;
bars[1] = 0;
bars[2] = 1;
for (int i=0; i<4; i++) {
for (int j=0; j<7; j++) {
bars[i*7 + 3 + j] = left[EAN8[i]][j];
}
}
bars[31] = 0;
bars[32] = 1;
bars[33] = 0;
bars[34] = 1;
bars[35] = 0;
for (int i=4; i<8; i++) {
for (int j=0; j<7; j++) {
bars[(i-4)*7 + 36 + j] = right[EAN8[i]][j];
}
}
bars[64] = 1;
bars[65] = 0;
bars[66] = 1;
return bars;
}
/**
* calculates checksum of EAN8
*/
private boolean checkSum(byte EAN8[]) {
int sum = 0;
int sum_even = 0;
int sum_uneven = 0;
int i;
for (i=1; i<7; i+=2) {
sum_uneven += EAN8[i];
}
for (i=0; i<7; i+=2) {
sum_even += EAN8[i];
}
sum = sum_uneven + 3*sum_even;
sum %= 10;
sum = 10 - sum;
sum %= 10;
if (EAN8[7] == sum) {
return true;
}
else {
return false;
}
}
}

