package com.google.zxing.oned;
import com.google.zxing.BarcodeFormat;
-import com.google.zxing.ReaderException;
+import com.google.zxing.ChecksumException;
+import com.google.zxing.FormatException;
+import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.ResultPoint;
import com.google.zxing.common.BitArray;
* <p>Decodes Code 39 barcodes. This does not support "Full ASCII Code 39" yet.</p>
*
* @author Sean Owen
+ * @see Code93Reader
*/
public final class Code39Reader extends OneDReader {
- private static final String ALPHABET_STRING = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. *$/+%";
+ static final String ALPHABET_STRING = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. *$/+%";
private static final char[] ALPHABET = ALPHABET_STRING.toCharArray();
/**
* The 9 least-significant bits of each int correspond to the pattern of wide and narrow,
* with 1s representing "wide" and 0s representing narrow.
*/
- private static final int[] CHARACTER_ENCODINGS = {
+ static final int[] CHARACTER_ENCODINGS = {
0x034, 0x121, 0x061, 0x160, 0x031, 0x130, 0x070, 0x025, 0x124, 0x064, // 0-9
0x109, 0x049, 0x148, 0x019, 0x118, 0x058, 0x00D, 0x10C, 0x04C, 0x01C, // A-J
0x103, 0x043, 0x142, 0x013, 0x112, 0x052, 0x007, 0x106, 0x046, 0x016, // K-T
this.extendedMode = extendedMode;
}
- public Result decodeRow(int rowNumber, BitArray row, Hashtable hints) throws ReaderException {
+ public Result decodeRow(int rowNumber, BitArray row, Hashtable hints)
+ throws NotFoundException, ChecksumException, FormatException {
int[] start = findAsteriskPattern(row);
int nextStart = start[1];
recordPattern(row, nextStart, counters);
int pattern = toNarrowWidePattern(counters);
if (pattern < 0) {
- throw ReaderException.getInstance();
+ throw NotFoundException.getNotFoundInstance();
}
decodedChar = patternToChar(pattern);
result.append(decodedChar);
// If 50% of last pattern size, following last pattern, is not whitespace, fail
// (but if it's whitespace to the very end of the image, that's OK)
if (nextStart != end && whiteSpaceAfterEnd / 2 < lastPatternSize) {
- throw ReaderException.getInstance();
+ throw NotFoundException.getNotFoundInstance();
}
if (usingCheckDigit) {
for (int i = 0; i < max; i++) {
total += ALPHABET_STRING.indexOf(result.charAt(i));
}
- if (total % 43 != ALPHABET_STRING.indexOf(result.charAt(max))) {
- throw ReaderException.getInstance();
+ if (result.charAt(max) != ALPHABET[total % 43]) {
+ throw ChecksumException.getChecksumInstance();
}
result.deleteCharAt(max);
}
- String resultString = result.toString();
- if (extendedMode) {
- resultString = decodeExtended(resultString);
+ if (result.length() == 0) {
+ // Almost surely a false positive
+ throw NotFoundException.getNotFoundInstance();
}
- if (resultString.length() == 0) {
- // Almost surely a false positive
- throw ReaderException.getInstance();
+ String resultString;
+ if (extendedMode) {
+ resultString = decodeExtended(result);
+ } else {
+ resultString = result.toString();
}
float left = (float) (start[1] + start[0]) / 2.0f;
}
- private static int[] findAsteriskPattern(BitArray row) throws ReaderException {
+ private static int[] findAsteriskPattern(BitArray row) throws NotFoundException {
int width = row.getSize();
int rowOffset = 0;
while (rowOffset < width) {
isWhite = !isWhite;
}
}
- throw ReaderException.getInstance();
+ throw NotFoundException.getNotFoundInstance();
}
// For efficiency, returns -1 on failure. Not throwing here saved as many as 700 exceptions
return -1;
}
- private static char patternToChar(int pattern) throws ReaderException {
+ private static char patternToChar(int pattern) throws NotFoundException {
for (int i = 0; i < CHARACTER_ENCODINGS.length; i++) {
if (CHARACTER_ENCODINGS[i] == pattern) {
return ALPHABET[i];
}
}
- throw ReaderException.getInstance();
+ throw NotFoundException.getNotFoundInstance();
}
- private static String decodeExtended(String encoded) throws ReaderException {
+ private static String decodeExtended(StringBuffer encoded) throws FormatException {
int length = encoded.length();
StringBuffer decoded = new StringBuffer(length);
for (int i = 0; i < length; i++) {
if (next >= 'A' && next <= 'Z') {
decodedChar = (char) (next + 32);
} else {
- throw ReaderException.getInstance();
+ throw FormatException.getFormatInstance();
}
break;
case '$':
if (next >= 'A' && next <= 'Z') {
decodedChar = (char) (next - 64);
} else {
- throw ReaderException.getInstance();
+ throw FormatException.getFormatInstance();
}
break;
case '%':
} else if (next >= 'F' && next <= 'W') {
decodedChar = (char) (next - 11);
} else {
- throw ReaderException.getInstance();
+ throw FormatException.getFormatInstance();
}
break;
case '/':
} else if (next == 'Z') {
decodedChar = ':';
} else {
- throw ReaderException.getInstance();
+ throw FormatException.getFormatInstance();
}
break;
}