From: dav.olivier@gmail.com Date: Sat, 4 Sep 2010 18:07:09 +0000 (+0000) Subject: corrected possibilities of crashing while detecting a datamatrix X-Git-Url: http://git.rot13.org/?p=zxing.git;a=commitdiff_plain;h=9a74c654dcd42f9308893eec241cf7aa2ce15503 corrected possibilities of crashing while detecting a datamatrix git-svn-id: http://zxing.googlecode.com/svn/trunk@1575 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- diff --git a/core/src/com/google/zxing/common/detector/WhiteRectangleDetector.java b/core/src/com/google/zxing/common/detector/WhiteRectangleDetector.java index 030d3a27..f5bd2892 100644 --- a/core/src/com/google/zxing/common/detector/WhiteRectangleDetector.java +++ b/core/src/com/google/zxing/common/detector/WhiteRectangleDetector.java @@ -83,6 +83,11 @@ public final class WhiteRectangleDetector { aBlackPointFoundOnBorder = true; } } + + if (right >= width) { + sizeExceeded = true; + break; + } // ..... // . . @@ -95,6 +100,11 @@ public final class WhiteRectangleDetector { aBlackPointFoundOnBorder = true; } } + + if (down >= height) { + sizeExceeded = true; + break; + } // ..... // | . @@ -107,6 +117,11 @@ public final class WhiteRectangleDetector { aBlackPointFoundOnBorder = true; } } + + if (left < 0) { + sizeExceeded = true; + break; + } // .___. // . . @@ -120,7 +135,7 @@ public final class WhiteRectangleDetector { } } - if (right >= width || down >= height || up < 0 || left < 0) { + if (up < 0) { sizeExceeded = true; break; } @@ -271,20 +286,26 @@ public final class WhiteRectangleDetector { private boolean containsBlackPoint(int a, int b, int fixed, boolean horizontal) { if (horizontal) { - for (int x = a; x < b; x++) { + for (int x = a; x <= b; x++) { + if (x>=480 || x < 0 || fixed >= 360 || fixed < 0){ + x++; + } if (image.get(x, fixed)) { return true; } } } else { - for (int y = a; y < b; y++) { + for (int y = a; y <= b; y++) { + if (y>=360 || y < 0 || fixed >= 480 || fixed < 0){ + y++; + } if (image.get(fixed, y)) { return true; } - } } - - return false; } + return false; + } + } \ No newline at end of file diff --git a/core/src/com/google/zxing/datamatrix/detector/Detector.java b/core/src/com/google/zxing/datamatrix/detector/Detector.java index 206e5187..ca1918f7 100644 --- a/core/src/com/google/zxing/datamatrix/detector/Detector.java +++ b/core/src/com/google/zxing/datamatrix/detector/Detector.java @@ -151,6 +151,9 @@ public final class Detector { //correct top right point to match the white module ResultPoint correctedTopRight = correctTopRight(bottomLeft, bottomRight, topLeft, topRight, dimension); + if (correctedTopRight == null){ + correctedTopRight = topRight; + } //We redetermine the dimension using the corrected top right point int dimension2 = Math.max(transitionsBetween(topLeft, correctedTopRight).getTransitions(), @@ -188,6 +191,15 @@ public final class Detector { ResultPoint c2 = new ResultPoint(topRight.getX()+corr*cos, topRight.getY()+corr*sin); + if (!isValid(c1)){ + if (isValid(c2)){ + return c2; + } + return null; + } else if (!isValid(c2)){ + return c1; + } + int l1 = Math.abs(transitionsBetween(topLeft, c1).getTransitions() - transitionsBetween(bottomRight, c1).getTransitions()); int l2 = Math.abs(transitionsBetween(topLeft, c2).getTransitions() - transitionsBetween(bottomRight, c2).getTransitions()); @@ -198,7 +210,11 @@ public final class Detector { return c2; } - // L2 distance + private boolean isValid(ResultPoint p) { + return (p.getX() >= 0 && p.getX() < image.width && p.getY() > 0 && p.getY() < image.height); + } + +// L2 distance private static int distance(ResultPoint a, ResultPoint b) { return (int) Math.round(Math.sqrt((a.getX() - b.getX()) * (a.getX() - b.getX()) + (a.getY() - b.getY())