Search in sources :

Example 16 with BarcodeFormat

use of com.google.zxing.BarcodeFormat in project weex-example by KalicyZhou.

the class UPCEANReader method decodeRow.

/**
   * <p>Like {@link #decodeRow(int, BitArray, Map)}, but
   * allows caller to inform method about where the UPC/EAN start pattern is
   * found. This allows this to be computed once and reused across many implementations.</p>
   *
   * @param rowNumber row index into the image
   * @param row encoding of the row of the barcode image
   * @param startGuardRange start/end column where the opening start pattern was found
   * @param hints optional hints that influence decoding
   * @return {@link Result} encapsulating the result of decoding a barcode in the row
   * @throws NotFoundException if no potential barcode is found
   * @throws ChecksumException if a potential barcode is found but does not pass its checksum
   * @throws FormatException if a potential barcode is found but format is invalid
   */
public Result decodeRow(int rowNumber, BitArray row, int[] startGuardRange, Map<DecodeHintType, ?> hints) throws NotFoundException, ChecksumException, FormatException {
    ResultPointCallback resultPointCallback = hints == null ? null : (ResultPointCallback) hints.get(DecodeHintType.NEED_RESULT_POINT_CALLBACK);
    if (resultPointCallback != null) {
        resultPointCallback.foundPossibleResultPoint(new ResultPoint((startGuardRange[0] + startGuardRange[1]) / 2.0f, rowNumber));
    }
    StringBuilder result = decodeRowStringBuffer;
    result.setLength(0);
    int endStart = decodeMiddle(row, startGuardRange, result);
    if (resultPointCallback != null) {
        resultPointCallback.foundPossibleResultPoint(new ResultPoint(endStart, rowNumber));
    }
    int[] endRange = decodeEnd(row, endStart);
    if (resultPointCallback != null) {
        resultPointCallback.foundPossibleResultPoint(new ResultPoint((endRange[0] + endRange[1]) / 2.0f, rowNumber));
    }
    // Make sure there is a quiet zone at least as big as the end pattern after the barcode. The
    // spec might want more whitespace, but in practice this is the maximum we can count on.
    int end = endRange[1];
    int quietEnd = end + (end - endRange[0]);
    if (quietEnd >= row.getSize() || !row.isRange(end, quietEnd, false)) {
        throw NotFoundException.getNotFoundInstance();
    }
    String resultString = result.toString();
    // UPC/EAN should never be less than 8 chars anyway
    if (resultString.length() < 8) {
        throw FormatException.getFormatInstance();
    }
    if (!checkChecksum(resultString)) {
        throw ChecksumException.getChecksumInstance();
    }
    float left = (float) (startGuardRange[1] + startGuardRange[0]) / 2.0f;
    float right = (float) (endRange[1] + endRange[0]) / 2.0f;
    BarcodeFormat format = getBarcodeFormat();
    Result decodeResult = new Result(resultString, // no natural byte representation for these barcodes
    null, new ResultPoint[] { new ResultPoint(left, (float) rowNumber), new ResultPoint(right, (float) rowNumber) }, format);
    int extensionLength = 0;
    try {
        Result extensionResult = extensionReader.decodeRow(rowNumber, row, endRange[1]);
        decodeResult.putMetadata(ResultMetadataType.UPC_EAN_EXTENSION, extensionResult.getText());
        decodeResult.putAllMetadata(extensionResult.getResultMetadata());
        decodeResult.addResultPoints(extensionResult.getResultPoints());
        extensionLength = extensionResult.getText().length();
    } catch (ReaderException re) {
    // continue
    }
    int[] allowedExtensions = hints == null ? null : (int[]) hints.get(DecodeHintType.ALLOWED_EAN_EXTENSIONS);
    if (allowedExtensions != null) {
        boolean valid = false;
        for (int length : allowedExtensions) {
            if (extensionLength == length) {
                valid = true;
                break;
            }
        }
        if (!valid) {
            throw NotFoundException.getNotFoundInstance();
        }
    }
    if (format == BarcodeFormat.EAN_13 || format == BarcodeFormat.UPC_A) {
        String countryID = eanManSupport.lookupCountryIdentifier(resultString);
        if (countryID != null) {
            decodeResult.putMetadata(ResultMetadataType.POSSIBLE_COUNTRY, countryID);
        }
    }
    return decodeResult;
}
Also used : ResultPointCallback(com.google.zxing.ResultPointCallback) ResultPoint(com.google.zxing.ResultPoint) BarcodeFormat(com.google.zxing.BarcodeFormat) ResultPoint(com.google.zxing.ResultPoint) Result(com.google.zxing.Result) ReaderException(com.google.zxing.ReaderException)

Example 17 with BarcodeFormat

use of com.google.zxing.BarcodeFormat in project weex-example by KalicyZhou.

the class ProductResultParser method parse.

// Treat all UPC and EAN variants as UPCs, in the sense that they are all product barcodes.
@Override
public ProductParsedResult parse(Result result) {
    BarcodeFormat format = result.getBarcodeFormat();
    if (!(format == BarcodeFormat.UPC_A || format == BarcodeFormat.UPC_E || format == BarcodeFormat.EAN_8 || format == BarcodeFormat.EAN_13)) {
        return null;
    }
    String rawText = getMassagedText(result);
    if (!isStringOfDigits(rawText, rawText.length())) {
        return null;
    }
    // Not actually checking the checksum again here    
    String normalizedProductID;
    // Expand UPC-E for purposes of searching
    if (format == BarcodeFormat.UPC_E && rawText.length() == 8) {
        normalizedProductID = UPCEReader.convertUPCEtoUPCA(rawText);
    } else {
        normalizedProductID = rawText;
    }
    return new ProductParsedResult(rawText, normalizedProductID);
}
Also used : BarcodeFormat(com.google.zxing.BarcodeFormat)

Aggregations

BarcodeFormat (com.google.zxing.BarcodeFormat)17 Result (com.google.zxing.Result)5 ResultPoint (com.google.zxing.ResultPoint)5 ReaderException (com.google.zxing.ReaderException)4 DecodeHintType (com.google.zxing.DecodeHintType)3 Paint (android.graphics.Paint)2 MultiFormatReader (com.google.zxing.MultiFormatReader)2 ResultPointCallback (com.google.zxing.ResultPointCallback)2 HashMap (java.util.HashMap)2 Vector (java.util.Vector)2 AlertDialog (android.app.AlertDialog)1 DialogInterface (android.content.DialogInterface)1 Bitmap (android.graphics.Bitmap)1 BitmapFactory (android.graphics.BitmapFactory)1 BinaryBitmap (com.google.zxing.BinaryBitmap)1 WriterException (com.google.zxing.WriterException)1 HybridBinarizer (com.google.zxing.common.HybridBinarizer)1 CameraSettings (com.journeyapps.barcodescanner.camera.CameraSettings)1 BarcodeString (com.mwaysolutions.barcode.constants.BarcodeString)1 BitmapLuminanceSource (com.uuzuche.lib_zxing.camera.BitmapLuminanceSource)1