+++ /dev/null
-/*\r
-* Copyright 2007 ZXing authors\r
-*\r
-* Licensed under the Apache License, Version 2.0 (the "License");\r
-* you may not use this file except in compliance with the License.\r
-* You may obtain a copy of the License at\r
-*\r
-* http://www.apache.org/licenses/LICENSE-2.0\r
-*\r
-* Unless required by applicable law or agreed to in writing, software\r
-* distributed under the License is distributed on an "AS IS" BASIS,\r
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
-* See the License for the specific language governing permissions and\r
-* limitations under the License.\r
-*/\r
-using System;\r
-using com.google.zxing;\r
-using com.google.zxing.common;\r
-\r
-namespace com.google.zxing.qrcode.detector\r
-{\r
- using Version = com.google.zxing.qrcode.decoder.Version; \r
-\r
- public sealed class Detector\r
- { \r
- private MonochromeBitmapSource image;\r
-\r
- public Detector(MonochromeBitmapSource image) {\r
- this.image = image;\r
- }\r
-\r
- /**\r
- * <p>Detects a QR Code in an image, simply.</p>\r
- *\r
- * @return {@link DetectorResult} encapsulating results of detecting a QR Code\r
- * @throws ReaderException if no QR Code can be found\r
- */\r
- public DetectorResult detect(){\r
- try{\r
- return detect(null);\r
- }catch(Exception e){\r
- throw new ReaderException(e.Message);\r
- } \r
- }\r
-\r
- /**\r
- * <p>Detects a QR Code in an image, simply.</p>\r
- *\r
- * @param hints optional hints to detector\r
- * @return {@link DetectorResult} encapsulating results of detecting a QR Code\r
- * @throws ReaderException if no QR Code can be found\r
- */\r
- public DetectorResult detect(System.Collections.Hashtable hints) {\r
-\r
- MonochromeBitmapSource image = this.image;\r
- if (!BlackPointEstimationMethod.TWO_D_SAMPLING.Equals(image.getLastEstimationMethod())) {\r
- image.estimateBlackPoint(BlackPointEstimationMethod.TWO_D_SAMPLING, 0);\r
- }\r
-\r
- FinderPatternFinder finder = new FinderPatternFinder(image);\r
- FinderPatternInfo info = finder.find(hints);\r
-\r
- FinderPattern topLeft = info.getTopLeft();\r
- FinderPattern topRight = info.getTopRight();\r
- FinderPattern bottomLeft = info.getBottomLeft();\r
-\r
- float moduleSize = calculateModuleSize(topLeft, topRight, bottomLeft);\r
- if (moduleSize < 1.0f) {\r
- throw new ReaderException();\r
- }\r
- int dimension = computeDimension(topLeft, topRight, bottomLeft, moduleSize);\r
-\r
- Version provisionalVersion = Version.getProvisionalVersionForDimension(dimension);\r
- int modulesBetweenFPCenters = provisionalVersion.getDimensionForVersion() - 7;\r
-\r
- AlignmentPattern alignmentPattern = null;\r
- // Anything above version 1 has an alignment pattern\r
- if (provisionalVersion.getAlignmentPatternCenters().Length > 0) {\r
-\r
- // Guess where a "bottom right" finder pattern would have been\r
- float bottomRightX = topRight.getX() - topLeft.getX() + bottomLeft.getX();\r
- float bottomRightY = topRight.getY() - topLeft.getY() + bottomLeft.getY();\r
-\r
- // Estimate that alignment pattern is closer by 3 modules\r
- // from "bottom right" to known top left location\r
- float correctionToTopLeft = 1.0f - 3.0f / (float) modulesBetweenFPCenters;\r
- int estAlignmentX = (int) (topLeft.getX() + correctionToTopLeft * (bottomRightX - topLeft.getX()));\r
- int estAlignmentY = (int) (topLeft.getY() + correctionToTopLeft * (bottomRightY - topLeft.getY()));\r
-\r
- // Kind of arbitrary -- expand search radius before giving up\r
- for (int i = 4; i <= 16; i <<= 1) {\r
- try {\r
- alignmentPattern = findAlignmentInRegion(moduleSize,\r
- estAlignmentX,\r
- estAlignmentY,\r
- (float) i);\r
- break;\r
- } catch (ReaderException re) {\r
- // try next round\r
- }\r
- }\r
- if (alignmentPattern == null) {\r
- throw new ReaderException();\r
- }\r
-\r
- }\r
-\r
- BitMatrix bits = sampleGrid(image, topLeft, topRight, bottomLeft, alignmentPattern, dimension);\r
-\r
- ResultPoint[] points;\r
- if (alignmentPattern == null) {\r
- points = new ResultPoint[]{bottomLeft, topLeft, topRight};\r
- } else {\r
- points = new ResultPoint[]{bottomLeft, topLeft, topRight, alignmentPattern};\r
- }\r
- return new DetectorResult(bits, points);\r
- }\r
-\r
- private static BitMatrix sampleGrid(MonochromeBitmapSource image,\r
- ResultPoint topLeft,\r
- ResultPoint topRight,\r
- ResultPoint bottomLeft,\r
- ResultPoint alignmentPattern,\r
- int dimension) {\r
- float dimMinusThree = (float) dimension - 3.5f;\r
- float bottomRightX;\r
- float bottomRightY;\r
- float sourceBottomRightX;\r
- float sourceBottomRightY;\r
- if (alignmentPattern != null) {\r
- bottomRightX = alignmentPattern.getX();\r
- bottomRightY = alignmentPattern.getY();\r
- sourceBottomRightX = sourceBottomRightY = dimMinusThree - 3.0f;\r
- } else {\r
- // Don't have an alignment pattern, just make up the bottom-right point\r
- bottomRightX = (topRight.getX() - topLeft.getX()) + bottomLeft.getX();\r
- bottomRightY = (topRight.getY() - topLeft.getY()) + bottomLeft.getY();\r
- sourceBottomRightX = sourceBottomRightY = dimMinusThree;\r
- }\r
-\r
- GridSampler sampler = GridSampler.Instance;\r
- return sampler.sampleGrid(\r
- image,\r
- dimension,\r
- 3.5f,\r
- 3.5f,\r
- dimMinusThree,\r
- 3.5f,\r
- sourceBottomRightX,\r
- sourceBottomRightY,\r
- 3.5f,\r
- dimMinusThree,\r
- topLeft.getX(),\r
- topLeft.getY(),\r
- topRight.getX(),\r
- topRight.getY(),\r
- bottomRightX,\r
- bottomRightY,\r
- bottomLeft.getX(),\r
- bottomLeft.getY());\r
- }\r
-\r
- /**\r
- * <p>Computes the dimension (number of modules on a size) of the QR Code based on the position\r
- * of the finder patterns and estimated module size.</p>\r
- */\r
- private static int computeDimension(ResultPoint topLeft,\r
- ResultPoint topRight,\r
- ResultPoint bottomLeft,\r
- float moduleSize) {\r
- int tltrCentersDimension = round(GenericResultPoint.distance(topLeft, topRight) / moduleSize);\r
- int tlblCentersDimension = round(GenericResultPoint.distance(topLeft, bottomLeft) / moduleSize);\r
- int dimension = ((tltrCentersDimension + tlblCentersDimension) >> 1) + 7;\r
- switch (dimension & 0x03) { // mod 4\r
- case 0:\r
- dimension++;\r
- break;\r
- // 1? do nothing\r
- case 2:\r
- dimension--;\r
- break;\r
- case 3:\r
- throw new ReaderException();\r
- }\r
- return dimension;\r
- }\r
-\r
- /**\r
- * <p>Computes an average estimated module size based on estimated derived from the positions\r
- * of the three finder patterns.</p>\r
- */\r
- private float calculateModuleSize(ResultPoint topLeft, ResultPoint topRight, ResultPoint bottomLeft) {\r
- // Take the average\r
- return (calculateModuleSizeOneWay(topLeft, topRight) +\r
- calculateModuleSizeOneWay(topLeft, bottomLeft)) / 2.0f;\r
- }\r
-\r
- /**\r
- * <p>Estimates module size based on two finder patterns -- it uses\r
- * {@link #sizeOfBlackWhiteBlackRunBothWays(int, int, int, int)} to figure the\r
- * width of each, measuring along the axis between their centers.</p>\r
- */\r
- private float calculateModuleSizeOneWay(ResultPoint pattern, ResultPoint otherPattern) {\r
- float moduleSizeEst1 = sizeOfBlackWhiteBlackRunBothWays((int) pattern.getX(),\r
- (int) pattern.getY(),\r
- (int) otherPattern.getX(),\r
- (int) otherPattern.getY());\r
- float moduleSizeEst2 = sizeOfBlackWhiteBlackRunBothWays((int) otherPattern.getX(),\r
- (int) otherPattern.getY(),\r
- (int) pattern.getX(),\r
- (int) pattern.getY());\r
- if (Single.IsNaN(moduleSizeEst1)) {\r
- return moduleSizeEst2;\r
- }\r
- if (Single.IsNaN(moduleSizeEst2))\r
- {\r
- return moduleSizeEst1;\r
- }\r
- // Average them, and divide by 7 since we've counted the width of 3 black modules,\r
- // and 1 white and 1 black module on either side. Ergo, divide sum by 14.\r
- return (moduleSizeEst1 + moduleSizeEst2) / 14.0f;\r
- }\r
-\r
- /**\r
- * See {@link #sizeOfBlackWhiteBlackRun(int, int, int, int)}; computes the total width of\r
- * a finder pattern by looking for a black-white-black run from the center in the direction\r
- * of another point (another finder pattern center), and in the opposite direction too.</p>\r
- */\r
- private float sizeOfBlackWhiteBlackRunBothWays(int fromX, int fromY, int toX, int toY) {\r
-\r
- float result = sizeOfBlackWhiteBlackRun(fromX, fromY, toX, toY);\r
-\r
- // Now count other way -- don't run off image though of course\r
- int otherToX = fromX - (toX - fromX);\r
- if (otherToX < 0) {\r
- // "to" should the be the first value not included, so, the first value off\r
- // the edge is -1\r
- otherToX = -1;\r
- } else if (otherToX >= image.getWidth()) {\r
- otherToX = image.getWidth();\r
- }\r
- int otherToY = fromY - (toY - fromY);\r
- if (otherToY < 0) {\r
- otherToY = -1;\r
- } else if (otherToY >= image.getHeight()) {\r
- otherToY = image.getHeight();\r
- }\r
- result += sizeOfBlackWhiteBlackRun(fromX, fromY, otherToX, otherToY);\r
- return result - 1.0f; // -1 because we counted the middle pixel twice\r
- }\r
-\r
- /**\r
- * <p>This method traces a line from a point in the image, in the direction towards another point.\r
- * It begins in a black region, and keeps going until it finds white, then black, then white again.\r
- * It reports the distance from the start to this point.</p>\r
- *\r
- * <p>This is used when figuring out how wide a finder pattern is, when the finder pattern\r
- * may be skewed or rotated.</p>\r
- */\r
- private float sizeOfBlackWhiteBlackRun(int fromX, int fromY, int toX, int toY) {\r
- // Mild variant of Bresenham's algorithm;\r
- // see http://en.wikipedia.org/wiki/Bresenham's_line_algorithm\r
- bool steep = Math.Abs(toY - fromY) > Math.Abs(toX - fromX);\r
- if (steep) {\r
- int temp = fromX;\r
- fromX = fromY;\r
- fromY = temp;\r
- temp = toX;\r
- toX = toY;\r
- toY = temp;\r
- }\r
-\r
- int dx = Math.Abs(toX - fromX);\r
- int dy = Math.Abs(toY - fromY);\r
- int error = -dx >> 1;\r
- int ystep = fromY < toY ? 1 : -1;\r
- int xstep = fromX < toX ? 1 : -1;\r
- int state = 0; // In black pixels, looking for white, first or second time\r
- int diffX =0;\r
- int diffY =0;\r
-\r
- for (int x = fromX, y = fromY; x != toX; x += xstep) {\r
-\r
- int realX = steep ? y : x;\r
- int realY = steep ? x : y;\r
- if (state == 1) { // In white pixels, looking for black\r
- if (image.isBlack(realX, realY)) {\r
- state++;\r
- }\r
- } else {\r
- if (!image.isBlack(realX, realY)) {\r
- state++;\r
- }\r
- }\r
-\r
- if (state == 3) { // Found black, white, black, and stumbled back onto white; done\r
- diffX = x - fromX;\r
- diffY = y - fromY;\r
- return (float) Math.Sqrt((double) (diffX * diffX + diffY * diffY));\r
- }\r
- error += dy;\r
- if (error > 0) {\r
- y += ystep;\r
- error -= dx;\r
- }\r
- }\r
-\r
- diffX = toX - fromX;\r
- diffY = toY - fromY;\r
- return (float) Math.Sqrt((double) (diffX * diffX + diffY * diffY));\r
- }\r
-\r
- /**\r
- * <p>Attempts to locate an alignment pattern in a limited region of the image, which is\r
- * guessed to contain it. This method uses {@link AlignmentPattern}.</p>\r
- *\r
- * @param overallEstModuleSize estimated module size so far\r
- * @param estAlignmentX x coordinate of center of area probably containing alignment pattern\r
- * @param estAlignmentY y coordinate of above\r
- * @param allowanceFactor number of pixels in all directons to search from the center\r
- * @return {@link AlignmentPattern} if found, or null otherwise\r
- * @throws ReaderException if an unexpected error occurs during detection\r
- */\r
- private AlignmentPattern findAlignmentInRegion(float overallEstModuleSize,\r
- int estAlignmentX,\r
- int estAlignmentY,\r
- float allowanceFactor){\r
- // Look for an alignment pattern (3 modules in size) around where it\r
- // should be\r
- int allowance = (int) (allowanceFactor * overallEstModuleSize);\r
- int alignmentAreaLeftX = Math.Max(0, estAlignmentX - allowance);\r
- int alignmentAreaRightX = Math.Min(image.getWidth() - 1, estAlignmentX + allowance);\r
- if (alignmentAreaRightX - alignmentAreaLeftX < overallEstModuleSize * 3) {\r
- throw new ReaderException();\r
- }\r
-\r
- int alignmentAreaTopY = Math.Max(0, estAlignmentY - allowance);\r
- int alignmentAreaBottomY = Math.Min(image.getHeight() - 1, estAlignmentY + allowance);\r
-\r
- AlignmentPatternFinder alignmentFinder =\r
- new AlignmentPatternFinder(\r
- image,\r
- alignmentAreaLeftX,\r
- alignmentAreaTopY,\r
- alignmentAreaRightX - alignmentAreaLeftX,\r
- alignmentAreaBottomY - alignmentAreaTopY,\r
- overallEstModuleSize);\r
- return alignmentFinder.find();\r
- }\r
-\r
- /**\r
- * Ends up being a bit faster than Math.round(). This merely rounds its argument to the nearest int,\r
- * where x.5 rounds up.\r
- */\r
- private static int round(float d) {\r
- return (int) (d + 0.5f);\r
- }\r
- \r
- }\r
-\r
-\r
-}
\ No newline at end of file