Search in sources :

Example 21 with ChecksumException

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

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

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

the class ShowQRCodeFragment method onActivityResult.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if (result != null) {
        if (result.getContents() == null) {
            // request was canceled...
            Timber.i("QR code scanning cancelled");
        } else {
            try {
                applySettings(CompressionUtils.decompress(result.getContents()));
            } catch (IOException | DataFormatException e) {
                Timber.e(e);
            }
            return;
        }
    }
    if (requestCode == SELECT_PHOTO) {
        if (resultCode == Activity.RESULT_OK) {
            try {
                final Uri imageUri = data.getData();
                final InputStream imageStream = getActivity().getContentResolver().openInputStream(imageUri);
                final Bitmap bitmap = BitmapFactory.decodeStream(imageStream);
                String response = QRCodeUtils.decodeFromBitmap(bitmap);
                if (response != null) {
                    applySettings(response);
                }
            } catch (FormatException | NotFoundException | ChecksumException e) {
                Timber.i(e);
                ToastUtils.showLongToast("QR Code not found in the selected image");
            } catch (IOException e) {
                Timber.e(e);
                ToastUtils.showLongToast("Unable to read the settings");
            } catch (DataFormatException e) {
                Timber.e(e);
                ToastUtils.showShortToast(getString(R.string.invalid_qrcode));
            }
        } else {
            Timber.i("Choosing QR code from sdcard cancelled");
        }
    }
}
Also used : Bitmap(android.graphics.Bitmap) IntentResult(com.google.zxing.integration.android.IntentResult) DataFormatException(java.util.zip.DataFormatException) InputStream(java.io.InputStream) ChecksumException(com.google.zxing.ChecksumException) NotFoundException(com.google.zxing.NotFoundException) IOException(java.io.IOException) Uri(android.net.Uri) FormatException(com.google.zxing.FormatException) DataFormatException(java.util.zip.DataFormatException)

Example 23 with ChecksumException

use of com.google.zxing.ChecksumException in project incubator-weex by apache.

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

use of com.google.zxing.ChecksumException in project Signal-Android by signalapp.

the class ScanningThread method getScannedData.

@Nullable
private String getScannedData(byte[] data, int width, int height, int orientation) {
    try {
        if (orientation == Configuration.ORIENTATION_PORTRAIT) {
            byte[] rotatedData = new byte[data.length];
            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++) {
                    rotatedData[x * height + height - y - 1] = data[x + y * width];
                }
            }
            int tmp = width;
            width = height;
            height = tmp;
            data = rotatedData;
        }
        PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(data, width, height, 0, 0, width, height, false);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        Result result = reader.decode(bitmap, hints);
        if (result != null)
            return result.getText();
    } catch (NullPointerException | ChecksumException | FormatException e) {
        Log.w(TAG, e);
    } catch (NotFoundException e) {
    // Thanks ZXing...
    }
    return null;
}
Also used : PlanarYUVLuminanceSource(com.google.zxing.PlanarYUVLuminanceSource) ChecksumException(com.google.zxing.ChecksumException) NotFoundException(com.google.zxing.NotFoundException) BinaryBitmap(com.google.zxing.BinaryBitmap) HybridBinarizer(com.google.zxing.common.HybridBinarizer) FormatException(com.google.zxing.FormatException) Result(com.google.zxing.Result) Nullable(androidx.annotation.Nullable)

Example 25 with ChecksumException

use of com.google.zxing.ChecksumException in project summer-android by cn-cerc.

the class FrmScanBarcode method scanningImage.

// @Override
// protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// super.onActivityResult(requestCode, resultCode, data);
// if (resultCode == RESULT_OK) {
// switch (requestCode) {
// case REQUEST_CODE:
// //获取选中图片的路径
// Cursor cursor = getContentResolver().query(data.getData(), null, null, null, null);
// if (cursor.moveToFirst()) {
// photo_path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
// }
// cursor.close();
// 
// mProgress = new ProgressDialog(FrmScanBarcode.this);
// mProgress.setMessage("正在扫描...");
// mProgress.setCancelable(false);
// mProgress.show();
// 
// new Thread(new Runnable() {
// @Override
// public void run() {
// Result result = scanningImage(photo_path);
// if (result != null) {
// Message m = mHandler.obtainMessage();
// m.what = PARSE_BARCODE_SUC;
// m.obj = result.getText();
// mHandler.sendMessage(m);
// } else {
// Message m = mHandler.obtainMessage();
// m.what = PARSE_BARCODE_FAIL;
// m.obj = "Scan failed!";
// mHandler.sendMessage(m);
// }
// }
// }).start();
// 
// break;
// 
// }
// }
// }
/**
 * 识别二维码图片中的数据
 *
 * @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(cn.cerc.summer.android.parts.barcode.zxing.decoding.RGBLuminanceSource)

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