Search in sources :

Example 36 with NotFoundException

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

the class AztecReader method decode.

@Override
public Result decode(BinaryBitmap image, Map<DecodeHintType, ?> hints) throws NotFoundException, FormatException {
    NotFoundException notFoundException = null;
    FormatException formatException = null;
    Detector detector = new Detector(image.getBlackMatrix());
    ResultPoint[] points = null;
    DecoderResult decoderResult = null;
    try {
        AztecDetectorResult detectorResult = detector.detect(false);
        points = detectorResult.getPoints();
        decoderResult = new Decoder().decode(detectorResult);
    } catch (NotFoundException e) {
        notFoundException = e;
    } catch (FormatException e) {
        formatException = e;
    }
    if (decoderResult == null) {
        try {
            AztecDetectorResult detectorResult = detector.detect(true);
            points = detectorResult.getPoints();
            decoderResult = new Decoder().decode(detectorResult);
        } catch (NotFoundException | FormatException e) {
            if (notFoundException != null) {
                throw notFoundException;
            }
            if (formatException != null) {
                throw formatException;
            }
            throw e;
        }
    }
    if (hints != null) {
        ResultPointCallback rpcb = (ResultPointCallback) hints.get(DecodeHintType.NEED_RESULT_POINT_CALLBACK);
        if (rpcb != null) {
            for (ResultPoint point : points) {
                rpcb.foundPossibleResultPoint(point);
            }
        }
    }
    Result result = new Result(decoderResult.getText(), decoderResult.getRawBytes(), points, BarcodeFormat.AZTEC);
    List<byte[]> byteSegments = decoderResult.getByteSegments();
    if (byteSegments != null) {
        result.putMetadata(ResultMetadataType.BYTE_SEGMENTS, byteSegments);
    }
    String ecLevel = decoderResult.getECLevel();
    if (ecLevel != null) {
        result.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, ecLevel);
    }
    return result;
}
Also used : ResultPointCallback(com.google.zxing.ResultPointCallback) ResultPoint(com.google.zxing.ResultPoint) NotFoundException(com.google.zxing.NotFoundException) Decoder(com.google.zxing.aztec.decoder.Decoder) FormatException(com.google.zxing.FormatException) Result(com.google.zxing.Result) DecoderResult(com.google.zxing.common.DecoderResult) Detector(com.google.zxing.aztec.detector.Detector) DecoderResult(com.google.zxing.common.DecoderResult)

Example 37 with NotFoundException

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

the class RSS14Reader method decodePair.

private Pair decodePair(BitArray row, boolean right, int rowNumber, Map<DecodeHintType, ?> hints) {
    try {
        int[] startEnd = findFinderPattern(row, 0, right);
        FinderPattern pattern = parseFoundFinderPattern(row, rowNumber, right, startEnd);
        ResultPointCallback resultPointCallback = hints == null ? null : (ResultPointCallback) hints.get(DecodeHintType.NEED_RESULT_POINT_CALLBACK);
        if (resultPointCallback != null) {
            float center = (startEnd[0] + startEnd[1]) / 2.0f;
            if (right) {
                // row is actually reversed
                center = row.getSize() - 1 - center;
            }
            resultPointCallback.foundPossibleResultPoint(new ResultPoint(center, rowNumber));
        }
        DataCharacter outside = decodeDataCharacter(row, pattern, true);
        DataCharacter inside = decodeDataCharacter(row, pattern, false);
        return new Pair(1597 * outside.getValue() + inside.getValue(), outside.getChecksumPortion() + 4 * inside.getChecksumPortion(), pattern);
    } catch (NotFoundException ignored) {
        return null;
    }
}
Also used : ResultPointCallback(com.google.zxing.ResultPointCallback) ResultPoint(com.google.zxing.ResultPoint) NotFoundException(com.google.zxing.NotFoundException)

Example 38 with NotFoundException

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

the class Detector method processFinderPatternInfo.

protected final DetectorResult processFinderPatternInfo(FinderPatternInfo info) throws NotFoundException, FormatException {
    FinderPattern topLeft = info.getTopLeft();
    FinderPattern topRight = info.getTopRight();
    FinderPattern bottomLeft = info.getBottomLeft();
    float moduleSize = calculateModuleSize(topLeft, topRight, bottomLeft);
    if (moduleSize < 1.0f) {
        throw NotFoundException.getNotFoundInstance();
    }
    int dimension = computeDimension(topLeft, topRight, bottomLeft, moduleSize);
    Version provisionalVersion = Version.getProvisionalVersionForDimension(dimension);
    int modulesBetweenFPCenters = provisionalVersion.getDimensionForVersion() - 7;
    AlignmentPattern alignmentPattern = null;
    // Anything above version 1 has an alignment pattern
    if (provisionalVersion.getAlignmentPatternCenters().length > 0) {
        // Guess where a "bottom right" finder pattern would have been
        float bottomRightX = topRight.getX() - topLeft.getX() + bottomLeft.getX();
        float bottomRightY = topRight.getY() - topLeft.getY() + bottomLeft.getY();
        // Estimate that alignment pattern is closer by 3 modules
        // from "bottom right" to known top left location
        float correctionToTopLeft = 1.0f - 3.0f / modulesBetweenFPCenters;
        int estAlignmentX = (int) (topLeft.getX() + correctionToTopLeft * (bottomRightX - topLeft.getX()));
        int estAlignmentY = (int) (topLeft.getY() + correctionToTopLeft * (bottomRightY - topLeft.getY()));
        // Kind of arbitrary -- expand search radius before giving up
        for (int i = 4; i <= 16; i <<= 1) {
            try {
                alignmentPattern = findAlignmentInRegion(moduleSize, estAlignmentX, estAlignmentY, i);
                break;
            } catch (NotFoundException re) {
            // try next round
            }
        }
    // If we didn't find alignment pattern... well try anyway without it
    }
    PerspectiveTransform transform = createTransform(topLeft, topRight, bottomLeft, alignmentPattern, dimension);
    BitMatrix bits = sampleGrid(image, transform, dimension);
    ResultPoint[] points;
    if (alignmentPattern == null) {
        points = new ResultPoint[] { bottomLeft, topLeft, topRight };
    } else {
        points = new ResultPoint[] { bottomLeft, topLeft, topRight, alignmentPattern };
    }
    return new DetectorResult(bits, points);
}
Also used : ResultPoint(com.google.zxing.ResultPoint) Version(com.google.zxing.qrcode.decoder.Version) PerspectiveTransform(com.google.zxing.common.PerspectiveTransform) NotFoundException(com.google.zxing.NotFoundException) DetectorResult(com.google.zxing.common.DetectorResult) BitMatrix(com.google.zxing.common.BitMatrix) ResultPoint(com.google.zxing.ResultPoint)

Example 39 with NotFoundException

use of com.google.zxing.NotFoundException in project collect by opendatakit.

the class ShowQRCodeFragment method onActivityResult.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if (result != null) {
        if (result.getContents() == null) {
            // request was canceled...
            Timber.i("QR code scanning cancelled");
        } else {
            try {
                applySettings(CompressionUtils.decompress(result.getContents()));
            } catch (IOException | DataFormatException e) {
                Timber.e(e);
            }
            return;
        }
    }
    if (requestCode == SELECT_PHOTO) {
        if (resultCode == Activity.RESULT_OK) {
            try {
                final Uri imageUri = data.getData();
                final InputStream imageStream = getActivity().getContentResolver().openInputStream(imageUri);
                final Bitmap bitmap = BitmapFactory.decodeStream(imageStream);
                String response = QRCodeUtils.decodeFromBitmap(bitmap);
                if (response != null) {
                    applySettings(response);
                }
            } catch (FormatException | NotFoundException | ChecksumException e) {
                Timber.i(e);
                ToastUtils.showLongToast("QR Code not found in the selected image");
            } catch (IOException e) {
                Timber.e(e);
                ToastUtils.showLongToast("Unable to read the settings");
            } catch (DataFormatException e) {
                Timber.e(e);
                ToastUtils.showShortToast(getString(R.string.invalid_qrcode));
            }
        } else {
            Timber.i("Choosing QR code from sdcard cancelled");
        }
    }
}
Also used : Bitmap(android.graphics.Bitmap) IntentResult(com.google.zxing.integration.android.IntentResult) DataFormatException(java.util.zip.DataFormatException) InputStream(java.io.InputStream) ChecksumException(com.google.zxing.ChecksumException) NotFoundException(com.google.zxing.NotFoundException) IOException(java.io.IOException) Uri(android.net.Uri) FormatException(com.google.zxing.FormatException) DataFormatException(java.util.zip.DataFormatException)

Example 40 with NotFoundException

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

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)

Aggregations

NotFoundException (com.google.zxing.NotFoundException)56 ResultPoint (com.google.zxing.ResultPoint)38 Result (com.google.zxing.Result)22 BinaryBitmap (com.google.zxing.BinaryBitmap)20 DecodeHintType (com.google.zxing.DecodeHintType)12 FormatException (com.google.zxing.FormatException)12 HybridBinarizer (com.google.zxing.common.HybridBinarizer)12 FinderPattern (com.google.zxing.oned.rss.FinderPattern)9 ChecksumException (com.google.zxing.ChecksumException)8 ResultPointCallback (com.google.zxing.ResultPointCallback)8 BitArray (com.google.zxing.common.BitArray)7 BitMatrix (com.google.zxing.common.BitMatrix)7 ReaderException (com.google.zxing.ReaderException)6 DecoderResult (com.google.zxing.common.DecoderResult)6 Hashtable (java.util.Hashtable)6 Decoder (com.google.zxing.aztec.decoder.Decoder)5 DetectorResult (com.google.zxing.common.DetectorResult)5 QRCodeReader (com.google.zxing.qrcode.QRCodeReader)5 EnumMap (java.util.EnumMap)5 BitmapFactory (android.graphics.BitmapFactory)4