Simple check in Code 39 reader to determine wide bars vary in width a lot, to cut...
[zxing.git] / core / src / com / google / zxing / oned / Code39Reader.java
index 12f36b1..36ff24c 100644 (file)
@@ -216,14 +216,30 @@ public final class Code39Reader extends AbstractOneDReader {
       }
       maxNarrowCounter = minCounter;
       wideCounters = 0;
+      int totalWideCountersWidth = 0;
       int pattern = 0;
       for (int i = 0; i < numCounters; i++) {
+        int counter = counters[i];
         if (counters[i] > maxNarrowCounter) {
           pattern |= 1 << (numCounters - 1 - i);
           wideCounters++;
+          totalWideCountersWidth += counter;
         }
       }
       if (wideCounters == 3) {
+        // Found 3 wide counters, but are they close enough in width?
+        // We can perform a cheap, conservative check to see if any individual
+        // counter is more than 1.5 times the average:
+        for (int i = 0; i < numCounters && wideCounters > 0; i++) {
+          int counter = counters[i];
+          if (counters[i] > maxNarrowCounter) {
+            wideCounters--;
+            // totalWideCountersWidth = 3 * average, so this checks if counter >= 3/2 * average
+            if ((counter << 1) >= totalWideCountersWidth) {
+              throw new ReaderException("Wide bars vary too much in width, rejecting");
+            }
+          }
+        }
         return pattern;
       }
     } while (wideCounters > 3);