Search in sources :

Example 26 with Result

use of com.google.zxing.Result in project zxing by zxing.

the class GenericMultipleBarcodeReader method translateResultPoints.

private static Result translateResultPoints(Result result, int xOffset, int yOffset) {
    ResultPoint[] oldResultPoints = result.getResultPoints();
    if (oldResultPoints == null) {
        return result;
    }
    ResultPoint[] newResultPoints = new ResultPoint[oldResultPoints.length];
    for (int i = 0; i < oldResultPoints.length; i++) {
        ResultPoint oldPoint = oldResultPoints[i];
        if (oldPoint != null) {
            newResultPoints[i] = new ResultPoint(oldPoint.getX() + xOffset, oldPoint.getY() + yOffset);
        }
    }
    Result newResult = new Result(result.getText(), result.getRawBytes(), result.getNumBits(), newResultPoints, result.getBarcodeFormat(), result.getTimestamp());
    newResult.putAllMetadata(result.getResultMetadata());
    return newResult;
}
Also used : ResultPoint(com.google.zxing.ResultPoint) ResultPoint(com.google.zxing.ResultPoint) Result(com.google.zxing.Result)

Example 27 with Result

use of com.google.zxing.Result in project zxing by zxing.

the class QRCodeMultiReader method processStructuredAppend.

private static List<Result> processStructuredAppend(List<Result> results) {
    boolean hasSA = false;
    // first, check, if there is at least on SA result in the list
    for (Result result : results) {
        if (result.getResultMetadata().containsKey(ResultMetadataType.STRUCTURED_APPEND_SEQUENCE)) {
            hasSA = true;
            break;
        }
    }
    if (!hasSA) {
        return results;
    }
    // it is, second, split the lists and built a new result list
    List<Result> newResults = new ArrayList<>();
    List<Result> saResults = new ArrayList<>();
    for (Result result : results) {
        newResults.add(result);
        if (result.getResultMetadata().containsKey(ResultMetadataType.STRUCTURED_APPEND_SEQUENCE)) {
            saResults.add(result);
        }
    }
    // sort and concatenate the SA list items
    Collections.sort(saResults, new SAComparator());
    StringBuilder concatedText = new StringBuilder();
    int rawBytesLen = 0;
    int byteSegmentLength = 0;
    for (Result saResult : saResults) {
        concatedText.append(saResult.getText());
        rawBytesLen += saResult.getRawBytes().length;
        if (saResult.getResultMetadata().containsKey(ResultMetadataType.BYTE_SEGMENTS)) {
            @SuppressWarnings("unchecked") Iterable<byte[]> byteSegments = (Iterable<byte[]>) saResult.getResultMetadata().get(ResultMetadataType.BYTE_SEGMENTS);
            for (byte[] segment : byteSegments) {
                byteSegmentLength += segment.length;
            }
        }
    }
    byte[] newRawBytes = new byte[rawBytesLen];
    byte[] newByteSegment = new byte[byteSegmentLength];
    int newRawBytesIndex = 0;
    int byteSegmentIndex = 0;
    for (Result saResult : saResults) {
        System.arraycopy(saResult.getRawBytes(), 0, newRawBytes, newRawBytesIndex, saResult.getRawBytes().length);
        newRawBytesIndex += saResult.getRawBytes().length;
        if (saResult.getResultMetadata().containsKey(ResultMetadataType.BYTE_SEGMENTS)) {
            @SuppressWarnings("unchecked") Iterable<byte[]> byteSegments = (Iterable<byte[]>) saResult.getResultMetadata().get(ResultMetadataType.BYTE_SEGMENTS);
            for (byte[] segment : byteSegments) {
                System.arraycopy(segment, 0, newByteSegment, byteSegmentIndex, segment.length);
                byteSegmentIndex += segment.length;
            }
        }
    }
    Result newResult = new Result(concatedText.toString(), newRawBytes, NO_POINTS, BarcodeFormat.QR_CODE);
    if (byteSegmentLength > 0) {
        Collection<byte[]> byteSegmentList = new ArrayList<>();
        byteSegmentList.add(newByteSegment);
        newResult.putMetadata(ResultMetadataType.BYTE_SEGMENTS, byteSegmentList);
    }
    newResults.add(newResult);
    return newResults;
}
Also used : ArrayList(java.util.ArrayList) ResultPoint(com.google.zxing.ResultPoint) Result(com.google.zxing.Result) DetectorResult(com.google.zxing.common.DetectorResult) DecoderResult(com.google.zxing.common.DecoderResult)

Example 28 with Result

use of com.google.zxing.Result in project zxing by zxing.

the class DataMatrixReader method decode.

@Override
public Result decode(BinaryBitmap image, Map<DecodeHintType, ?> hints) throws NotFoundException, ChecksumException, FormatException {
    DecoderResult decoderResult;
    ResultPoint[] points;
    if (hints != null && hints.containsKey(DecodeHintType.PURE_BARCODE)) {
        BitMatrix bits = extractPureBits(image.getBlackMatrix());
        decoderResult = decoder.decode(bits);
        points = NO_POINTS;
    } else {
        DetectorResult detectorResult = new Detector(image.getBlackMatrix()).detect();
        decoderResult = decoder.decode(detectorResult.getBits());
        points = detectorResult.getPoints();
    }
    Result result = new Result(decoderResult.getText(), decoderResult.getRawBytes(), points, BarcodeFormat.DATA_MATRIX);
    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 : ResultPoint(com.google.zxing.ResultPoint) Detector(com.google.zxing.datamatrix.detector.Detector) DecoderResult(com.google.zxing.common.DecoderResult) DetectorResult(com.google.zxing.common.DetectorResult) BitMatrix(com.google.zxing.common.BitMatrix) Result(com.google.zxing.Result) DetectorResult(com.google.zxing.common.DetectorResult) DecoderResult(com.google.zxing.common.DecoderResult)

Example 29 with Result

use of com.google.zxing.Result in project zxing by zxing.

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 30 with Result

use of com.google.zxing.Result in project zxing by zxing.

the class CodaBarReader method decodeRow.

@Override
public Result decodeRow(int rowNumber, BitArray row, Map<DecodeHintType, ?> hints) throws NotFoundException {
    Arrays.fill(counters, 0);
    setCounters(row);
    int startOffset = findStartPattern();
    int nextStart = startOffset;
    decodeRowResult.setLength(0);
    do {
        int charOffset = toNarrowWidePattern(nextStart);
        if (charOffset == -1) {
            throw NotFoundException.getNotFoundInstance();
        }
        // Hack: We store the position in the alphabet table into a
        // StringBuilder, so that we can access the decoded patterns in
        // validatePattern. We'll translate to the actual characters later.
        decodeRowResult.append((char) charOffset);
        nextStart += 8;
        // Stop as soon as we see the end character.
        if (decodeRowResult.length() > 1 && arrayContains(STARTEND_ENCODING, ALPHABET[charOffset])) {
            break;
        }
    } while (// no fixed end pattern so keep on reading while data is available
    nextStart < counterLength);
    // Look for whitespace after pattern:
    int trailingWhitespace = counters[nextStart - 1];
    int lastPatternSize = 0;
    for (int i = -8; i < -1; i++) {
        lastPatternSize += counters[nextStart + i];
    }
    // at the end of the row. (I.e. the barcode barely fits.)
    if (nextStart < counterLength && trailingWhitespace < lastPatternSize / 2) {
        throw NotFoundException.getNotFoundInstance();
    }
    validatePattern(startOffset);
    // Translate character table offsets to actual characters.
    for (int i = 0; i < decodeRowResult.length(); i++) {
        decodeRowResult.setCharAt(i, ALPHABET[decodeRowResult.charAt(i)]);
    }
    // Ensure a valid start and end character
    char startchar = decodeRowResult.charAt(0);
    if (!arrayContains(STARTEND_ENCODING, startchar)) {
        throw NotFoundException.getNotFoundInstance();
    }
    char endchar = decodeRowResult.charAt(decodeRowResult.length() - 1);
    if (!arrayContains(STARTEND_ENCODING, endchar)) {
        throw NotFoundException.getNotFoundInstance();
    }
    // remove stop/start characters character and check if a long enough string is contained
    if (decodeRowResult.length() <= MIN_CHARACTER_LENGTH) {
        // Almost surely a false positive ( start + stop + at least 1 character)
        throw NotFoundException.getNotFoundInstance();
    }
    if (hints == null || !hints.containsKey(DecodeHintType.RETURN_CODABAR_START_END)) {
        decodeRowResult.deleteCharAt(decodeRowResult.length() - 1);
        decodeRowResult.deleteCharAt(0);
    }
    int runningCount = 0;
    for (int i = 0; i < startOffset; i++) {
        runningCount += counters[i];
    }
    float left = runningCount;
    for (int i = startOffset; i < nextStart - 1; i++) {
        runningCount += counters[i];
    }
    float right = runningCount;
    return new Result(decodeRowResult.toString(), null, new ResultPoint[] { new ResultPoint(left, rowNumber), new ResultPoint(right, rowNumber) }, BarcodeFormat.CODABAR);
}
Also used : ResultPoint(com.google.zxing.ResultPoint) ResultPoint(com.google.zxing.ResultPoint) Result(com.google.zxing.Result)

Aggregations

Result (com.google.zxing.Result)117 ResultPoint (com.google.zxing.ResultPoint)43 BinaryBitmap (com.google.zxing.BinaryBitmap)37 ReaderException (com.google.zxing.ReaderException)35 HybridBinarizer (com.google.zxing.common.HybridBinarizer)30 Bundle (android.os.Bundle)19 Message (android.os.Message)17 DecoderResult (com.google.zxing.common.DecoderResult)14 ArrayList (java.util.ArrayList)14 MultiFormatReader (com.google.zxing.MultiFormatReader)11 NotFoundException (com.google.zxing.NotFoundException)11 PlanarYUVLuminanceSource (com.google.zxing.PlanarYUVLuminanceSource)11 Cursor (android.database.Cursor)9 LuminanceSource (com.google.zxing.LuminanceSource)9 BitArray (com.google.zxing.common.BitArray)8 DetectorResult (com.google.zxing.common.DetectorResult)8 BufferedImage (java.awt.image.BufferedImage)8 Test (org.junit.Test)8 Bitmap (android.graphics.Bitmap)7 Handler (android.os.Handler)7