Some formatting changes, and a few tiny optimizations
[zxing.git] / core / src / com / google / zxing / oned / Code128Reader.java
index 969a341..75be560 100644 (file)
@@ -28,7 +28,7 @@ import java.util.Hashtable;
 /**
  * <p>Decodes Code 128 barcodes.</p>
  *
- * @author srowen@google.com (Sean Owen)
+ * @author Sean Owen
  */
 public final class Code128Reader extends AbstractOneDReader {
 
@@ -194,7 +194,10 @@ public final class Code128Reader extends AbstractOneDReader {
             }
           }
           if (bestMatch >= 0) {
-            return new int[]{patternStart, i, bestMatch};
+            // Look for whitespace before start pattern, >= 50% of width of start pattern            
+            if (row.isRange(Math.max(0, patternStart - (i - patternStart) / 2), patternStart, false)) {
+              return new int[]{patternStart, i, bestMatch};
+            }
           }
           patternStart += counters[0] + counters[1];
           for (int y = 2; y < patternLength; y++) {
@@ -210,7 +213,7 @@ public final class Code128Reader extends AbstractOneDReader {
         isWhite = !isWhite;
       }
     }
-    throw new ReaderException("Can't find pattern");
+    throw ReaderException.getInstance();
   }
 
   private static int decodeCode(BitArray row, int[] counters, int rowOffset) throws ReaderException {
@@ -229,7 +232,7 @@ public final class Code128Reader extends AbstractOneDReader {
     if (bestMatch >= 0) {
       return bestMatch;
     } else {
-      throw new ReaderException("Could not match any code pattern");
+      throw ReaderException.getInstance();
     }
   }
 
@@ -249,7 +252,7 @@ public final class Code128Reader extends AbstractOneDReader {
         codeSet = CODE_CODE_C;
         break;
       default:
-        throw new ReaderException("Illegal start code");
+        throw ReaderException.getInstance();
     }
 
     boolean done = false;
@@ -299,7 +302,7 @@ public final class Code128Reader extends AbstractOneDReader {
         case CODE_START_A:
         case CODE_START_B:
         case CODE_START_C:
-          throw new ReaderException("Unexpected start code");
+          throw ReaderException.getInstance();
       }
 
       switch (codeSet) {
@@ -413,11 +416,21 @@ public final class Code128Reader extends AbstractOneDReader {
 
     }
 
+    // Check for ample whitespice following pattern, but, to do this we first need to remember that we
+    // fudged decoding CODE_STOP since it actually has 7 bars, not 6. There is a black bar left to read off.
+    // Would be slightly better to properly read. Here we just skip it:
+    while (row.get(nextStart)) {
+      nextStart++;
+    }
+    if (!row.isRange(nextStart, Math.min(row.getSize(), nextStart + (nextStart - lastStart) / 2), false)) {
+      throw ReaderException.getInstance();
+    }
+
     // Pull out from sum the value of the penultimate check code
     checksumTotal -= multiplier * lastCode;
     // lastCode is the checksum then:
     if (checksumTotal % 103 != lastCode) {
-      throw new ReaderException("Checksum failed");
+      throw ReaderException.getInstance();
     }
 
     // Need to pull out the check digits from string
@@ -436,7 +449,7 @@ public final class Code128Reader extends AbstractOneDReader {
 
     if (resultString.length() == 0) {
       // Almost surely a false positive
-      throw new ReaderException("Empty barcode found; assuming a false positive");
+      throw ReaderException.getInstance();
     }
 
     float left = (float) (startPatternInfo[1] + startPatternInfo[0]) / 2.0f;