Separated rectangle detection from decoder for reuse
authorsrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Wed, 14 Jan 2009 10:44:46 +0000 (10:44 +0000)
committersrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Wed, 14 Jan 2009 10:44:46 +0000 (10:44 +0000)
git-svn-id: http://zxing.googlecode.com/svn/trunk@824 59b500cc-1b3d-0410-9834-0bbf25fbcc57

core/src/com/google/zxing/common/detector/MonochromeRectangleDetector.java [new file with mode: 0644]
core/src/com/google/zxing/datamatrix/detector/Detector.java
javase/src/com/google/zxing/client/j2se/CommandLineRunner.java

diff --git a/core/src/com/google/zxing/common/detector/MonochromeRectangleDetector.java b/core/src/com/google/zxing/common/detector/MonochromeRectangleDetector.java
new file mode 100644 (file)
index 0000000..f3c70d3
--- /dev/null
@@ -0,0 +1,212 @@
+/*
+ * Copyright 2009 ZXing authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.zxing.common.detector;
+
+import com.google.zxing.MonochromeBitmapSource;
+import com.google.zxing.ReaderException;
+import com.google.zxing.ResultPoint;
+import com.google.zxing.BlackPointEstimationMethod;
+import com.google.zxing.common.BitArray;
+import com.google.zxing.common.GenericResultPoint;
+
+/**
+ * <p>A somewhat generic detector that looks for a barcode-like rectangular region within an image.
+ * It looks within a mostly white region of an image for a region of black and white, but mostly black.
+ * It returns the four corners of the region, as best it can determine.</p>
+ *
+ * @author Sean Owen
+ */
+public final class MonochromeRectangleDetector {
+
+  private static final int MAX_MODULES = 32;
+
+  private final MonochromeBitmapSource image;
+
+  public MonochromeRectangleDetector(MonochromeBitmapSource image) {
+    this.image = image;
+  }
+
+  /**
+   * <p>Detects a rectangular region of black and white -- mostly black -- with a region of mostly
+   * white, in an image.</p>
+   *
+   * @return {@link ResultPoint[]} describing the corners of the rectangular region. The first and last points
+   *  are opposed on the diagonal, as are the second and third. The first point will be the topmost point and
+   *  the last, the bottommost. The second point will be leftmost and the third, the rightmost
+   * @throws ReaderException if no Data Matrix Code can be found
+   */
+  public ResultPoint[] detect() throws ReaderException {
+
+    if (!BlackPointEstimationMethod.TWO_D_SAMPLING.equals(image.getLastEstimationMethod())) {
+      image.estimateBlackPoint(BlackPointEstimationMethod.TWO_D_SAMPLING, 0);
+    }
+
+    int height = image.getHeight();
+    int width = image.getWidth();
+    int halfHeight = height >> 1;
+    int halfWidth = width >> 1;
+    int iSkip = Math.max(1, height / (MAX_MODULES << 3));
+    int jSkip = Math.max(1, width / (MAX_MODULES << 3));
+
+    int minI = 0;
+    int maxI = height;
+    int minJ = 0;
+    int maxJ = width;
+    ResultPoint pointA = findCornerFromCenter(halfHeight, -iSkip, minI, maxI, halfWidth,      0, minJ, maxJ, halfWidth >> 1);
+    minI = (int) pointA.getY() - 1;
+    ResultPoint pointB = findCornerFromCenter(halfHeight, 0,      minI, maxI, halfWidth, -jSkip, minJ, maxJ, halfHeight >> 1);
+    minJ = (int) pointB.getX() - 1;
+    ResultPoint pointC = findCornerFromCenter(halfHeight, 0,      minI, maxI, halfWidth,  jSkip, minJ, maxJ, halfHeight >> 1);
+    maxJ = (int) pointC.getX() + 1;
+    ResultPoint pointD = findCornerFromCenter(halfHeight,  iSkip, minI, maxI, halfWidth,      0, minJ, maxJ, halfWidth >> 1);
+    maxI = (int) pointD.getY() + 1;
+    // Go try to find point A again with better information -- might have been off at first.
+    pointA = findCornerFromCenter(halfHeight, -iSkip, minI, maxI, halfWidth,      0, minJ, maxJ, halfWidth >> 2);
+
+    return new ResultPoint[] { pointA, pointB, pointC, pointD };
+  }
+
+  /**
+   * Attempts to locate a corner of the barcode by scanning up, down, left or right from a center
+   * point which should be within the barcode.
+   *
+   * @param centerI center's i componennt (vertical)
+   * @param di change in i per step. If scanning up this is negative; down, positive; left or right, 0
+   * @param minI minimum value of i to search through (meaningless when di == 0)
+   * @param maxI maximum value of i
+   * @param centerJ center's j component (horizontal)
+   * @param dj same as di but change in j per step instead
+   * @param minJ see minI
+   * @param maxJ see minJ
+   * @param maxWhiteRun maximum run of white pixels that can still be considered to be within
+   *  the barcode
+   * @return a {@link com.google.zxing.ResultPoint} encapsulating the corner that was found
+   * @throws com.google.zxing.ReaderException if such a point cannot be found
+   */
+  private ResultPoint findCornerFromCenter(int centerI, int di, int minI, int maxI,
+                                           int centerJ, int dj, int minJ, int maxJ,
+                                           int maxWhiteRun) throws ReaderException {
+    int[] lastRange = null;
+    for (int i = centerI, j = centerJ;
+         i < maxI && i >= minI && j < maxJ && j >= minJ;
+         i += di, j += dj) {
+      int[] range;
+      if (dj == 0) {
+        // horizontal slices, up and down
+        range = blackWhiteRange(i, maxWhiteRun, minJ, maxJ, true);
+      } else {
+        // vertical slices, left and right
+        range = blackWhiteRange(j, maxWhiteRun, minI, maxI, false);
+      }
+      if (range == null) {
+        if (lastRange == null) {
+          throw ReaderException.getInstance();
+        }
+        // lastRange was found
+        if (dj == 0) {
+          int lastI = i - di;
+          if (lastRange[0] < centerJ) {
+            if (lastRange[1] > centerJ) {
+              // straddle, choose one or the other based on direction
+              return new GenericResultPoint(di > 0 ? lastRange[0] : lastRange[1], lastI);
+            }
+            return new GenericResultPoint(lastRange[0], lastI);
+          } else {
+            return new GenericResultPoint(lastRange[1], lastI);
+          }
+        } else {
+          int lastJ = j - dj;
+          if (lastRange[0] < centerI) {
+            if (lastRange[1] > centerI) {
+              return new GenericResultPoint(lastJ, dj < 0 ? lastRange[0] : lastRange[1]);
+            }
+            return new GenericResultPoint(lastJ, lastRange[0]);
+          } else {
+            return new GenericResultPoint(lastJ, lastRange[1]);
+          }
+        }
+      }
+      lastRange = range;
+    }
+    throw ReaderException.getInstance();
+  }
+
+  /**
+   * Computes the start and end of a region of pixels, either horizontally or vertically, that could be
+   * part of a Data Matrix barcode.
+   *
+   * @param fixedDimension if scanning horizontally, this is the row (the fixed vertical location) where
+   *  we are scanning. If scanning vertically it's the colummn, the fixed horizontal location
+   * @param maxWhiteRun largest run of white pixels that can still be considered part of the barcode region
+   * @param minDim minimum pixel location, horizontally or vertically, to consider
+   * @param maxDim maximum pixel location, horizontally or vertically, to consider
+   * @param horizontal if true, we're scanning left-right, instead of up-down
+   * @return int[] with start and end of found range, or null if no such range is found (e.g. only white was found)
+   */
+  private int[] blackWhiteRange(int fixedDimension, int maxWhiteRun, int minDim, int maxDim, boolean horizontal) {
+
+    int center = (minDim + maxDim) / 2;
+
+    BitArray rowOrColumn = horizontal ? image.getBlackRow(fixedDimension, null, 0, image.getWidth())
+                                      : image.getBlackColumn(fixedDimension, null, 0, image.getHeight());
+
+    // Scan left/up first
+    int start = center;
+    while (start >= minDim) {
+      if (rowOrColumn.get(start)) {
+        start--;
+      } else {
+        int whiteRunStart = start;
+        do {
+          start--;
+        } while (start >= minDim && !rowOrColumn.get(start));
+        int whiteRunSize = whiteRunStart - start;
+        if (start < minDim || whiteRunSize > maxWhiteRun) {
+          start = whiteRunStart + 1; // back up
+          break;
+        }
+      }
+    }
+    start++;
+
+    // Then try right/down
+    int end = center;
+    while (end < maxDim) {
+      if (rowOrColumn.get(end)) {
+        end++;
+      } else {
+        int whiteRunStart = end;
+        do {
+          end++;
+        } while (end < maxDim && !rowOrColumn.get(end));
+        int whiteRunSize = end - whiteRunStart;
+        if (end >= maxDim || whiteRunSize > maxWhiteRun) {
+          end = whiteRunStart - 1;
+          break;
+        }
+      }
+    }
+    end--;
+
+    if (end > start) {
+      return new int[] { start, end };
+    } else {
+      return null;
+    }
+  }
+
+}
\ No newline at end of file
index 644fb79..4e47692 100644 (file)
@@ -19,14 +19,13 @@ package com.google.zxing.datamatrix.detector;
 import com.google.zxing.MonochromeBitmapSource;
 import com.google.zxing.ReaderException;
 import com.google.zxing.ResultPoint;
-import com.google.zxing.BlackPointEstimationMethod;
-import com.google.zxing.common.BitArray;
 import com.google.zxing.common.BitMatrix;
 import com.google.zxing.common.Collections;
 import com.google.zxing.common.Comparator;
 import com.google.zxing.common.DetectorResult;
 import com.google.zxing.common.GenericResultPoint;
 import com.google.zxing.common.GridSampler;
+import com.google.zxing.common.detector.MonochromeRectangleDetector;
 
 import java.util.Enumeration;
 import java.util.Hashtable;
@@ -48,9 +47,11 @@ public final class Detector {
       { new Integer(0), new Integer(1), new Integer(2), new Integer(3), new Integer(4) };
 
   private final MonochromeBitmapSource image;
+  private final MonochromeRectangleDetector rectangleDetector;
 
   public Detector(MonochromeBitmapSource image) {
     this.image = image;
+    rectangleDetector = new MonochromeRectangleDetector(image);
   }
 
   /**
@@ -61,31 +62,11 @@ public final class Detector {
    */
   public DetectorResult detect() throws ReaderException {
 
-    if (!BlackPointEstimationMethod.TWO_D_SAMPLING.equals(image.getLastEstimationMethod())) {
-      image.estimateBlackPoint(BlackPointEstimationMethod.TWO_D_SAMPLING, 0);
-    }
-
-    int height = image.getHeight();
-    int width = image.getWidth();
-    int halfHeight = height >> 1;
-    int halfWidth = width >> 1;
-    int iSkip = Math.max(1, height / (MAX_MODULES << 3));
-    int jSkip = Math.max(1, width / (MAX_MODULES << 3));
-
-    int minI = 0;
-    int maxI = height;
-    int minJ = 0;
-    int maxJ = width;
-    ResultPoint pointA = findCornerFromCenter(halfHeight, -iSkip, minI, maxI, halfWidth,      0, minJ, maxJ, halfWidth >> 1);
-    minI = (int) pointA.getY() - 1;
-    ResultPoint pointB = findCornerFromCenter(halfHeight, 0,      minI, maxI, halfWidth, -jSkip, minJ, maxJ, halfHeight >> 1);
-    minJ = (int) pointB.getX() - 1;
-    ResultPoint pointC = findCornerFromCenter(halfHeight, 0,      minI, maxI, halfWidth,  jSkip, minJ, maxJ, halfHeight >> 1);
-    maxJ = (int) pointC.getX() + 1;
-    ResultPoint pointD = findCornerFromCenter(halfHeight,  iSkip, minI, maxI, halfWidth,      0, minJ, maxJ, halfWidth >> 1);
-    maxI = (int) pointD.getY() + 1;
-    // Go try to find point A again with better information -- might have been off at first.
-    pointA = findCornerFromCenter(halfHeight, -iSkip, minI, maxI, halfWidth,      0, minJ, maxJ, halfWidth >> 2);
+    ResultPoint[] cornerPoints = rectangleDetector.detect();
+    ResultPoint pointA = cornerPoints[0];
+    ResultPoint pointB = cornerPoints[1];
+    ResultPoint pointC = cornerPoints[2];
+    ResultPoint pointD = cornerPoints[3];
 
     // Point A and D are across the diagonal from one another,
     // as are B and C. Figure out which are the solid black lines
@@ -178,71 +159,6 @@ public final class Detector {
     return new DetectorResult(bits, new ResultPoint[] {pointA, pointB, pointC, pointD});
   }
 
-  /**
-   * Attempts to locate a corner of the barcode by scanning up, down, left or right from a center
-   * point which should be within the barcode.
-   *
-   * @param centerI center's i componennt (vertical)
-   * @param di change in i per step. If scanning up this is negative; down, positive; left or right, 0
-   * @param minI minimum value of i to search through (meaningless when di == 0)
-   * @param maxI maximum value of i
-   * @param centerJ center's j component (horizontal)
-   * @param dj same as di but change in j per step instead
-   * @param minJ see minI
-   * @param maxJ see minJ
-   * @param maxWhiteRun maximum run of white pixels that can still be considered to be within
-   *  the barcode
-   * @return a {@link ResultPoint} encapsulating the corner that was found
-   * @throws ReaderException if such a point cannot be found
-   */
-  private ResultPoint findCornerFromCenter(int centerI, int di, int minI, int maxI,
-                                           int centerJ, int dj, int minJ, int maxJ,
-                                           int maxWhiteRun) throws ReaderException {
-    int[] lastRange = null;
-    for (int i = centerI, j = centerJ;
-         i < maxI && i >= minI && j < maxJ && j >= minJ;
-         i += di, j += dj) {
-      int[] range;
-      if (dj == 0) {
-        // horizontal slices, up and down
-        range = blackWhiteRange(i, maxWhiteRun, minJ, maxJ, true);
-      } else {
-        // vertical slices, left and right
-        range = blackWhiteRange(j, maxWhiteRun, minI, maxI, false);
-      }
-      if (range == null) {
-        if (lastRange == null) {
-          throw ReaderException.getInstance();
-        }
-        // lastRange was found
-        if (dj == 0) {
-          int lastI = i - di;
-          if (lastRange[0] < centerJ) {
-            if (lastRange[1] > centerJ) {
-              // straddle, choose one or the other based on direction
-              return new GenericResultPoint(di > 0 ? lastRange[0] : lastRange[1], lastI);
-            }
-            return new GenericResultPoint(lastRange[0], lastI);
-          } else {
-            return new GenericResultPoint(lastRange[1], lastI);
-          }
-        } else {
-          int lastJ = j - dj;
-          if (lastRange[0] < centerI) {
-            if (lastRange[1] > centerI) {
-              return new GenericResultPoint(lastJ, dj < 0 ? lastRange[0] : lastRange[1]);
-            }
-            return new GenericResultPoint(lastJ, lastRange[0]);
-          } else {
-            return new GenericResultPoint(lastJ, lastRange[1]);
-          }
-        }
-      }
-      lastRange = range;
-    }
-    throw ReaderException.getInstance();
-  }
-
   /**
    * Increments the Integer associated with a key by one.
    */
@@ -251,70 +167,6 @@ public final class Detector {
     table.put(key, value == null ? INTEGERS[1] : INTEGERS[value.intValue() + 1]);
   }
 
-  /**
-   * Computes the start and end of a region of pixels, either horizontally or vertically, that could be
-   * part of a Data Matrix barcode.
-   *
-   * @param fixedDimension if scanning horizontally, this is the row (the fixed vertical location) where
-   *  we are scanning. If scanning vertically it's the colummn, the fixed horizontal location
-   * @param maxWhiteRun largest run of white pixels that can still be considered part of the barcode region
-   * @param minDim minimum pixel location, horizontally or vertically, to consider
-   * @param maxDim maximum pixel location, horizontally or vertically, to consider
-   * @param horizontal if true, we're scanning left-right, instead of up-down
-   * @return int[] with start and end of found range, or null if no such range is found (e.g. only white was found)
-   */
-  private int[] blackWhiteRange(int fixedDimension, int maxWhiteRun, int minDim, int maxDim, boolean horizontal) {
-
-    int center = (minDim + maxDim) / 2;
-
-    BitArray rowOrColumn = horizontal ? image.getBlackRow(fixedDimension, null, 0, image.getWidth())
-                                      : image.getBlackColumn(fixedDimension, null, 0, image.getHeight());
-
-    // Scan left/up first
-    int start = center;
-    while (start >= minDim) {
-      if (rowOrColumn.get(start)) {
-        start--;
-      } else {
-        int whiteRunStart = start;
-        do {
-          start--;
-        } while (start >= minDim && !rowOrColumn.get(start));
-        int whiteRunSize = whiteRunStart - start;
-        if (start < minDim || whiteRunSize > maxWhiteRun) {
-          start = whiteRunStart + 1; // back up
-          break;
-        }
-      }
-    }
-    start++;
-
-    // Then try right/down
-    int end = center;
-    while (end < maxDim) {
-      if (rowOrColumn.get(end)) {
-        end++;
-      } else {
-        int whiteRunStart = end;
-        do {
-          end++;
-        } while (end < maxDim && !rowOrColumn.get(end));
-        int whiteRunSize = end - whiteRunStart;
-        if (end >= maxDim || whiteRunSize > maxWhiteRun) {
-          end = whiteRunStart - 1;
-          break;
-        }
-      }
-    }
-    end--;
-
-    if (end > start) {
-      return new int[] { start, end };
-    } else {
-      return null;
-    }
-  }
-
   private static BitMatrix sampleGrid(MonochromeBitmapSource image,
                                       ResultPoint topLeft,
                                       ResultPoint bottomLeft,
index 78f7626..235950e 100644 (file)
@@ -33,6 +33,7 @@ import java.io.IOException;
 import java.io.OutputStreamWriter;
 import java.io.Writer;
 import java.net.URI;
+import java.net.URISyntaxException;
 import java.nio.charset.Charset;
 import java.util.Hashtable;
 
@@ -72,7 +73,7 @@ public final class CommandLineRunner {
   }
 
   private static void decodeOneArgument(String argument, Hashtable<DecodeHintType, Object> hints,
-      boolean dumpResults) throws Exception {
+      boolean dumpResults) throws IOException, URISyntaxException {
 
     File inputFile = new File(argument);
     if (inputFile.exists()) {