Search in sources :

Example 86 with ResultPoint

use of com.google.zxing.ResultPoint in project android-zxing by PearceXu.

the class MonochromeRectangleDetector method detect.

/**
 * <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 NotFoundException if no Data Matrix Code can be found
 */
public ResultPoint[] detect() throws NotFoundException {
    int height = image.getHeight();
    int width = image.getWidth();
    int halfHeight = height / 2;
    int halfWidth = width / 2;
    int deltaY = Math.max(1, height / (MAX_MODULES * 8));
    int deltaX = Math.max(1, width / (MAX_MODULES * 8));
    int top = 0;
    int bottom = height;
    int left = 0;
    int right = width;
    ResultPoint pointA = findCornerFromCenter(halfWidth, 0, left, right, halfHeight, -deltaY, top, bottom, halfWidth / 2);
    top = (int) pointA.getY() - 1;
    ResultPoint pointB = findCornerFromCenter(halfWidth, -deltaX, left, right, halfHeight, 0, top, bottom, halfHeight / 2);
    left = (int) pointB.getX() - 1;
    ResultPoint pointC = findCornerFromCenter(halfWidth, deltaX, left, right, halfHeight, 0, top, bottom, halfHeight / 2);
    right = (int) pointC.getX() + 1;
    ResultPoint pointD = findCornerFromCenter(halfWidth, 0, left, right, halfHeight, deltaY, top, bottom, halfWidth / 2);
    bottom = (int) pointD.getY() + 1;
    // Go try to find point A again with better information -- might have been off at first.
    pointA = findCornerFromCenter(halfWidth, 0, left, right, halfHeight, -deltaY, top, bottom, halfWidth / 4);
    return new ResultPoint[] { pointA, pointB, pointC, pointD };
}
Also used : ResultPoint(com.google.zxing.ResultPoint) ResultPoint(com.google.zxing.ResultPoint)

Example 87 with ResultPoint

use of com.google.zxing.ResultPoint in project android-zxing by PearceXu.

the class MonochromeRectangleDetector method findCornerFromCenter.

/**
 * 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 centerX center's x component (horizontal)
 * @param deltaX same as deltaY but change in x per step instead
 * @param left minimum value of x
 * @param right maximum value of x
 * @param centerY center's y component (vertical)
 * @param deltaY change in y per step. If scanning up this is negative; down, positive;
 *  left or right, 0
 * @param top minimum value of y to search through (meaningless when di == 0)
 * @param bottom maximum value of y
 * @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 NotFoundException if such a point cannot be found
 */
private ResultPoint findCornerFromCenter(int centerX, int deltaX, int left, int right, int centerY, int deltaY, int top, int bottom, int maxWhiteRun) throws NotFoundException {
    int[] lastRange = null;
    for (int y = centerY, x = centerX; y < bottom && y >= top && x < right && x >= left; y += deltaY, x += deltaX) {
        int[] range;
        if (deltaX == 0) {
            // horizontal slices, up and down
            range = blackWhiteRange(y, maxWhiteRun, left, right, true);
        } else {
            // vertical slices, left and right
            range = blackWhiteRange(x, maxWhiteRun, top, bottom, false);
        }
        if (range == null) {
            if (lastRange == null) {
                throw NotFoundException.getNotFoundInstance();
            }
            // lastRange was found
            if (deltaX == 0) {
                int lastY = y - deltaY;
                if (lastRange[0] < centerX) {
                    if (lastRange[1] > centerX) {
                        // straddle, choose one or the other based on direction
                        return new ResultPoint(lastRange[deltaY > 0 ? 0 : 1], lastY);
                    }
                    return new ResultPoint(lastRange[0], lastY);
                } else {
                    return new ResultPoint(lastRange[1], lastY);
                }
            } else {
                int lastX = x - deltaX;
                if (lastRange[0] < centerY) {
                    if (lastRange[1] > centerY) {
                        return new ResultPoint(lastX, lastRange[deltaX < 0 ? 0 : 1]);
                    }
                    return new ResultPoint(lastX, lastRange[0]);
                } else {
                    return new ResultPoint(lastX, lastRange[1]);
                }
            }
        }
        lastRange = range;
    }
    throw NotFoundException.getNotFoundInstance();
}
Also used : ResultPoint(com.google.zxing.ResultPoint) ResultPoint(com.google.zxing.ResultPoint)

Example 88 with ResultPoint

use of com.google.zxing.ResultPoint in project android-zxing by PearceXu.

the class WhiteRectangleDetector method getBlackPointOnSegment.

private ResultPoint getBlackPointOnSegment(float aX, float aY, float bX, float bY) {
    int dist = MathUtils.round(MathUtils.distance(aX, aY, bX, bY));
    float xStep = (bX - aX) / dist;
    float yStep = (bY - aY) / dist;
    for (int i = 0; i < dist; i++) {
        int x = MathUtils.round(aX + i * xStep);
        int y = MathUtils.round(aY + i * yStep);
        if (image.get(x, y)) {
            return new ResultPoint(x, y);
        }
    }
    return null;
}
Also used : ResultPoint(com.google.zxing.ResultPoint) ResultPoint(com.google.zxing.ResultPoint)

Example 89 with ResultPoint

use of com.google.zxing.ResultPoint in project android-zxing by PearceXu.

the class WhiteRectangleDetector method detect.

/**
 * <p>
 * Detects a candidate barcode-like rectangular region within an image. It
 * starts around the center of the image, increases the size of the candidate
 * region until it finds a white rectangular region.
 * </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 NotFoundException if no Data Matrix Code can be found
 */
public ResultPoint[] detect() throws NotFoundException {
    int left = leftInit;
    int right = rightInit;
    int up = upInit;
    int down = downInit;
    boolean sizeExceeded = false;
    boolean aBlackPointFoundOnBorder = true;
    boolean atLeastOneBlackPointFoundOnBorder = false;
    boolean atLeastOneBlackPointFoundOnRight = false;
    boolean atLeastOneBlackPointFoundOnBottom = false;
    boolean atLeastOneBlackPointFoundOnLeft = false;
    boolean atLeastOneBlackPointFoundOnTop = false;
    while (aBlackPointFoundOnBorder) {
        aBlackPointFoundOnBorder = false;
        // .....
        // .   |
        // .....
        boolean rightBorderNotWhite = true;
        while ((rightBorderNotWhite || !atLeastOneBlackPointFoundOnRight) && right < width) {
            rightBorderNotWhite = containsBlackPoint(up, down, right, false);
            if (rightBorderNotWhite) {
                right++;
                aBlackPointFoundOnBorder = true;
                atLeastOneBlackPointFoundOnRight = true;
            } else if (!atLeastOneBlackPointFoundOnRight) {
                right++;
            }
        }
        if (right >= width) {
            sizeExceeded = true;
            break;
        }
        // .....
        // .   .
        // .___.
        boolean bottomBorderNotWhite = true;
        while ((bottomBorderNotWhite || !atLeastOneBlackPointFoundOnBottom) && down < height) {
            bottomBorderNotWhite = containsBlackPoint(left, right, down, true);
            if (bottomBorderNotWhite) {
                down++;
                aBlackPointFoundOnBorder = true;
                atLeastOneBlackPointFoundOnBottom = true;
            } else if (!atLeastOneBlackPointFoundOnBottom) {
                down++;
            }
        }
        if (down >= height) {
            sizeExceeded = true;
            break;
        }
        // .....
        // |   .
        // .....
        boolean leftBorderNotWhite = true;
        while ((leftBorderNotWhite || !atLeastOneBlackPointFoundOnLeft) && left >= 0) {
            leftBorderNotWhite = containsBlackPoint(up, down, left, false);
            if (leftBorderNotWhite) {
                left--;
                aBlackPointFoundOnBorder = true;
                atLeastOneBlackPointFoundOnLeft = true;
            } else if (!atLeastOneBlackPointFoundOnLeft) {
                left--;
            }
        }
        if (left < 0) {
            sizeExceeded = true;
            break;
        }
        // .___.
        // .   .
        // .....
        boolean topBorderNotWhite = true;
        while ((topBorderNotWhite || !atLeastOneBlackPointFoundOnTop) && up >= 0) {
            topBorderNotWhite = containsBlackPoint(left, right, up, true);
            if (topBorderNotWhite) {
                up--;
                aBlackPointFoundOnBorder = true;
                atLeastOneBlackPointFoundOnTop = true;
            } else if (!atLeastOneBlackPointFoundOnTop) {
                up--;
            }
        }
        if (up < 0) {
            sizeExceeded = true;
            break;
        }
        if (aBlackPointFoundOnBorder) {
            atLeastOneBlackPointFoundOnBorder = true;
        }
    }
    if (!sizeExceeded && atLeastOneBlackPointFoundOnBorder) {
        int maxSize = right - left;
        ResultPoint z = null;
        for (int i = 1; z == null && i < maxSize; i++) {
            z = getBlackPointOnSegment(left, down - i, left + i, down);
        }
        if (z == null) {
            throw NotFoundException.getNotFoundInstance();
        }
        ResultPoint t = null;
        // go down right
        for (int i = 1; t == null && i < maxSize; i++) {
            t = getBlackPointOnSegment(left, up + i, left + i, up);
        }
        if (t == null) {
            throw NotFoundException.getNotFoundInstance();
        }
        ResultPoint x = null;
        // go down left
        for (int i = 1; x == null && i < maxSize; i++) {
            x = getBlackPointOnSegment(right, up + i, right - i, up);
        }
        if (x == null) {
            throw NotFoundException.getNotFoundInstance();
        }
        ResultPoint y = null;
        // go up left
        for (int i = 1; y == null && i < maxSize; i++) {
            y = getBlackPointOnSegment(right, down - i, right - i, down);
        }
        if (y == null) {
            throw NotFoundException.getNotFoundInstance();
        }
        return centerEdges(y, z, x, t);
    } else {
        throw NotFoundException.getNotFoundInstance();
    }
}
Also used : ResultPoint(com.google.zxing.ResultPoint) ResultPoint(com.google.zxing.ResultPoint)

Example 90 with ResultPoint

use of com.google.zxing.ResultPoint in project android-zxing by PearceXu.

the class UPCEANExtension5Support method decodeRow.

Result decodeRow(int rowNumber, BitArray row, int[] extensionStartRange) throws NotFoundException {
    StringBuilder result = decodeRowStringBuffer;
    result.setLength(0);
    int end = decodeMiddle(row, extensionStartRange, result);
    String resultString = result.toString();
    Map<ResultMetadataType, Object> extensionData = parseExtensionString(resultString);
    Result extensionResult = new Result(resultString, null, new ResultPoint[] { new ResultPoint((extensionStartRange[0] + extensionStartRange[1]) / 2.0f, rowNumber), new ResultPoint(end, rowNumber) }, BarcodeFormat.UPC_EAN_EXTENSION);
    if (extensionData != null) {
        extensionResult.putAllMetadata(extensionData);
    }
    return extensionResult;
}
Also used : ResultPoint(com.google.zxing.ResultPoint) ResultMetadataType(com.google.zxing.ResultMetadataType) ResultPoint(com.google.zxing.ResultPoint) Result(com.google.zxing.Result)

Aggregations

ResultPoint (com.google.zxing.ResultPoint)252 Result (com.google.zxing.Result)77 Paint (android.graphics.Paint)45 Rect (android.graphics.Rect)24 BitMatrix (com.google.zxing.common.BitMatrix)22 DecoderResult (com.google.zxing.common.DecoderResult)22 NotFoundException (com.google.zxing.NotFoundException)21 DetectorResult (com.google.zxing.common.DetectorResult)20 ArrayList (java.util.ArrayList)20 ReaderException (com.google.zxing.ReaderException)16 SuppressLint (android.annotation.SuppressLint)13 ResultPointCallback (com.google.zxing.ResultPointCallback)12 Canvas (android.graphics.Canvas)10 ResultMetadataType (com.google.zxing.ResultMetadataType)8 BitArray (com.google.zxing.common.BitArray)8 QRCodeDecoderMetaData (com.google.zxing.qrcode.decoder.QRCodeDecoderMetaData)8 BarcodeFormat (com.google.zxing.BarcodeFormat)5 FormatException (com.google.zxing.FormatException)4 Decoder (com.google.zxing.aztec.decoder.Decoder)4 Detector (com.google.zxing.aztec.detector.Detector)4