Search in sources :

Example 16 with ChecksumException

use of com.google.zxing.ChecksumException in project WeexErosFramework by bmfe.

the class EventImage method scan.

public void scan(String json, Context context, JSCallback jsCallback) {
    ScanImageBean bean = ManagerFactory.getManagerService(ParseManager.class).parseObject(json, ScanImageBean.class);
    Bitmap bitmap;
    String path = bean.path;
    bitmap = BitmapFactory.decodeFile(path);
    // 获取bitmap的宽高,像素矩阵
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int[] pixels = new int[width * height];
    bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
    RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);
    BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source));
    Reader reader = new MultiFormatReader();
    String result = "";
    try {
        result = reader.decode(binaryBitmap).getText();
        JsPoster.postSuccess(result, jsCallback);
        return;
    } catch (NotFoundException e) {
        e.printStackTrace();
    } catch (ChecksumException e) {
        e.printStackTrace();
    } catch (FormatException e) {
        e.printStackTrace();
    }
    JsPoster.postFailed(jsCallback);
}
Also used : MultiFormatReader(com.google.zxing.MultiFormatReader) ChecksumException(com.google.zxing.ChecksumException) MultiFormatReader(com.google.zxing.MultiFormatReader) Reader(com.google.zxing.Reader) NotFoundException(com.google.zxing.NotFoundException) ParseManager(com.eros.framework.manager.impl.ParseManager) HybridBinarizer(com.google.zxing.common.HybridBinarizer) FormatException(com.google.zxing.FormatException) Bitmap(android.graphics.Bitmap) BinaryBitmap(com.google.zxing.BinaryBitmap) ScanImageBean(com.eros.framework.model.ScanImageBean) BinaryBitmap(com.google.zxing.BinaryBitmap) RGBLuminanceSource(com.google.zxing.RGBLuminanceSource)

Example 17 with ChecksumException

use of com.google.zxing.ChecksumException in project QRCode by 5peak2me.

the class MipcaActivityCapture method scanningImage.

/**
 * 扫描二维码图片的方法
 * @param path
 * @return
 */
public Result scanningImage(String path) {
    if (TextUtils.isEmpty(path)) {
        return null;
    }
    Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();
    // 设置二维码内容的编码
    hints.put(DecodeHintType.CHARACTER_SET, "UTF8");
    BitmapFactory.Options options = new BitmapFactory.Options();
    // 先获取原大小
    options.inJustDecodeBounds = true;
    scanBitmap = BitmapFactory.decodeFile(path, options);
    // 获取新的大小
    options.inJustDecodeBounds = false;
    int sampleSize = (int) (options.outHeight / (float) 200);
    if (sampleSize <= 0)
        sampleSize = 1;
    options.inSampleSize = sampleSize;
    scanBitmap = BitmapFactory.decodeFile(path, options);
    RGBLuminanceSource source = new RGBLuminanceSource(scanBitmap);
    BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
    QRCodeReader reader = new QRCodeReader();
    try {
        return reader.decode(bitmap1, hints);
    } catch (NotFoundException e) {
        e.printStackTrace();
    } catch (ChecksumException e) {
        e.printStackTrace();
    } catch (FormatException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : QRCodeReader(com.google.zxing.qrcode.QRCodeReader) DecodeHintType(com.google.zxing.DecodeHintType) Hashtable(java.util.Hashtable) ChecksumException(com.google.zxing.ChecksumException) NotFoundException(com.google.zxing.NotFoundException) HybridBinarizer(com.google.zxing.common.HybridBinarizer) FormatException(com.google.zxing.FormatException) BitmapFactory(android.graphics.BitmapFactory) BinaryBitmap(com.google.zxing.BinaryBitmap) RGBLuminanceSource(com.google.zxing.zxing.image.RGBLuminanceSource)

Example 18 with ChecksumException

use of com.google.zxing.ChecksumException in project android-zxing by PearceXu.

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 19 with ChecksumException

use of com.google.zxing.ChecksumException in project android-zxing by PearceXu.

the class PDF417ScanningDecoder method createDecoderResultFromAmbiguousValues.

/**
 * This method deals with the fact, that the decoding process doesn't always yield a single most likely value. The
 * current error correction implementation doesn't deal with erasures very well, so it's better to provide a value
 * for these ambiguous codewords instead of treating it as an erasure. The problem is that we don't know which of
 * the ambiguous values to choose. We try decode using the first value, and if that fails, we use another of the
 * ambiguous values and try to decode again. This usually only happens on very hard to read and decode barcodes,
 * so decoding the normal barcodes is not affected by this.
 *
 * @param erasureArray contains the indexes of erasures
 * @param ambiguousIndexes array with the indexes that have more than one most likely value
 * @param ambiguousIndexValues two dimensional array that contains the ambiguous values. The first dimension must
 * be the same length as the ambiguousIndexes array
 */
private static DecoderResult createDecoderResultFromAmbiguousValues(int ecLevel, int[] codewords, int[] erasureArray, int[] ambiguousIndexes, int[][] ambiguousIndexValues) throws FormatException, ChecksumException {
    int[] ambiguousIndexCount = new int[ambiguousIndexes.length];
    int tries = 100;
    while (tries-- > 0) {
        for (int i = 0; i < ambiguousIndexCount.length; i++) {
            codewords[ambiguousIndexes[i]] = ambiguousIndexValues[i][ambiguousIndexCount[i]];
        }
        try {
            return decodeCodewords(codewords, ecLevel, erasureArray);
        } catch (ChecksumException ignored) {
        // 
        }
        if (ambiguousIndexCount.length == 0) {
            throw ChecksumException.getChecksumInstance();
        }
        for (int i = 0; i < ambiguousIndexCount.length; i++) {
            if (ambiguousIndexCount[i] < ambiguousIndexValues[i].length - 1) {
                ambiguousIndexCount[i]++;
                break;
            } else {
                ambiguousIndexCount[i] = 0;
                if (i == ambiguousIndexCount.length - 1) {
                    throw ChecksumException.getChecksumInstance();
                }
            }
        }
    }
    throw ChecksumException.getChecksumInstance();
}
Also used : ChecksumException(com.google.zxing.ChecksumException) ResultPoint(com.google.zxing.ResultPoint)

Example 20 with ChecksumException

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

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)

Aggregations

ChecksumException (com.google.zxing.ChecksumException)37 FormatException (com.google.zxing.FormatException)31 BinaryBitmap (com.google.zxing.BinaryBitmap)22 NotFoundException (com.google.zxing.NotFoundException)22 HybridBinarizer (com.google.zxing.common.HybridBinarizer)19 RGBLuminanceSource (com.google.zxing.RGBLuminanceSource)14 Result (com.google.zxing.Result)14 QRCodeReader (com.google.zxing.qrcode.QRCodeReader)14 DecodeHintType (com.google.zxing.DecodeHintType)13 Bitmap (android.graphics.Bitmap)11 Hashtable (java.util.Hashtable)10 BitmapFactory (android.graphics.BitmapFactory)8 IOException (java.io.IOException)7 LuminanceSource (com.google.zxing.LuminanceSource)6 Reader (com.google.zxing.Reader)6 DecoderResult (com.google.zxing.common.DecoderResult)6 MultiFormatReader (com.google.zxing.MultiFormatReader)5 ResultPoint (com.google.zxing.ResultPoint)4 GlobalHistogramBinarizer (com.google.zxing.common.GlobalHistogramBinarizer)4 Nullable (androidx.annotation.Nullable)3