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

This commit is contained in:
Sean Owen 2017-01-06 14:17:48 +00:00
parent 7daac7cb8b
commit b8c3d08e19
No known key found for this signature in database
GPG key ID: F6CE9695C9318406
3 changed files with 29 additions and 16 deletions

View file

@ -29,6 +29,13 @@ import com.google.zxing.common.BitArray;
* @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 @@ public final class UPCEReader extends UPCEANReader {
// 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}
};

View file

@ -35,6 +35,11 @@ public final class UPCEWriter extends UPCEANWriter {
(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 final class UPCEWriter extends UPCEANWriter {
}
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;

View file

@ -27,16 +27,25 @@ public final class UPCEWriterTestCase extends Assert {
@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));
}
}