Search in sources :

Example 51 with DecoderResult

use of com.google.zxing.common.DecoderResult in project incubator-weex by apache.

the class DecodedBitStreamParser method decode.

static DecoderResult decode(byte[] bytes, int mode) {
    StringBuilder result = new StringBuilder(144);
    switch(mode) {
        case 2:
        case 3:
            String postcode;
            if (mode == 2) {
                int pc = getPostCode2(bytes);
                NumberFormat df = new DecimalFormat("0000000000".substring(0, getPostCode2Length(bytes)));
                postcode = df.format(pc);
            } else {
                postcode = getPostCode3(bytes);
            }
            String country = THREE_DIGITS.format(getCountry(bytes));
            String service = THREE_DIGITS.format(getServiceClass(bytes));
            result.append(getMessage(bytes, 10, 84));
            if (result.toString().startsWith("[)>" + RS + "01" + GS)) {
                result.insert(9, postcode + GS + country + GS + service + GS);
            } else {
                result.insert(0, postcode + GS + country + GS + service + GS);
            }
            break;
        case 4:
            result.append(getMessage(bytes, 1, 93));
            break;
        case 5:
            result.append(getMessage(bytes, 1, 77));
            break;
    }
    return new DecoderResult(bytes, result.toString(), null, String.valueOf(mode));
}
Also used : DecimalFormat(java.text.DecimalFormat) DecoderResult(com.google.zxing.common.DecoderResult) NumberFormat(java.text.NumberFormat)

Example 52 with DecoderResult

use of com.google.zxing.common.DecoderResult in project incubator-weex by apache.

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 53 with DecoderResult

use of com.google.zxing.common.DecoderResult in project incubator-weex by apache.

the class Decoder method decode.

public DecoderResult decode(AztecDetectorResult detectorResult) throws FormatException {
    ddata = detectorResult;
    BitMatrix matrix = detectorResult.getBits();
    boolean[] rawbits = extractBits(matrix);
    boolean[] correctedBits = correctBits(rawbits);
    String result = getEncodedData(correctedBits);
    return new DecoderResult(null, result, null, null);
}
Also used : DecoderResult(com.google.zxing.common.DecoderResult) BitMatrix(com.google.zxing.common.BitMatrix)

Example 54 with DecoderResult

use of com.google.zxing.common.DecoderResult in project incubator-weex by apache.

the class Decoder method decode.

/**
 * <p>Decodes a QR Code represented as a {@link BitMatrix}. A 1 or "true" is taken to mean a black module.</p>
 *
 * @param bits booleans representing white/black QR Code modules
 * @param hints decoding hints that should be used to influence decoding
 * @return text and bytes encoded within the QR Code
 * @throws FormatException if the QR Code cannot be decoded
 * @throws ChecksumException if error correction fails
 */
public DecoderResult decode(BitMatrix bits, Map<DecodeHintType, ?> hints) throws FormatException, ChecksumException {
    // Construct a parser and read version, error-correction level
    BitMatrixParser parser = new BitMatrixParser(bits);
    FormatException fe = null;
    ChecksumException ce = null;
    try {
        return decode(parser, hints);
    } catch (FormatException e) {
        fe = e;
    } catch (ChecksumException e) {
        ce = e;
    }
    try {
        // Revert the bit matrix
        parser.remask();
        // Will be attempting a mirrored reading of the version and format info.
        parser.setMirror(true);
        // Preemptively read the version.
        parser.readVersion();
        // Preemptively read the format information.
        parser.readFormatInformation();
        /*
       * Since we're here, this means we have successfully detected some kind
       * of version and format information when mirrored. This is a good sign,
       * that the QR code may be mirrored, and we should try once more with a
       * mirrored content.
       */
        // Prepare for a mirrored reading.
        parser.mirror();
        DecoderResult result = decode(parser, hints);
        // Success! Notify the caller that the code was mirrored.
        result.setOther(new QRCodeDecoderMetaData(true));
        return result;
    } catch (FormatException | ChecksumException e) {
        // Throw the exception from the original reading
        if (fe != null) {
            throw fe;
        }
        if (ce != null) {
            throw ce;
        }
        throw e;
    }
}
Also used : ChecksumException(com.google.zxing.ChecksumException) DecoderResult(com.google.zxing.common.DecoderResult) FormatException(com.google.zxing.FormatException)

Example 55 with DecoderResult

use of com.google.zxing.common.DecoderResult in project incubator-weex by apache.

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));
            }
            if (mode != Mode.TERMINATOR) {
                if (mode == Mode.FNC1_FIRST_POSITION || mode == Mode.FNC1_SECOND_POSITION) {
                    // We do little with FNC1 except alter the parsed result a bit according to the spec
                    fc1InEffect = true;
                } else if (mode == Mode.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);
                } else if (mode == Mode.ECI) {
                    // Count doesn't apply to ECI
                    int value = parseECIValue(bits);
                    currentCharacterSetECI = CharacterSetECI.getCharacterSetECIByValue(value);
                    if (currentCharacterSetECI == null) {
                        throw FormatException.getFormatInstance();
                    }
                } else {
                    // First handle Hanzi mode which does not start with character count
                    if (mode == Mode.HANZI) {
                        // 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);
                        }
                    } else {
                        // "Normal" QR code modes:
                        // How many characters will follow, encoded in this mode?
                        int count = bits.readBits(mode.getCharacterCountBits(version));
                        if (mode == Mode.NUMERIC) {
                            decodeNumericSegment(bits, result, count);
                        } else if (mode == Mode.ALPHANUMERIC) {
                            decodeAlphanumericSegment(bits, result, count, fc1InEffect);
                        } else if (mode == Mode.BYTE) {
                            decodeByteSegment(bits, result, count, currentCharacterSetECI, byteSegments, hints);
                        } else if (mode == Mode.KANJI) {
                            decodeKanjiSegment(bits, result, count);
                        } else {
                            throw FormatException.getFormatInstance();
                        }
                    }
                }
            }
        } 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)

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