public byte[] encode(String contents) {
int length = contents.length();
if (length > 80) {
throw new IllegalArgumentException(
"Requested contents should be less than 80 digits long, but got " + length);
}
int codeWidth = 11 + 11 + 13;
for (int i = 0; i < length; i++) {
int[] patterns = Code128Reader.CODE_PATTERNS[contents.charAt(i) - ' '];
for (int j = 0; j < patterns.length; j++) {
codeWidth += patterns[j];
}
}
byte[] result = new byte[codeWidth];
int pos = appendPattern(result, 0, Code128Reader.CODE_PATTERNS[104], 1);
int check = 104;
for(int i = 0; i < length; i++) {
check += (contents.charAt(i) - ' ') * (i + 1);
pos += appendPattern(result, pos, Code128Reader.CODE_PATTERNS[contents.charAt(i) - ' '],1);
}
check %= 103;
pos += appendPattern(result,pos,Code128Reader.CODE_PATTERNS[check],1);
pos += appendPattern(result,pos,Code128Reader.CODE_PATTERNS[106],1);
return result;
}