Nadesłany przez Tomasz Lubiński, 20 września 2006 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)2006 algorytm.org
*
* Code 93 Bar Code
*/
public class BarCode {
private static final char special_1 = 127 + 1; // ($)
private static final char special_2 = 127 + 2; // (%)
private static final char special_3 = 127 + 3; // (/)
private static final char special_4 = 127 + 4; // (+)
private static final char code93signs[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z', '-', '.', ' ', '$',
'/', '+', '%', special_1, special_2, special_3, special_4};
private static final char code93bars[][] = {
{1, 0, 0, 0, 1, 0, 1, 0, 0},
{1, 0, 1, 0, 0, 1, 0, 0, 0},
{1, 0, 1, 0, 0, 0, 1, 0, 0},
{1, 0, 1, 0, 0, 0, 0, 1, 0},
{1, 0, 0, 1, 0, 1, 0, 0, 0},
{1, 0, 0, 1, 0, 0, 1, 0, 0},
{1, 0, 0, 1, 0, 0, 0, 1, 0},
{1, 0, 1, 0, 1, 0, 0, 0, 0},
{1, 0, 0, 0, 1, 0, 0, 1, 0},
{1, 0, 0, 0, 0, 1, 0, 1, 0},
{1, 1, 0, 1, 0, 1, 0, 0, 0},
{1, 1, 0, 1, 0, 0, 1, 0, 0},
{1, 1, 0, 1, 0, 0, 0, 1, 0},
{1, 1, 0, 0, 1, 0, 1, 0, 0},
{1, 1, 0, 0, 1, 0, 0, 1, 0},
{1, 1, 0, 0, 0, 1, 0, 1, 0},
{1, 0, 1, 1, 0, 1, 0, 0, 0},
{1, 0, 1, 1, 0, 0, 1, 0, 0},
{1, 0, 1, 1, 0, 0, 0, 1, 0},
{1, 0, 0, 1, 1, 0, 1, 0, 0},
{1, 0, 0, 0, 1, 1, 0, 1, 0},
{1, 0, 1, 0, 1, 1, 0, 0, 0},
{1, 0, 1, 0, 0, 1, 1, 0, 0},
{1, 0, 1, 0, 0, 0, 1, 1, 0},
{1, 0, 0, 1, 0, 1, 1, 0, 0},
{1, 0, 0, 0, 1, 0, 1, 1, 0},
{1, 1, 0, 1, 1, 0, 1, 0, 0},
{1, 1, 0, 1, 1, 0, 0, 1, 0},
{1, 1, 0, 1, 0, 1, 1, 0, 0},
{1, 1, 0, 1, 0, 0, 1, 1, 0},
{1, 1, 0, 0, 1, 0, 1, 1, 0},
{1, 1, 0, 0, 1, 1, 0, 1, 0},
{1, 0, 1, 1, 0, 1, 1, 0, 0},
{1, 0, 1, 1, 0, 0, 1, 1, 0},
{1, 0, 0, 1, 1, 0, 1, 1, 0},
{1, 0, 0, 1, 1, 1, 0, 1, 0},
{1, 0, 0, 1, 0, 1, 1, 1, 0},
{1, 1, 1, 0, 1, 0, 1, 0, 0},
{1, 1, 1, 0, 1, 0, 0, 1, 0},
{1, 1, 1, 0, 0, 1, 0, 1, 0},
{1, 0, 1, 1, 0, 1, 1, 1, 0},
{1, 0, 1, 1, 1, 0, 1, 1, 0},
{1, 1, 0, 1, 0, 1, 1, 1, 0},
{1, 0, 0, 1, 0, 0, 1, 1, 0},
{1, 1, 1, 0, 1, 1, 0, 1, 0},
{1, 1, 1, 0, 1, 0, 1, 1, 0},
{1, 0, 0, 1, 1, 0, 0, 1, 0}
};
private static final String extendedCode93Replace[] = {
"\u0081U", "\u0080A", "\u0080B", "\u0080C", "\u0080D", "\u0080E", "\u0080F", "\u0080G", "\u0080H",
"\u0080I", "\u0080J", "\u0080K", "\u0080L", "\u0080M", "\u0080N", "\u0080O", "\u0080P", "\u0080Q",
"\u0080R", "\u0080S", "\u0080T", "\u0080U", "\u0080V", "\u0080W", "\u0080X", "\u0080Y", "\u0080Z",
"\u0081A", "\u0081B", "\u0081C", "\u0081D", "\u0081E", " ", "\u0082A", "\u0082B", "\u0082C", "\u0082D",
"\u0082E", "\u0082F", "\u0082G", "\u0082H", "\u0082I", "\u0082J", "\u0082K", "\u0082L", "-", ".",
"\u0082O", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "\u0082Z", "\u0081F", "\u0081G", "\u0081H",
"\u0081I", "\u0081J", "\u0081V", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
"P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "\u0081K", "\u0081L", "\u0081M", "\u0081N",
"\u0081O", "\u0081W", "\u0083A", "\u0083B", "\u0083C", "\u0083D", "\u0083E", "\u0083F", "\u0083G",
"\u0083H", "\u0083I", "\u0083J", "\u0083K", "\u0083L", "\u0083M", "\u0083N", "\u0083O", "\u0083P",
"\u0083Q", "\u0083R", "\u0083S", "\u0083T", "\u0083U", "\u0083V", "\u0083W", "\u0083X", "\u0083Y",
"\u0083Z", "\u0081P", "\u0081Q", "\u0081R", "\u0081S", "\u0081T"
};
private static final char start_stop[] = {
1, 0, 1, 0, 1, 1, 1, 1, 0
};
private static final char termination_bar[] = {
1
};
public boolean check(String txt) {
for (int i=0; i<txt.length(); i++) {
if (checkLetter(txt.charAt(i)) == false)
return false;
}
return true;
}
private boolean checkLetter(char ch) {
for (int i=0; i<code93signs.length; i++) {
if (code93signs[i] == ch)
return true;
}
return false;
}
public char[] getBars(char ch) {
for (int i=0; i<code93signs.length; i++) {
if (code93signs[i] == ch)
return code93bars[i];
}
return null;
}
public char[] getStartStopBars() {
return start_stop;
}
public char[] getTermination_bar() {
return termination_bar;
}
//returns letter value for check digit
private int getLetterValue(char ch) {
for (int i=0; i<code93signs.length; i++)
if (code93signs[i] == ch)
return i;
return 0;
}
// return check digit C
private int checkDigitC(String txt) {
int sum = 0;
int w = 0;
for (int i=txt.length()-1; i>=0; i--)
{
sum += (getLetterValue(txt.charAt(i)) * ((w % 20) + 1));
w++;
}
return (sum % 47);
}
// return check digit K
private int checkDigitK(String txt) {
int sum = checkDigitC(txt);
int w = 1;
for (int i=txt.length()-1; i>=0; i--)
{
sum += (getLetterValue(txt.charAt(i)) * ((w % 15) + 1));
w++;
}
return (sum % 47);
}
public char[] getCheckDigitBarsC(String txt) {
return code93bars[checkDigitC(txt)];
}
public char[] getCheckDigitBarsK(String txt) {
return code93bars[checkDigitK(txt)];
}
// converts to code 39 set of characters
public String convert(String txt)
{
String result = "";
for (int i=0; i<txt.length(); i++)
{
if ((txt.charAt(i) >= 0) && (txt.charAt(i) <= 127))
result += extendedCode93Replace[txt.charAt(i)];
else
result += txt.substring(i, i+1);
}
return result;
}
}

