Search in sources :

Example 26 with ResultPoint

use of com.google.zxing.ResultPoint in project weex-example by KalicyZhou.

the class Detector method extractParameters.

/**
   * Extracts the number of data layers and data blocks from the layer around the bull's eye.
   *
   * @param bullsEyeCorners the array of bull's eye corners
   * @throws NotFoundException in case of too many errors or invalid parameters
   */
private void extractParameters(ResultPoint[] bullsEyeCorners) throws NotFoundException {
    if (!isValid(bullsEyeCorners[0]) || !isValid(bullsEyeCorners[1]) || !isValid(bullsEyeCorners[2]) || !isValid(bullsEyeCorners[3])) {
        throw NotFoundException.getNotFoundInstance();
    }
    int length = 2 * nbCenterLayers;
    // Get the bits around the bull's eye
    int[] sides = { // Right side
    sampleLine(bullsEyeCorners[0], bullsEyeCorners[1], length), // Bottom 
    sampleLine(bullsEyeCorners[1], bullsEyeCorners[2], length), // Left side
    sampleLine(bullsEyeCorners[2], bullsEyeCorners[3], length), // Top 
    sampleLine(bullsEyeCorners[3], bullsEyeCorners[0], length) };
    // bullsEyeCorners[shift] is the corner of the bulls'eye that has three 
    // orientation marks.  
    // sides[shift] is the row/column that goes from the corner with three
    // orientation marks to the corner with two.
    shift = getRotation(sides, length);
    // Flatten the parameter bits into a single 28- or 40-bit long
    long parameterData = 0;
    for (int i = 0; i < 4; i++) {
        int side = sides[(shift + i) % 4];
        if (compact) {
            // Each side of the form ..XXXXXXX. where Xs are parameter data
            parameterData <<= 7;
            parameterData += (side >> 1) & 0x7F;
        } else {
            // Each side of the form ..XXXXX.XXXXX. where Xs are parameter data
            parameterData <<= 10;
            parameterData += ((side >> 2) & (0x1f << 5)) + ((side >> 1) & 0x1F);
        }
    }
    // Corrects parameter data using RS.  Returns just the data portion
    // without the error correction.
    int correctedData = getCorrectedParameterData(parameterData, compact);
    if (compact) {
        // 8 bits:  2 bits layers and 6 bits data blocks
        nbLayers = (correctedData >> 6) + 1;
        nbDataBlocks = (correctedData & 0x3F) + 1;
    } else {
        // 16 bits:  5 bits layers and 11 bits data blocks
        nbLayers = (correctedData >> 11) + 1;
        nbDataBlocks = (correctedData & 0x7FF) + 1;
    }
}
Also used : ResultPoint(com.google.zxing.ResultPoint)

Example 27 with ResultPoint

use of com.google.zxing.ResultPoint in project weex-example by KalicyZhou.

the class Detector method getMatrixCenter.

/**
   * Finds a candidate center point of an Aztec code from an image
   *
   * @return the center point
   */
private Point getMatrixCenter() {
    ResultPoint pointA;
    ResultPoint pointB;
    ResultPoint pointC;
    ResultPoint pointD;
    //Get a white rectangle that can be the border of the matrix in center bull's eye or
    try {
        ResultPoint[] cornerPoints = new WhiteRectangleDetector(image).detect();
        pointA = cornerPoints[0];
        pointB = cornerPoints[1];
        pointC = cornerPoints[2];
        pointD = cornerPoints[3];
    } catch (NotFoundException e) {
        // 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.
        int cx = image.getWidth() / 2;
        int cy = image.getHeight() / 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
    int cx = MathUtils.round((pointA.getX() + pointD.getX() + pointB.getX() + pointC.getX()) / 4.0f);
    int cy = MathUtils.round((pointA.getY() + pointD.getY() + pointB.getY() + pointC.getY()) / 4.0f);
    // in order to compute a more accurate center.
    try {
        ResultPoint[] cornerPoints = new WhiteRectangleDetector(image, 15, cx, cy).detect();
        pointA = cornerPoints[0];
        pointB = cornerPoints[1];
        pointC = cornerPoints[2];
        pointD = cornerPoints[3];
    } catch (NotFoundException e) {
        // 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.getX() + pointD.getX() + pointB.getX() + pointC.getX()) / 4.0f);
    cy = MathUtils.round((pointA.getY() + pointD.getY() + pointB.getY() + pointC.getY()) / 4.0f);
    return new Point(cx, cy);
}
Also used : ResultPoint(com.google.zxing.ResultPoint) WhiteRectangleDetector(com.google.zxing.common.detector.WhiteRectangleDetector) NotFoundException(com.google.zxing.NotFoundException) ResultPoint(com.google.zxing.ResultPoint) ResultPoint(com.google.zxing.ResultPoint)

Example 28 with ResultPoint

use of com.google.zxing.ResultPoint in project weex-example by KalicyZhou.

the class CaptureActivity method drawResultPoints.

/**
	 * Superimpose a line for 1D or dots for 2D to highlight the key features of
	 * the barcode.
	 * 
	 * @param barcode
	 *            A bitmap of the captured image.
	 * @param scaleFactor
	 *            amount by which thumbnail was scaled
	 * @param rawResult
	 *            The decoded results which contains the points to draw.
	 */
private void drawResultPoints(Bitmap barcode, float scaleFactor, Result rawResult) {
    ResultPoint[] points = rawResult.getResultPoints();
    if (points != null && points.length > 0) {
        Canvas canvas = new Canvas(barcode);
        Paint paint = new Paint();
        paint.setColor(getResources().getColor(R.color.result_points));
        if (points.length == 2) {
            paint.setStrokeWidth(4.0f);
            drawLine(canvas, paint, points[0], points[1], scaleFactor);
        } else if (points.length == 4 && (rawResult.getBarcodeFormat() == BarcodeFormat.UPC_A || rawResult.getBarcodeFormat() == BarcodeFormat.EAN_13)) {
            // Hacky special case -- draw two lines, for the barcode and
            // metadata
            drawLine(canvas, paint, points[0], points[1], scaleFactor);
            drawLine(canvas, paint, points[2], points[3], scaleFactor);
        } else {
            paint.setStrokeWidth(10.0f);
            for (ResultPoint point : points) {
                if (point != null) {
                    canvas.drawPoint(scaleFactor * point.getX(), scaleFactor * point.getY(), paint);
                }
            }
        }
    }
}
Also used : ResultPoint(com.google.zxing.ResultPoint) Canvas(android.graphics.Canvas) Paint(android.graphics.Paint)

Example 29 with ResultPoint

use of com.google.zxing.ResultPoint in project weex-example by KalicyZhou.

the class Detector method detect.

/**
   * Detects an Aztec Code in an image.
   *
   * @param isMirror if true, image is a mirror-image of original
   * @return {@link AztecDetectorResult} encapsulating results of detecting an Aztec Code
   * @throws NotFoundException if no Aztec Code can be found
   */
public AztecDetectorResult detect(boolean isMirror) throws NotFoundException {
    // 1. Get the center of the aztec matrix
    Point pCenter = getMatrixCenter();
    // 2. Get the center points of the four diagonal points just outside the bull's eye
    //  [topRight, bottomRight, bottomLeft, topLeft]
    ResultPoint[] bullsEyeCorners = getBullsEyeCorners(pCenter);
    if (isMirror) {
        ResultPoint temp = bullsEyeCorners[0];
        bullsEyeCorners[0] = bullsEyeCorners[2];
        bullsEyeCorners[2] = temp;
    }
    // 3. Get the size of the matrix and other parameters from the bull's eye
    extractParameters(bullsEyeCorners);
    // 4. Sample the grid
    BitMatrix bits = sampleGrid(image, bullsEyeCorners[shift % 4], bullsEyeCorners[(shift + 1) % 4], bullsEyeCorners[(shift + 2) % 4], bullsEyeCorners[(shift + 3) % 4]);
    // 5. Get the corners of the matrix.
    ResultPoint[] corners = getMatrixCornerPoints(bullsEyeCorners);
    return new AztecDetectorResult(bits, corners, compact, nbDataBlocks, nbLayers);
}
Also used : ResultPoint(com.google.zxing.ResultPoint) ResultPoint(com.google.zxing.ResultPoint) BitMatrix(com.google.zxing.common.BitMatrix) AztecDetectorResult(com.google.zxing.aztec.AztecDetectorResult)

Example 30 with ResultPoint

use of com.google.zxing.ResultPoint in project weex-example by KalicyZhou.

the class GenericMultipleBarcodeReader method doDecodeMultiple.

private void doDecodeMultiple(BinaryBitmap image, Map<DecodeHintType, ?> hints, List<Result> results, int xOffset, int yOffset, int currentDepth) {
    if (currentDepth > MAX_DEPTH) {
        return;
    }
    Result result;
    try {
        result = delegate.decode(image, hints);
    } catch (ReaderException ignored) {
        return;
    }
    boolean alreadyFound = false;
    for (Result existingResult : results) {
        if (existingResult.getText().equals(result.getText())) {
            alreadyFound = true;
            break;
        }
    }
    if (!alreadyFound) {
        results.add(translateResultPoints(result, xOffset, yOffset));
    }
    ResultPoint[] resultPoints = result.getResultPoints();
    if (resultPoints == null || resultPoints.length == 0) {
        return;
    }
    int width = image.getWidth();
    int height = image.getHeight();
    float minX = width;
    float minY = height;
    float maxX = 0.0f;
    float maxY = 0.0f;
    for (ResultPoint point : resultPoints) {
        if (point == null) {
            continue;
        }
        float x = point.getX();
        float y = point.getY();
        if (x < minX) {
            minX = x;
        }
        if (y < minY) {
            minY = y;
        }
        if (x > maxX) {
            maxX = x;
        }
        if (y > maxY) {
            maxY = y;
        }
    }
    // Decode left of barcode
    if (minX > MIN_DIMENSION_TO_RECUR) {
        doDecodeMultiple(image.crop(0, 0, (int) minX, height), hints, results, xOffset, yOffset, currentDepth + 1);
    }
    // Decode above barcode
    if (minY > MIN_DIMENSION_TO_RECUR) {
        doDecodeMultiple(image.crop(0, 0, width, (int) minY), hints, results, xOffset, yOffset, currentDepth + 1);
    }
    // Decode right of barcode
    if (maxX < width - MIN_DIMENSION_TO_RECUR) {
        doDecodeMultiple(image.crop((int) maxX, 0, width - (int) maxX, height), hints, results, xOffset + (int) maxX, yOffset, currentDepth + 1);
    }
    // Decode below barcode
    if (maxY < height - MIN_DIMENSION_TO_RECUR) {
        doDecodeMultiple(image.crop(0, (int) maxY, width, height - (int) maxY), hints, results, xOffset, yOffset + (int) maxY, currentDepth + 1);
    }
}
Also used : ResultPoint(com.google.zxing.ResultPoint) ResultPoint(com.google.zxing.ResultPoint) Result(com.google.zxing.Result) ReaderException(com.google.zxing.ReaderException)

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