import com.google.zxing.ReaderException;\r
import com.google.zxing.Result;\r
import com.google.zxing.ResultPoint;\r
+import com.google.zxing.DecodeHintType;\r
import com.google.zxing.common.BitArray;\r
import com.google.zxing.common.GenericResultPoint;\r
\r
private static final int W = 3; // Pixel width of a wide line\r
private static final int N = 1; // Pixed width of a narrow line\r
\r
+ private static final int[] DEFAULT_ALLOWED_LENGTHS = { 6, 10, 14 };\r
+\r
// Stores the actual narrow line width of the image being decoded.\r
private int narrowLineWidth = -1;\r
\r
/**\r
* Patterns of Wide / Narrow lines to indicate each digit\r
*/\r
- static final int[][] PATTERNS = {\r
+ private static final int[][] PATTERNS = {\r
{N, N, W, W, N}, // 0\r
{W, N, N, N, W}, // 1\r
{N, W, N, N, W}, // 2\r
{N, W, N, W, N} // 9\r
};\r
\r
- public final Result decodeRow(int rowNumber, BitArray row, Hashtable hints) throws ReaderException {\r
+ public Result decodeRow(int rowNumber, BitArray row, Hashtable hints) throws ReaderException {\r
\r
StringBuffer result = new StringBuffer(20);\r
\r
\r
String resultString = result.toString();\r
\r
+ int[] allowedLengths = (int[]) hints.get(DecodeHintType.ALLOWED_LENGTHS);\r
+ if (allowedLengths == null) {\r
+ allowedLengths = DEFAULT_ALLOWED_LENGTHS;\r
+ }\r
+\r
// To avoid false positives with 2D barcodes (and other patterns), make\r
// an assumption that the decoded string must be 6, 10 or 14 digits.\r
int length = resultString.length();\r
- if (length != 6 && length != 10 && length != 14) {\r
+ boolean lengthOK = false;\r
+ for (int i = 0; i < allowedLengths.length; i++) {\r
+ if (length == allowedLengths[i]) {\r
+ lengthOK = true;\r
+ break;\r
+ }\r
+\r
+ }\r
+ if (!lengthOK) {\r
throw ReaderException.getInstance();\r
}\r
\r
* @param resultString {@link StringBuffer} to append decoded chars to\r
* @throws ReaderException if decoding could not complete successfully\r
*/\r
- protected void decodeMiddle(BitArray row, int payloadStart, int payloadEnd, StringBuffer resultString) throws ReaderException {\r
+ static void decodeMiddle(BitArray row, int payloadStart, int payloadEnd, StringBuffer resultString) throws ReaderException {\r
\r
// Digits are interleaved in pairs - 5 black lines for one digit, and the\r
// 5\r
}\r
\r
int bestMatch = decodeDigit(counterBlack);\r
- resultString.append((char) ('0' + bestMatch % 10));\r
+ resultString.append((char) ('0' + bestMatch));\r
bestMatch = decodeDigit(counterWhite);\r
- resultString.append((char) ('0' + bestMatch % 10));\r
+ resultString.append((char) ('0' + bestMatch));\r
\r
for (int i = 0; i < counterDigitPair.length; i++) {\r
payloadStart += counterDigitPair[i];\r
*/\r
int[] decodeStart(BitArray row) throws ReaderException {\r
int endStart = skipWhiteSpace(row);\r
- int startPattern[] = findGuardPattern(row, endStart, START_PATTERN);\r
+ int[] startPattern = findGuardPattern(row, endStart, START_PATTERN);\r
\r
// Determine the width of a narrow line in pixels. We can do this by\r
// getting the width of the start pattern and dividing by 4 because its\r
* @return index of the first black line.\r
* @throws ReaderException Throws exception if no black lines are found in the row\r
*/\r
- private int skipWhiteSpace(BitArray row) throws ReaderException {\r
+ private static int skipWhiteSpace(BitArray row) throws ReaderException {\r
int width = row.getSize();\r
int endStart = 0;\r
while (endStart < width) {\r
row.reverse();\r
\r
int endStart = skipWhiteSpace(row);\r
- int endPattern[];\r
+ int[] endPattern;\r
try {\r
endPattern = findGuardPattern(row, endStart, END_PATTERN_REVERSED);\r
} catch (ReaderException e) {\r
* ints\r
* @throws ReaderException if pattern is not found\r
*/\r
- int[] findGuardPattern(BitArray row, int rowOffset, int[] pattern) throws ReaderException {\r
+ static int[] findGuardPattern(BitArray row, int rowOffset, int[] pattern) throws ReaderException {\r
\r
// TODO: This is very similar to implementation in AbstractUPCEANReader. Consider if they can be merged to\r
// a single method.\r
int patternStart = rowOffset;\r
for (int x = rowOffset; x < width; x++) {\r
boolean pixel = row.get(x);\r
- if ((!pixel && isWhite) || (pixel && !isWhite)) {\r
+ if (pixel ^ isWhite) {\r
counters[counterPosition]++;\r
} else {\r
if (counterPosition == patternLength - 1) {\r
counterPosition++;\r
}\r
counters[counterPosition] = 1;\r
- isWhite = !isWhite;\r
+ isWhite ^= true; // isWhite = !isWhite;\r
}\r
}\r
throw ReaderException.getInstance();\r
* @return The decoded digit\r
* @throws ReaderException if digit cannot be decoded\r
*/\r
- static int decodeDigit(int[] counters) throws ReaderException {\r
+ private static int decodeDigit(int[] counters) throws ReaderException {\r
\r
int bestVariance = MAX_AVG_VARIANCE; // worst variance we'll accept\r
int bestMatch = -1;\r