private static Rectangle FindBarcodeRectangle(Bitmap bitmap, ResultPoint[] resultPoints) { var startX = resultPoints[0].X; float startY; var width = resultPoints[1].X - resultPoints[0].X; float height; var luminanceSource = new BitmapLuminanceSource(bitmap); var binarizer = new HybridBinarizer(luminanceSource); var bitMatrix = binarizer.BlackMatrix; var whiteRectangleDetector = WhiteRectangleDetector.Create(bitMatrix); var whiteRectanblePoints = whiteRectangleDetector.detect(); if (whiteRectanblePoints != null) { height = whiteRectanblePoints[3].Y - whiteRectanblePoints[0].Y; startY = whiteRectanblePoints[0].Y; } else { height = 1; startY = resultPoints[0].Y; } var barcodeRectangle = new Rectangle((int)startX, (int)startY, (int)width, (int)height); return(barcodeRectangle); }
/// <summary> /// Finds a candidate center point of an Aztec code from an image /// </summary> /// <returns>the center point</returns> private Point getMatrixCenter() { ResultPoint pointA; ResultPoint pointB; ResultPoint pointC; ResultPoint pointD; int cx; int cy; //Get a white rectangle that can be the border of the matrix in center bull's eye or var whiteDetector = WhiteRectangleDetector.Create(image); if (whiteDetector == null) return null; ResultPoint[] cornerPoints = whiteDetector.detect(); if (cornerPoints != null) { pointA = cornerPoints[0]; pointB = cornerPoints[1]; pointC = cornerPoints[2]; pointD = cornerPoints[3]; } else { // This exception can be in case the initial rectangle is white // In that case, surely in the bull's eye, we try to expand the rectangle. cx = image.Width/2; cy = image.Height/2; pointA = getFirstDifferent(new Point(cx + 7, cy - 7), false, 1, -1).toResultPoint(); pointB = getFirstDifferent(new Point(cx + 7, cy + 7), false, 1, 1).toResultPoint(); pointC = getFirstDifferent(new Point(cx - 7, cy + 7), false, -1, 1).toResultPoint(); pointD = getFirstDifferent(new Point(cx - 7, cy - 7), false, -1, -1).toResultPoint(); } //Compute the center of the rectangle cx = MathUtils.round((pointA.X + pointD.X + pointB.X + pointC.X) / 4.0f); cy = MathUtils.round((pointA.Y + pointD.Y + pointB.Y + pointC.Y) / 4.0f); // Redetermine the white rectangle starting from previously computed center. // This will ensure that we end up with a white rectangle in center bull's eye // in order to compute a more accurate center. whiteDetector = WhiteRectangleDetector.Create(image, 15, cx, cy); if (whiteDetector == null) return null; cornerPoints = whiteDetector.detect(); if (cornerPoints != null) { pointA = cornerPoints[0]; pointB = cornerPoints[1]; pointC = cornerPoints[2]; pointD = cornerPoints[3]; } else { // This exception can be in case the initial rectangle is white // In that case we try to expand the rectangle. pointA = getFirstDifferent(new Point(cx + 7, cy - 7), false, 1, -1).toResultPoint(); pointB = getFirstDifferent(new Point(cx + 7, cy + 7), false, 1, 1).toResultPoint(); pointC = getFirstDifferent(new Point(cx - 7, cy + 7), false, -1, 1).toResultPoint(); pointD = getFirstDifferent(new Point(cx - 7, cy - 7), false, -1, -1).toResultPoint(); } // Recompute the center of the rectangle cx = MathUtils.round((pointA.X + pointD.X + pointB.X + pointC.X) / 4.0f); cy = MathUtils.round((pointA.Y + pointD.Y + pointB.Y + pointC.Y) / 4.0f); return new Point(cx, cy); }
/// <summary> /// Initializes a new instance of the <see cref="Detector"/> class. /// </summary> /// <param name="image">The image.</param> public Detector(BitMatrix image) { this.image = image; rectangleDetector = WhiteRectangleDetector.Create(image); }