Search in sources :

Example 21 with DecoderResult

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

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(), decoderResult.getNumBits(), points, BarcodeFormat.AZTEC, System.currentTimeMillis());
    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 22 with DecoderResult

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

the class QRCodeMultiReader method decodeMultiple.

@Override
public Result[] decodeMultiple(BinaryBitmap image, Map<DecodeHintType, ?> hints) throws NotFoundException {
    List<Result> results = new ArrayList<>();
    DetectorResult[] detectorResults = new MultiDetector(image.getBlackMatrix()).detectMulti(hints);
    for (DetectorResult detectorResult : detectorResults) {
        try {
            DecoderResult decoderResult = getDecoder().decode(detectorResult.getBits(), hints);
            ResultPoint[] points = detectorResult.getPoints();
            // If the code was mirrored: swap the bottom-left and the top-right points.
            if (decoderResult.getOther() instanceof QRCodeDecoderMetaData) {
                ((QRCodeDecoderMetaData) decoderResult.getOther()).applyMirroredCorrection(points);
            }
            Result result = new Result(decoderResult.getText(), decoderResult.getRawBytes(), points, BarcodeFormat.QR_CODE);
            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);
            }
            if (decoderResult.hasStructuredAppend()) {
                result.putMetadata(ResultMetadataType.STRUCTURED_APPEND_SEQUENCE, decoderResult.getStructuredAppendSequenceNumber());
                result.putMetadata(ResultMetadataType.STRUCTURED_APPEND_PARITY, decoderResult.getStructuredAppendParity());
            }
            results.add(result);
        } catch (ReaderException re) {
        // ignore and continue
        }
    }
    if (results.isEmpty()) {
        return EMPTY_RESULT_ARRAY;
    } else {
        results = processStructuredAppend(results);
        return results.toArray(new Result[results.size()]);
    }
}
Also used : ResultPoint(com.google.zxing.ResultPoint) ArrayList(java.util.ArrayList) Result(com.google.zxing.Result) DetectorResult(com.google.zxing.common.DetectorResult) DecoderResult(com.google.zxing.common.DecoderResult) ReaderException(com.google.zxing.ReaderException) DecoderResult(com.google.zxing.common.DecoderResult) DetectorResult(com.google.zxing.common.DetectorResult) MultiDetector(com.google.zxing.multi.qrcode.detector.MultiDetector) QRCodeDecoderMetaData(com.google.zxing.qrcode.decoder.QRCodeDecoderMetaData)

Example 23 with DecoderResult

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

the class DecodedBitStreamParser method decode.

static DecoderResult decode(byte[] bytes, Version version, ErrorCorrectionLevel ecLevel, Map<DecodeHintType, ?> hints) throws FormatException {
    BitSource bits = new BitSource(bytes);
    StringBuilder result = new StringBuilder(50);
    List<byte[]> byteSegments = new ArrayList<>(1);
    int symbolSequence = -1;
    int parityData = -1;
    try {
        CharacterSetECI currentCharacterSetECI = null;
        boolean fc1InEffect = false;
        Mode mode;
        do {
            // While still another segment to read...
            if (bits.available() < 4) {
                // OK, assume we're done. Really, a TERMINATOR mode should have been recorded here
                mode = Mode.TERMINATOR;
            } else {
                // mode is encoded by 4 bits
                mode = Mode.forBits(bits.readBits(4));
            }
            switch(mode) {
                case TERMINATOR:
                    break;
                case FNC1_FIRST_POSITION:
                case FNC1_SECOND_POSITION:
                    // We do little with FNC1 except alter the parsed result a bit according to the spec
                    fc1InEffect = true;
                    break;
                case STRUCTURED_APPEND:
                    if (bits.available() < 16) {
                        throw FormatException.getFormatInstance();
                    }
                    // sequence number and parity is added later to the result metadata
                    // Read next 8 bits (symbol sequence #) and 8 bits (parity data), then continue
                    symbolSequence = bits.readBits(8);
                    parityData = bits.readBits(8);
                    break;
                case ECI:
                    // Count doesn't apply to ECI
                    int value = parseECIValue(bits);
                    currentCharacterSetECI = CharacterSetECI.getCharacterSetECIByValue(value);
                    if (currentCharacterSetECI == null) {
                        throw FormatException.getFormatInstance();
                    }
                    break;
                case HANZI:
                    // First handle Hanzi mode which does not start with character count
                    // Chinese mode contains a sub set indicator right after mode indicator
                    int subset = bits.readBits(4);
                    int countHanzi = bits.readBits(mode.getCharacterCountBits(version));
                    if (subset == GB2312_SUBSET) {
                        decodeHanziSegment(bits, result, countHanzi);
                    }
                    break;
                default:
                    // "Normal" QR code modes:
                    // How many characters will follow, encoded in this mode?
                    int count = bits.readBits(mode.getCharacterCountBits(version));
                    switch(mode) {
                        case NUMERIC:
                            decodeNumericSegment(bits, result, count);
                            break;
                        case ALPHANUMERIC:
                            decodeAlphanumericSegment(bits, result, count, fc1InEffect);
                            break;
                        case BYTE:
                            decodeByteSegment(bits, result, count, currentCharacterSetECI, byteSegments, hints);
                            break;
                        case KANJI:
                            decodeKanjiSegment(bits, result, count);
                            break;
                        default:
                            throw FormatException.getFormatInstance();
                    }
                    break;
            }
        } while (mode != Mode.TERMINATOR);
    } catch (IllegalArgumentException iae) {
        // from readBits() calls
        throw FormatException.getFormatInstance();
    }
    return new DecoderResult(bytes, result.toString(), byteSegments.isEmpty() ? null : byteSegments, ecLevel == null ? null : ecLevel.toString(), symbolSequence, parityData);
}
Also used : BitSource(com.google.zxing.common.BitSource) ArrayList(java.util.ArrayList) DecoderResult(com.google.zxing.common.DecoderResult) CharacterSetECI(com.google.zxing.common.CharacterSetECI)

Example 24 with DecoderResult

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

the class PDF417ScanningDecoder method decodeCodewords.

private static DecoderResult decodeCodewords(int[] codewords, int ecLevel, int[] erasures) throws FormatException, ChecksumException {
    if (codewords.length == 0) {
        throw FormatException.getFormatInstance();
    }
    int numECCodewords = 1 << (ecLevel + 1);
    int correctedErrorsCount = correctErrors(codewords, erasures, numECCodewords);
    verifyCodewordCount(codewords, numECCodewords);
    // Decode the codewords
    DecoderResult decoderResult = DecodedBitStreamParser.decode(codewords, String.valueOf(ecLevel));
    decoderResult.setErrorsCorrected(correctedErrorsCount);
    decoderResult.setErasures(erasures.length);
    return decoderResult;
}
Also used : DecoderResult(com.google.zxing.common.DecoderResult) ResultPoint(com.google.zxing.ResultPoint)

Example 25 with DecoderResult

use of com.google.zxing.common.DecoderResult in project zxing by zxing.

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(), decoderResult.getNumBits(), points, BarcodeFormat.AZTEC, System.currentTimeMillis());
    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)

Aggregations

DecoderResult (com.google.zxing.common.DecoderResult)57 ResultPoint (com.google.zxing.ResultPoint)27 Result (com.google.zxing.Result)24 BitMatrix (com.google.zxing.common.BitMatrix)21 ArrayList (java.util.ArrayList)16 DetectorResult (com.google.zxing.common.DetectorResult)13 FormatException (com.google.zxing.FormatException)9 BitSource (com.google.zxing.common.BitSource)8 CharacterSetECI (com.google.zxing.common.CharacterSetECI)8 QRCodeDecoderMetaData (com.google.zxing.qrcode.decoder.QRCodeDecoderMetaData)8 Decoder (com.google.zxing.aztec.decoder.Decoder)7 NotFoundException (com.google.zxing.NotFoundException)6 ChecksumException (com.google.zxing.ChecksumException)5 Detector (com.google.zxing.qrcode.detector.Detector)5 ReaderException (com.google.zxing.ReaderException)4 ResultPointCallback (com.google.zxing.ResultPointCallback)4 AztecDetectorResult (com.google.zxing.aztec.AztecDetectorResult)4 Detector (com.google.zxing.aztec.detector.Detector)4 Detector (com.google.zxing.datamatrix.detector.Detector)4 MultiDetector (com.google.zxing.multi.qrcode.detector.MultiDetector)4