Issue 158: Correct some off-by-one problems in Data Matrix detector and a few more...
authorsrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Mon, 6 Apr 2009 21:22:07 +0000 (21:22 +0000)
committersrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Mon, 6 Apr 2009 21:22:07 +0000 (21:22 +0000)
git-svn-id: http://zxing.googlecode.com/svn/trunk@895 59b500cc-1b3d-0410-9834-0bbf25fbcc57

core/src/com/google/zxing/common/detector/MonochromeRectangleDetector.java
core/src/com/google/zxing/datamatrix/decoder/DecodedBitStreamParser.java
core/src/com/google/zxing/datamatrix/detector/Detector.java
core/test/src/com/google/zxing/datamatrix/DataMatrixBlackBox2TestCase.java

index 3b3ced1..63240d0 100644 (file)
@@ -159,7 +159,7 @@ public final class MonochromeRectangleDetector {
    */
   private int[] blackWhiteRange(int fixedDimension, int maxWhiteRun, int minDim, int maxDim, boolean horizontal) {
 
-    int center = (minDim + maxDim) / 2;
+    int center = (minDim + maxDim) >> 1;
 
     BitArray rowOrColumn = horizontal ? image.getBlackRow(fixedDimension, null, 0, image.getWidth())
                                       : image.getBlackColumn(fixedDimension, null, 0, image.getHeight());
@@ -176,7 +176,7 @@ public final class MonochromeRectangleDetector {
         } while (start >= minDim && !rowOrColumn.get(start));
         int whiteRunSize = whiteRunStart - start;
         if (start < minDim || whiteRunSize > maxWhiteRun) {
-          start = whiteRunStart + 1; // back up
+          start = whiteRunStart;
           break;
         }
       }
@@ -195,18 +195,14 @@ public final class MonochromeRectangleDetector {
         } while (end < maxDim && !rowOrColumn.get(end));
         int whiteRunSize = end - whiteRunStart;
         if (end >= maxDim || whiteRunSize > maxWhiteRun) {
-          end = whiteRunStart - 1;
+          end = whiteRunStart;
           break;
         }
       }
     }
     end--;
 
-    if (end > start) {
-      return new int[] { start, end };
-    } else {
-      return null;
-    }
+    return end > start ? new int[]{start, end} : null;
   }
 
 }
\ No newline at end of file
index 2f7c1f4..b5996cc 100644 (file)
@@ -141,11 +141,14 @@ final class DecodedBitStreamParser {
            } else if (oneByte == 231) {  // Latch to Base 256 encodation
                return BASE256_ENCODE;
            } else if (oneByte == 232) {  // FNC1
-               throw ReaderException.getInstance();
+               //throw ReaderException.getInstance();
+        // Ignore this symbol for now
            } else if (oneByte == 233) {  // Structured Append
-               throw ReaderException.getInstance();
+               //throw ReaderException.getInstance();
+        // Ignore this symbol for now
            } else if (oneByte == 234) {  // Reader Programming
-               throw ReaderException.getInstance();
+               //throw ReaderException.getInstance();
+        // Ignore this symbol for now
            } else if (oneByte == 235) {  // Upper Shift (shift to Extended ASCII)
                upperShift = true;
            } else if (oneByte == 236) {  // 05 Macro
@@ -162,7 +165,8 @@ final class DecodedBitStreamParser {
                return EDIFACT_ENCODE;
            } else if (oneByte == 241) {  // ECI Character
                // TODO(bbrown): I think we need to support ECI
-               throw ReaderException.getInstance();
+               //throw ReaderException.getInstance();
+        // Ignore this symbol for now
            } else if (oneByte >= 242) {  // Not to be used in ASCII encodation
                throw ReaderException.getInstance();
            }
index 3cd2c1d..30fb437 100644 (file)
@@ -143,16 +143,11 @@ public final class Detector {
 
     // The top right point is actually the corner of a module, which is one of the two black modules
     // adjacent to the white module at the top right. Tracing to that corner from either the top left
-    // or bottom right should work here, but, one will be more reliable since it's traced straight
-    // up or across, rather than at a slight angle. We use dot products to figure out which is
-    // better to use:
-    int dimension;
-    if (GenericResultPoint.crossProductZ(bottomLeft, bottomRight, topRight) <
-        GenericResultPoint.crossProductZ(topRight, topLeft, bottomLeft)) {
-      dimension = transitionsBetween(topLeft, topRight).getTransitions();
-    } else {
-      dimension = transitionsBetween(bottomRight, topRight).getTransitions();
-    }
+    // or bottom right should work here. The number of transitions could be higher than it should be
+    // due to noise. So we try both and take the min.
+
+    int dimension = Math.min(transitionsBetween(topLeft, topRight).getTransitions(), 
+                             transitionsBetween(bottomRight, topRight).getTransitions());
     dimension += 2;
 
     BitMatrix bits = sampleGrid(image, topLeft, bottomLeft, bottomRight, dimension);
index 9d84be7..bb13f1c 100644 (file)
@@ -29,8 +29,8 @@ public final class DataMatrixBlackBox2TestCase extends AbstractBlackBoxTestCase
     super("test/data/blackbox/datamatrix-2", new DataMatrixReader(), BarcodeFormat.DATAMATRIX);
     addTest(3, 3, 0.0f);
     addTest(1, 1, 90.0f);
-    addTest(3, 3, 180.0f);
-    addTest(4, 4, 270.0f);
+    addTest(4, 4, 180.0f);
+    addTest(3, 3, 270.0f);
   }
 
 }