Permalink
Browse files

Closes #738 : fix UPC-E encoding with system 1

1 parent 7daac7c commit b8c3d08e199cc3bc687ca505007785f1561224a4 @srowen srowen committed Jan 6, 2017
@@ -29,6 +29,13 @@
* @author Sean Owen
*/
public final class UPCEReader extends UPCEANReader {
+
+ /**
+ * The pattern that marks the middle, and end, of a UPC-E pattern.
+ * There is no "second half" to a UPC-E barcode.
+ */
+ private static final int[] MIDDLE_END_PATTERN = {1, 1, 1, 1, 1, 1};
+
// For an UPC-E barcode, the final digit is represented by the parities used
// to encode the middle six digits, according to the table below.
//
@@ -52,21 +59,13 @@
// in binary:
// 0 1 1 0 0 1 == 0x19
//
- static final int[] CHECK_DIGIT_ENCODINGS = {
- 0x38, 0x34, 0x32, 0x31, 0x2C, 0x26, 0x23, 0x2A, 0x29, 0x25
- };
- /**
- * The pattern that marks the middle, and end, of a UPC-E pattern.
- * There is no "second half" to a UPC-E barcode.
- */
- private static final int[] MIDDLE_END_PATTERN = {1, 1, 1, 1, 1, 1};
/**
* See {@link #L_AND_G_PATTERNS}; these values similarly represent patterns of
* even-odd parity encodings of digits that imply both the number system (0 or 1)
* used, and the check digit.
*/
- private static final int[][] NUMSYS_AND_CHECK_DIGIT_PATTERNS = {
+ static final int[][] NUMSYS_AND_CHECK_DIGIT_PATTERNS = {
{0x38, 0x34, 0x32, 0x31, 0x2C, 0x26, 0x23, 0x2A, 0x29, 0x25},
{0x07, 0x0B, 0x0D, 0x0E, 0x13, 0x19, 0x1C, 0x15, 0x16, 0x1A}
};
@@ -35,6 +35,11 @@
(7 * 6) + // bars
6; // end guard
+ public static void main(String[] args) throws Exception {
+ BitMatrix bm = new UPCEWriter().encode("12345670", BarcodeFormat.UPC_E, 200, 100, null);
+ System.out.println(bm);
+ }
+
@Override
public BitMatrix encode(String contents,
BarcodeFormat format,
@@ -82,7 +87,7 @@ public BitMatrix encode(String contents,
}
int checkDigit = Character.digit(contents.charAt(7), 10);
- int parities = UPCEReader.CHECK_DIGIT_ENCODINGS[checkDigit];
+ int parities = UPCEReader.NUMSYS_AND_CHECK_DIGIT_PATTERNS[firstDigit][checkDigit];
boolean[] result = new boolean[CODE_WIDTH];
int pos = 0;
@@ -27,16 +27,25 @@
@Test
public void testEncode() throws WriterException {
- String testStr = "0000000000010101110010100111000101101011110110111001011101010100000000000";
- BitMatrix result = new UPCEWriter().encode("05096893", BarcodeFormat.UPC_E, testStr.length(), 0);
- assertEquals(testStr, BitMatrixTestCase.matrixToString(result));
+ doTest("05096893",
+ "0000000000010101110010100111000101101011110110111001011101010100000000000");
+ }
+
+ @Test
+ public void testEncodeSystem1() throws WriterException {
+ doTest("12345670",
+ "0000000000010100100110111101010001101110010000101001000101010100000000000");
}
@Test
public void testAddChecksumAndEncode() throws WriterException {
- String testStr = "0000000000010101110010100111000101101011110110111001011101010100000000000";
- BitMatrix result = new UPCEWriter().encode("0509689", BarcodeFormat.UPC_E, testStr.length(), 0);
- assertEquals(testStr, BitMatrixTestCase.matrixToString(result));
+ doTest("0509689",
+ "0000000000010101110010100111000101101011110110111001011101010100000000000");
+ }
+
+ private static void doTest(String content, String encoding) throws WriterException {
+ BitMatrix result = new UPCEWriter().encode(content, BarcodeFormat.UPC_E, encoding.length(), 0);
+ assertEquals(encoding, BitMatrixTestCase.matrixToString(result));
}
}

0 comments on commit b8c3d08

Please sign in to comment.