Another round of optimization, focused on reusing arrays and small objects.
[zxing.git] / core / src / com / google / zxing / oned / Code39Reader.java
index 96b551f..4273c4f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2008 Google Inc.
+ * Copyright 2008 ZXing authors
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package com.google.zxing.oned;
 
+import com.google.zxing.BarcodeFormat;
 import com.google.zxing.ReaderException;
 import com.google.zxing.Result;
 import com.google.zxing.ResultPoint;
 import com.google.zxing.common.BitArray;
 import com.google.zxing.common.GenericResultPoint;
 
+import java.util.Hashtable;
+
 /**
- * <p>Decodes Code 39 barcodes. This does not supported "Full ASCII Code 39" yet.</p>
+ * <p>Decodes Code 39 barcodes. This does not support "Full ASCII Code 39" yet.</p>
  *
  * @author srowen@google.com (Sean Owen)
  */
@@ -32,10 +35,15 @@ public final class Code39Reader extends AbstractOneDReader {
   private static final String ALPHABET_STRING = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. *$/+%";
   private static final char[] ALPHABET = ALPHABET_STRING.toCharArray();
 
+  /**
+   * These represent the encodings of characters, as patterns of wide and narrow bars.
+   * 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 = {
-      0x038, 0x121, 0x061, 0x160, 0x031, 0x130, 0x070, 0x025, 0x124, 0x064, // 0-9
-      0x109, 0x049, 0x148, 0x019, 0x118, 0x058, 0x00D, 0x10C, 0x08C, 0x01C, // A-J
-      0x103, 0x043, 0x142, 0x013, 0x112, 0x052, 0x007, 0x106, 0x046, 0x016,  // K-T
+      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
       0x181, 0x0C1, 0x1C0, 0x091, 0x190, 0x0D0, 0x085, 0x184, 0x0C4, 0x094, // U-*
       0x0A8, 0x0A2, 0x08A, 0x02A // $-%
   };
@@ -43,31 +51,48 @@ public final class Code39Reader extends AbstractOneDReader {
   private static final int ASTERISK_ENCODING = CHARACTER_ENCODINGS[39];
 
   private final boolean usingCheckDigit;
+  private final boolean extendedMode;
 
   /**
    * Creates a reader that assumes all encoded data is data, and does not treat the final
-   * character as a check digit.
+   * character as a check digit. It will not decoded "extended Code 39" sequences.
    */
   public Code39Reader() {
     usingCheckDigit = false;
+    extendedMode = false;
   }
 
   /**
    * Creates a reader that can be configured to check the last character as a check digit.
+   * It will not decoded "extended Code 39" sequences.
    *
    * @param usingCheckDigit if true, treat the last data character as a check digit, not
-   *                        data, and verify that the checksum passes
+   * data, and verify that the checksum passes.
    */
   public Code39Reader(boolean usingCheckDigit) {
     this.usingCheckDigit = usingCheckDigit;
+    this.extendedMode = false;
   }
 
-  public Result decodeRow(final int rowNumber, final BitArray row) throws ReaderException {
+  /**
+   * Creates a reader that can be configured to check the last character as a check digit,
+   * or optionally attempt to decode "extended Code 39" sequences that are used to encode
+   * the full ASCII character set.
+   *
+   * @param usingCheckDigit if true, treat the last data character as a check digit, not
+   * data, and verify that the checksum passes.
+   * @param extendedMode if true, will attempt to decode extended Code 39 sequences in the
+   * text.
+   */
+  public Code39Reader(boolean usingCheckDigit, boolean extendedMode) {
+    this.usingCheckDigit = usingCheckDigit;
+    this.extendedMode = extendedMode;
+  }
 
-    int[] start = findAsteriskPattern(row);
+  public Result decodeRow(int rowNumber, BitArray row, Hashtable hints) throws ReaderException {
 
+    int[] start = findAsteriskPattern(row);
     int nextStart = start[1];
-
     int end = row.getSize();
 
     // Read off white space
@@ -95,6 +120,18 @@ public final class Code39Reader extends AbstractOneDReader {
     } while (decodedChar != '*');
     result.deleteCharAt(result.length() - 1); // remove asterisk
 
+    // Look for whitespace after pattern:
+    int lastPatternSize = 0;
+    for (int i = 0; i < counters.length; i++) {
+      lastPatternSize += counters[i];
+    }
+    int whiteSpaceAfterEnd = nextStart - lastStart - lastPatternSize;
+    // 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 new ReaderException("Pattern not followed by whitespace");
+    }
+
     if (usingCheckDigit) {
       int max = result.length() - 1;
       int total = 0;
@@ -108,9 +145,24 @@ public final class Code39Reader extends AbstractOneDReader {
     }
 
     String resultString = result.toString();
-    return new Result(resultString,
-        new ResultPoint[]{new GenericResultPoint((float) (start[1] - start[0]) / 2.0f, (float) rowNumber),
-            new GenericResultPoint((float) (nextStart - lastStart) / 2.0f, (float) rowNumber)});
+    if (extendedMode) {
+      resultString = decodeExtended(resultString);
+    }
+
+    if (resultString.length() == 0) {
+      // Almost surely a false positive
+      throw new ReaderException("Empty barcode found; assuming a false positive");
+    }
+
+    float left = (float) (start[1] + start[0]) / 2.0f;
+    float right = (float) (nextStart + lastStart) / 2.0f;
+    return new Result(
+        resultString,
+        null,
+        new ResultPoint[]{
+            new GenericResultPoint(left, (float) rowNumber),
+            new GenericResultPoint(right, (float) rowNumber)},
+        BarcodeFormat.CODE_39);
 
   }
 
@@ -138,7 +190,10 @@ public final class Code39Reader extends AbstractOneDReader {
         if (counterPosition == patternLength - 1) {
           try {
             if (toNarrowWidePattern(counters) == ASTERISK_ENCODING) {
-              return new int[] {patternStart, i};
+              // 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};
+              }
             }
           } catch (ReaderException re) {
             // no match, continue
@@ -147,6 +202,8 @@ public final class Code39Reader extends AbstractOneDReader {
           for (int y = 2; y < patternLength; y++) {
             counters[y - 2] = counters[y];
           }
+          counters[patternLength - 2] = 0;
+          counters[patternLength - 1] = 0;
           counterPosition--;
         } else {
           counterPosition++;
@@ -159,25 +216,47 @@ public final class Code39Reader extends AbstractOneDReader {
   }
 
   private static int toNarrowWidePattern(int[] counters) throws ReaderException {
-    int minCounter = Integer.MAX_VALUE;
-    for (int i = 0; i < counters.length; i++) {
-      if (counters[i] < minCounter) {
-        minCounter = counters[i];
+    int numCounters = counters.length;
+    int maxNarrowCounter = 0;
+    int wideCounters;
+    do {
+      int minCounter = Integer.MAX_VALUE;
+      for (int i = 0; i < numCounters; i++) {
+        int counter = counters[i];
+        if (counter < minCounter && counter > maxNarrowCounter) {
+          minCounter = counter;
+        }
       }
-    }
-    int maxNarrowCounter = (int) (minCounter * 1.5f);
-    int wideCounters = 0;
-    int pattern = 0;
-    for (int i = 0; i < counters.length; i++) {
-      if (counters[i] > maxNarrowCounter) {
-        pattern |= 1 << (counters.length - 1 - i);
-        wideCounters++;
+      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) {
-      throw new ReaderException("Can't find 3 wide bars/spaces out of 9");
-    }
-    return pattern;
+      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);
+    throw new ReaderException("Can't find 3 wide bars/spaces out of 9");
   }
 
   private static char patternToChar(int pattern) throws ReaderException {
@@ -189,4 +268,60 @@ public final class Code39Reader extends AbstractOneDReader {
     throw new ReaderException("Pattern did not match character encoding");
   }
 
-}
\ No newline at end of file
+  private static String decodeExtended(String encoded) throws ReaderException {
+    int length = encoded.length();
+    StringBuffer decoded = new StringBuffer(length);
+    for (int i = 0; i < length; i++) {
+      char c = encoded.charAt(i);
+      if (c == '+' || c == '$' || c == '%' || c == '/') {
+        char next = encoded.charAt(i + 1);
+        char decodedChar = '\0';
+        switch (c) {
+          case '+':
+            // +A to +Z map to a to z
+            if (next >= 'A' && next <= 'Z') {
+              decodedChar = (char) (next + 32);
+            } else {
+              throw new ReaderException("Invalid extended code 39 sequence: " + c + next);
+            }
+            break;
+          case '$':
+            // $A to $Z map to control codes SH to SB
+            if (next >= 'A' && next <= 'Z') {
+              decodedChar = (char) (next - 64);
+            } else {
+              throw new ReaderException("Invalid extended code 39 sequence: " + c + next);
+            }
+            break;
+          case '%':
+            // %A to %E map to control codes ESC to US
+            if (next >= 'A' && next <= 'E') {
+              decodedChar = (char) (next - 38);
+            } else if (next >= 'F' && next <= 'W') {
+              decodedChar = (char) (next - 11);
+            } else {
+              throw new ReaderException("Invalid extended code 39 sequence: " + c + next);
+            }
+            break;
+          case '/':
+            // /A to /O map to ! to , and /Z maps to :
+            if (next >= 'A' && next <= 'O') {
+              decodedChar = (char) (next - 32);
+            } else if (next == 'Z') {
+              decodedChar = ':';
+            } else {
+              throw new ReaderException("Invalid extended sequence: " + c + next);
+            }
+            break;
+        }
+        decoded.append(decodedChar);
+        // bump up i again since we read two characters
+        i++;
+      } else {
+        decoded.append(c);
+      }
+    }
+    return decoded.toString();
+  }
+
+}