Add ALLOWED_LENGTHS hint, for now, in support of ITF
authorsrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Fri, 30 Jan 2009 17:29:27 +0000 (17:29 +0000)
committersrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Fri, 30 Jan 2009 17:29:27 +0000 (17:29 +0000)
git-svn-id: http://zxing.googlecode.com/svn/trunk@834 59b500cc-1b3d-0410-9834-0bbf25fbcc57

core/src/com/google/zxing/DecodeHintType.java
core/src/com/google/zxing/oned/ITFReader.java

index ec0e823..4e84634 100644 (file)
@@ -52,6 +52,11 @@ public final class DecodeHintType {
    */
   public static final DecodeHintType TRY_HARDER = new DecodeHintType();
 
+  /**
+   * Allowed lengths of encoded data -- reject anything else. Maps to an {@link int[]}.
+   */
+  public static final DecodeHintType ALLOWED_LENGTHS = new DecodeHintType();
+
   private DecodeHintType() {
   }
 
index 8895f60..49f065d 100644 (file)
@@ -20,6 +20,7 @@ import com.google.zxing.BarcodeFormat;
 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
@@ -45,6 +46,8 @@ public final class ITFReader extends AbstractOneDReader {
   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
@@ -85,10 +88,23 @@ public final class ITFReader extends AbstractOneDReader {
 \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