Issue 1823: fix handling of initial alt Codabar char, and improve tests

git-svn-id: https://zxing.googlecode.com/svn/trunk@2996 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2013-12-19 14:35:03 +00:00
parent fe555e001b
commit 17f3ab9c7e
2 changed files with 36 additions and 15 deletions

View file

@ -27,6 +27,7 @@ public final class CodaBarWriter extends OneDimensionalCodeWriter {
private static final char[] START_END_CHARS = {'A', 'B', 'C', 'D'};
private static final char[] ALT_START_END_CHARS = {'T', 'N', '*', 'E'};
private static final char[] CHARS_WHICH_ARE_TEN_LENGTH_EACH_AFTER_DECODED = {'/', ':', '+', '.'};
@Override
public boolean[] encode(String contents) {
@ -51,13 +52,10 @@ public final class CodaBarWriter extends OneDimensionalCodeWriter {
// The start character and the end character are decoded to 10 length each.
int resultLength = 20;
char[] charsWhichAreTenLengthEachAfterDecoded = {'/', ':', '+', '.'};
for (int i = 1; i < contents.length() - 1; i++) {
if (Character.isDigit(contents.charAt(i)) || contents.charAt(i) == '-'
|| contents.charAt(i) == '$') {
if (Character.isDigit(contents.charAt(i)) || contents.charAt(i) == '-' || contents.charAt(i) == '$') {
resultLength += 9;
} else if (CodaBarReader.arrayContains(
charsWhichAreTenLengthEachAfterDecoded, contents.charAt(i))) {
} else if (CodaBarReader.arrayContains(CHARS_WHICH_ARE_TEN_LENGTH_EACH_AFTER_DECODED, contents.charAt(i))) {
resultLength += 10;
} else {
throw new IllegalArgumentException("Cannot encode : '" + contents.charAt(i) + '\'');
@ -70,8 +68,8 @@ public final class CodaBarWriter extends OneDimensionalCodeWriter {
int position = 0;
for (int index = 0; index < contents.length(); index++) {
char c = Character.toUpperCase(contents.charAt(index));
if (index == contents.length() - 1) {
// The end chars are not in the CodaBarReader.ALPHABET.
if (index == 0 || index == contents.length() - 1) {
// The start/end chars are not in the CodaBarReader.ALPHABET.
switch (c) {
case 'T':
c = 'A';

View file

@ -24,20 +24,43 @@ import org.junit.Test;
/**
* @author dsbnatut@gmail.com (Kazuki Nishiura)
* @author Sean Owen
*/
public final class CodaBarWriterTestCase extends Assert {
@Test
public void testEncode() throws WriterException {
// 1001001011 0 110101001 0 101011001 0 110101001 0 101001101 0 110010101 0 1101101011 0
// 1001001011
CharSequence resultStr = "0000000000" +
"1001001011011010100101010110010110101001010100110101100101010110110101101001001011"
+ "0000000000";
BitMatrix result = new CodaBarWriter().encode("B515-3/B", BarcodeFormat.CODABAR, resultStr.length(), 0);
for (int i = 0; i < resultStr.length(); i++) {
assertEquals("Element " + i, resultStr.charAt(i) == '1', result.get(i, 0));
doTest("B515-3/B",
"00000" +
"1001001011" + "0110101001" + "0101011001" + "0110101001" + "0101001101" +
"0110010101" + "01101101011" + "01001001011" +
"00000");
}
@Test
public void testEncode2() throws WriterException {
doTest("T123T",
"00000" +
"1011001001" + "0101011001" + "0101001011" + "0110010101" + "01011001001" +
"00000");
}
@Test
public void testAltStartEnd() throws WriterException {
assertEquals(encode("T123456789-$T"), encode("A123456789-$A"));
}
private static void doTest(String input, CharSequence expected) throws WriterException {
BitMatrix result = encode(input);
StringBuilder actual = new StringBuilder(result.getWidth());
for (int i = 0; i < result.getWidth(); i++) {
actual.append(result.get(i, 0) ? '1' : '0');
}
assertEquals(expected, actual.toString());
}
private static BitMatrix encode(String input) throws WriterException {
return new CodaBarWriter().encode(input, BarcodeFormat.CODABAR, 0, 0);
}
}