Search in sources :

Example 1 with DecoderResult

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

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);
    byte[] rawBytes = convertBoolArrayToByteArray(correctedBits);
    String result = getEncodedData(correctedBits);
    DecoderResult decoderResult = new DecoderResult(rawBytes, result, null, null);
    decoderResult.setNumBits(correctedBits.length);
    return decoderResult;
}
Also used : DecoderResult(com.google.zxing.common.DecoderResult) BitMatrix(com.google.zxing.common.BitMatrix)

Example 2 with DecoderResult

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

the class EncoderTest method testWriter.

private static void testWriter(String data, String charset, int eccPercent, boolean compact, int layers) throws FormatException {
    // 1. Perform an encode-decode round-trip because it can be lossy.
    // 2. Aztec Decoder currently always decodes the data with a LATIN-1 charset:
    String expectedData = new String(data.getBytes(Charset.forName(charset)), StandardCharsets.ISO_8859_1);
    Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
    hints.put(EncodeHintType.CHARACTER_SET, charset);
    hints.put(EncodeHintType.ERROR_CORRECTION, eccPercent);
    AztecWriter writer = new AztecWriter();
    BitMatrix matrix = writer.encode(data, BarcodeFormat.AZTEC, 0, 0, hints);
    AztecCode aztec = Encoder.encode(data.getBytes(Charset.forName(charset)), eccPercent, Encoder.DEFAULT_AZTEC_LAYERS);
    assertEquals("Unexpected symbol format (compact)", compact, aztec.isCompact());
    assertEquals("Unexpected nr. of layers", layers, aztec.getLayers());
    BitMatrix matrix2 = aztec.getMatrix();
    assertEquals(matrix, matrix2);
    AztecDetectorResult r = new AztecDetectorResult(matrix, NO_POINTS, aztec.isCompact(), aztec.getCodeWords(), aztec.getLayers());
    DecoderResult res = new Decoder().decode(r);
    assertEquals(expectedData, res.getText());
    // Check error correction by introducing up to eccPercent/2 errors
    int ecWords = aztec.getCodeWords() * eccPercent / 100 / 2;
    Random random = getPseudoRandom();
    for (int i = 0; i < ecWords; i++) {
        // don't touch the core
        int x = random.nextBoolean() ? random.nextInt(aztec.getLayers() * 2) : matrix.getWidth() - 1 - random.nextInt(aztec.getLayers() * 2);
        int y = random.nextBoolean() ? random.nextInt(aztec.getLayers() * 2) : matrix.getHeight() - 1 - random.nextInt(aztec.getLayers() * 2);
        matrix.flip(x, y);
    }
    r = new AztecDetectorResult(matrix, NO_POINTS, aztec.isCompact(), aztec.getCodeWords(), aztec.getLayers());
    res = new Decoder().decode(r);
    assertEquals(expectedData, res.getText());
}
Also used : BitMatrix(com.google.zxing.common.BitMatrix) AztecWriter(com.google.zxing.aztec.AztecWriter) Decoder(com.google.zxing.aztec.decoder.Decoder) ResultPoint(com.google.zxing.ResultPoint) EncodeHintType(com.google.zxing.EncodeHintType) Random(java.util.Random) DecoderResult(com.google.zxing.common.DecoderResult) AztecDetectorResult(com.google.zxing.aztec.AztecDetectorResult) EnumMap(java.util.EnumMap)

Example 3 with DecoderResult

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

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

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

the class DecodedBitStreamParser method decode.

static DecoderResult decode(byte[] bytes) throws FormatException {
    BitSource bits = new BitSource(bytes);
    StringBuilder result = new StringBuilder(100);
    StringBuilder resultTrailer = new StringBuilder(0);
    List<byte[]> byteSegments = new ArrayList<>(1);
    Mode mode = Mode.ASCII_ENCODE;
    do {
        if (mode == Mode.ASCII_ENCODE) {
            mode = decodeAsciiSegment(bits, result, resultTrailer);
        } else {
            switch(mode) {
                case C40_ENCODE:
                    decodeC40Segment(bits, result);
                    break;
                case TEXT_ENCODE:
                    decodeTextSegment(bits, result);
                    break;
                case ANSIX12_ENCODE:
                    decodeAnsiX12Segment(bits, result);
                    break;
                case EDIFACT_ENCODE:
                    decodeEdifactSegment(bits, result);
                    break;
                case BASE256_ENCODE:
                    decodeBase256Segment(bits, result, byteSegments);
                    break;
                default:
                    throw FormatException.getFormatInstance();
            }
            mode = Mode.ASCII_ENCODE;
        }
    } while (mode != Mode.PAD_ENCODE && bits.available() > 0);
    if (resultTrailer.length() > 0) {
        result.append(resultTrailer);
    }
    return new DecoderResult(bytes, result.toString(), byteSegments.isEmpty() ? null : byteSegments, null);
}
Also used : BitSource(com.google.zxing.common.BitSource) ArrayList(java.util.ArrayList) DecoderResult(com.google.zxing.common.DecoderResult)

Example 5 with DecoderResult

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

the class MaxiCodeReader method decode.

@Override
public Result decode(BinaryBitmap image, Map<DecodeHintType, ?> hints) throws NotFoundException, ChecksumException, FormatException {
    DecoderResult decoderResult;
    if (hints != null && hints.containsKey(DecodeHintType.PURE_BARCODE)) {
        BitMatrix bits = extractPureBits(image.getBlackMatrix());
        decoderResult = decoder.decode(bits, hints);
    } else {
        throw NotFoundException.getNotFoundInstance();
    }
    Result result = new Result(decoderResult.getText(), decoderResult.getRawBytes(), NO_POINTS, BarcodeFormat.MAXICODE);
    String ecLevel = decoderResult.getECLevel();
    if (ecLevel != null) {
        result.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, ecLevel);
    }
    return result;
}
Also used : DecoderResult(com.google.zxing.common.DecoderResult) BitMatrix(com.google.zxing.common.BitMatrix) Result(com.google.zxing.Result) 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