Search in sources :

Example 61 with Result

use of com.google.zxing.Result 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)

Example 62 with Result

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

the class Code93Reader method decodeRow.

@Override
public Result decodeRow(int rowNumber, BitArray row, Map<DecodeHintType, ?> hints) throws NotFoundException, ChecksumException, FormatException {
    int[] start = findAsteriskPattern(row);
    // Read off white space    
    int nextStart = row.getNextSet(start[1]);
    int end = row.getSize();
    int[] theCounters = counters;
    Arrays.fill(theCounters, 0);
    StringBuilder result = decodeRowResult;
    result.setLength(0);
    char decodedChar;
    int lastStart;
    do {
        recordPattern(row, nextStart, theCounters);
        int pattern = toPattern(theCounters);
        if (pattern < 0) {
            throw NotFoundException.getNotFoundInstance();
        }
        decodedChar = patternToChar(pattern);
        result.append(decodedChar);
        lastStart = nextStart;
        for (int counter : theCounters) {
            nextStart += counter;
        }
        // Read off white space
        nextStart = row.getNextSet(nextStart);
    } while (decodedChar != '*');
    // remove asterisk
    result.deleteCharAt(result.length() - 1);
    int lastPatternSize = 0;
    for (int counter : theCounters) {
        lastPatternSize += counter;
    }
    // Should be at least one more black module
    if (nextStart == end || !row.get(nextStart)) {
        throw NotFoundException.getNotFoundInstance();
    }
    if (result.length() < 2) {
        // false positive -- need at least 2 checksum digits
        throw NotFoundException.getNotFoundInstance();
    }
    checkChecksums(result);
    // Remove checksum digits
    result.setLength(result.length() - 2);
    String resultString = decodeExtended(result);
    float left = (start[1] + start[0]) / 2.0f;
    float right = lastStart + lastPatternSize / 2.0f;
    return new Result(resultString, null, new ResultPoint[] { new ResultPoint(left, rowNumber), new ResultPoint(right, rowNumber) }, BarcodeFormat.CODE_93);
}
Also used : ResultPoint(com.google.zxing.ResultPoint) ResultPoint(com.google.zxing.ResultPoint) Result(com.google.zxing.Result)

Example 63 with Result

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

the class MultiFormatUPCEANReader method decodeRow.

@Override
public Result decodeRow(int rowNumber, BitArray row, Map<DecodeHintType, ?> hints) throws NotFoundException {
    // Compute this location once and reuse it on multiple implementations
    int[] startGuardPattern = UPCEANReader.findStartGuardPattern(row);
    for (UPCEANReader reader : readers) {
        Result result;
        try {
            result = reader.decodeRow(rowNumber, row, startGuardPattern, hints);
        } catch (ReaderException ignored) {
            continue;
        }
        // Special case: a 12-digit code encoded in UPC-A is identical to a "0"
        // followed by those 12 digits encoded as EAN-13. Each will recognize such a code,
        // UPC-A as a 12-digit string and EAN-13 as a 13-digit string starting with "0".
        // Individually these are correct and their readers will both read such a code
        // and correctly call it EAN-13, or UPC-A, respectively.
        //
        // In this case, if we've been looking for both types, we'd like to call it
        // a UPC-A code. But for efficiency we only run the EAN-13 decoder to also read
        // UPC-A. So we special case it here, and convert an EAN-13 result to a UPC-A
        // result if appropriate.
        //
        // But, don't return UPC-A if UPC-A was not a requested format!
        boolean ean13MayBeUPCA = result.getBarcodeFormat() == BarcodeFormat.EAN_13 && result.getText().charAt(0) == '0';
        @SuppressWarnings("unchecked") Collection<BarcodeFormat> possibleFormats = hints == null ? null : (Collection<BarcodeFormat>) hints.get(DecodeHintType.POSSIBLE_FORMATS);
        boolean canReturnUPCA = possibleFormats == null || possibleFormats.contains(BarcodeFormat.UPC_A);
        if (ean13MayBeUPCA && canReturnUPCA) {
            // Transfer the metdata across
            Result resultUPCA = new Result(result.getText().substring(1), result.getRawBytes(), result.getResultPoints(), BarcodeFormat.UPC_A);
            resultUPCA.putAllMetadata(result.getResultMetadata());
            return resultUPCA;
        }
        return result;
    }
    throw NotFoundException.getNotFoundInstance();
}
Also used : BarcodeFormat(com.google.zxing.BarcodeFormat) Result(com.google.zxing.Result) ReaderException(com.google.zxing.ReaderException)

Example 64 with Result

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

the class ByQuadrantReader method decode.

@Override
public Result decode(BinaryBitmap image, Map<DecodeHintType, ?> hints) throws NotFoundException, ChecksumException, FormatException {
    int width = image.getWidth();
    int height = image.getHeight();
    int halfWidth = width / 2;
    int halfHeight = height / 2;
    try {
        // No need to call makeAbsolute as results will be relative to original top left here
        return delegate.decode(image.crop(0, 0, halfWidth, halfHeight), hints);
    } catch (NotFoundException re) {
    // continue
    }
    try {
        Result result = delegate.decode(image.crop(halfWidth, 0, halfWidth, halfHeight), hints);
        makeAbsolute(result.getResultPoints(), halfWidth, 0);
        return result;
    } catch (NotFoundException re) {
    // continue
    }
    try {
        Result result = delegate.decode(image.crop(0, halfHeight, halfWidth, halfHeight), hints);
        makeAbsolute(result.getResultPoints(), 0, halfHeight);
        return result;
    } catch (NotFoundException re) {
    // continue
    }
    try {
        Result result = delegate.decode(image.crop(halfWidth, halfHeight, halfWidth, halfHeight), hints);
        makeAbsolute(result.getResultPoints(), halfWidth, halfHeight);
        return result;
    } catch (NotFoundException re) {
    // continue
    }
    int quarterWidth = halfWidth / 2;
    int quarterHeight = halfHeight / 2;
    BinaryBitmap center = image.crop(quarterWidth, quarterHeight, halfWidth, halfHeight);
    Result result = delegate.decode(center, hints);
    makeAbsolute(result.getResultPoints(), quarterWidth, quarterHeight);
    return result;
}
Also used : NotFoundException(com.google.zxing.NotFoundException) BinaryBitmap(com.google.zxing.BinaryBitmap) ResultPoint(com.google.zxing.ResultPoint) Result(com.google.zxing.Result)

Example 65 with Result

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

the class GenericMultipleBarcodeReader method doDecodeMultiple.

private void doDecodeMultiple(BinaryBitmap image, Map<DecodeHintType, ?> hints, List<Result> results, int xOffset, int yOffset, int currentDepth) {
    if (currentDepth > MAX_DEPTH) {
        return;
    }
    Result result;
    try {
        result = delegate.decode(image, hints);
    } catch (ReaderException ignored) {
        return;
    }
    boolean alreadyFound = false;
    for (Result existingResult : results) {
        if (existingResult.getText().equals(result.getText())) {
            alreadyFound = true;
            break;
        }
    }
    if (!alreadyFound) {
        results.add(translateResultPoints(result, xOffset, yOffset));
    }
    ResultPoint[] resultPoints = result.getResultPoints();
    if (resultPoints == null || resultPoints.length == 0) {
        return;
    }
    int width = image.getWidth();
    int height = image.getHeight();
    float minX = width;
    float minY = height;
    float maxX = 0.0f;
    float maxY = 0.0f;
    for (ResultPoint point : resultPoints) {
        if (point == null) {
            continue;
        }
        float x = point.getX();
        float y = point.getY();
        if (x < minX) {
            minX = x;
        }
        if (y < minY) {
            minY = y;
        }
        if (x > maxX) {
            maxX = x;
        }
        if (y > maxY) {
            maxY = y;
        }
    }
    // Decode left of barcode
    if (minX > MIN_DIMENSION_TO_RECUR) {
        doDecodeMultiple(image.crop(0, 0, (int) minX, height), hints, results, xOffset, yOffset, currentDepth + 1);
    }
    // Decode above barcode
    if (minY > MIN_DIMENSION_TO_RECUR) {
        doDecodeMultiple(image.crop(0, 0, width, (int) minY), hints, results, xOffset, yOffset, currentDepth + 1);
    }
    // Decode right of barcode
    if (maxX < width - MIN_DIMENSION_TO_RECUR) {
        doDecodeMultiple(image.crop((int) maxX, 0, width - (int) maxX, height), hints, results, xOffset + (int) maxX, yOffset, currentDepth + 1);
    }
    // Decode below barcode
    if (maxY < height - MIN_DIMENSION_TO_RECUR) {
        doDecodeMultiple(image.crop(0, (int) maxY, width, height - (int) maxY), hints, results, xOffset, yOffset + (int) maxY, currentDepth + 1);
    }
}
Also used : ResultPoint(com.google.zxing.ResultPoint) ResultPoint(com.google.zxing.ResultPoint) Result(com.google.zxing.Result) ReaderException(com.google.zxing.ReaderException)

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