Search in sources :

Example 11 with ChecksumException

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

the class ScanQRCodeFromFile method scanQRImage.

public static String scanQRImage(Context context, Uri uri) {
    // Check if external storage is accessible
    if (!Tools.isExternalStorageReadable()) {
        Toast.makeText(context, R.string.backup_toast_storage_not_accessible, Toast.LENGTH_LONG).show();
        return null;
    }
    // Get image in bytes
    byte[] imageInBytes;
    try {
        imageInBytes = StorageAccessHelper.loadFile(context, uri);
    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(context, R.string.toast_file_load_error, Toast.LENGTH_LONG).show();
        return null;
    }
    Bitmap bMap = BitmapFactory.decodeByteArray(imageInBytes, 0, imageInBytes.length);
    String contents = null;
    int[] intArray = new int[bMap.getWidth() * bMap.getHeight()];
    bMap.getPixels(intArray, 0, bMap.getWidth(), 0, 0, bMap.getWidth(), bMap.getHeight());
    LuminanceSource source = new RGBLuminanceSource(bMap.getWidth(), bMap.getHeight(), intArray);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    Result result = null;
    QRCodeReader reader = new QRCodeReader();
    ReaderException savedException = null;
    try {
        // Try finding QR code
        result = reader.decode(bitmap, HINTS);
        contents = result.getText();
    } catch (ReaderException re) {
        savedException = re;
    }
    if (contents == null) {
        try {
            // Try finding QR code really hard
            result = reader.decode(bitmap, HINTS_HARDER);
            contents = result.getText();
        } catch (ReaderException re) {
            savedException = re;
        }
    }
    if (contents == null) {
        try {
            throw savedException == null ? NotFoundException.getNotFoundInstance() : savedException;
        } catch (ChecksumException e) {
            e.printStackTrace();
            Toast.makeText(context, R.string.toast_qr_checksum_exception, Toast.LENGTH_LONG).show();
        } catch (FormatException e) {
            e.printStackTrace();
            Toast.makeText(context, R.string.toast_qr_format_error, Toast.LENGTH_LONG).show();
        } catch (ReaderException e) {
            // Including NotFoundException
            e.printStackTrace();
            Toast.makeText(context, R.string.toast_qr_error, Toast.LENGTH_LONG).show();
        }
    }
    // Return QR code (if found)
    return contents;
}
Also used : QRCodeReader(com.google.zxing.qrcode.QRCodeReader) ChecksumException(com.google.zxing.ChecksumException) IOException(java.io.IOException) HybridBinarizer(com.google.zxing.common.HybridBinarizer) FormatException(com.google.zxing.FormatException) Result(com.google.zxing.Result) ReaderException(com.google.zxing.ReaderException) Bitmap(android.graphics.Bitmap) BinaryBitmap(com.google.zxing.BinaryBitmap) LuminanceSource(com.google.zxing.LuminanceSource) RGBLuminanceSource(com.google.zxing.RGBLuminanceSource) BinaryBitmap(com.google.zxing.BinaryBitmap) RGBLuminanceSource(com.google.zxing.RGBLuminanceSource)

Example 12 with ChecksumException

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

the class QRCodeUtils method decode.

private String decode(Bitmap bitmap) throws InvalidException, NotFoundException {
    Map<DecodeHintType, Object> tmpHintsMap = new EnumMap<>(DecodeHintType.class);
    tmpHintsMap.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
    tmpHintsMap.put(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE);
    tmpHintsMap.put(DecodeHintType.PURE_BARCODE, Boolean.FALSE);
    try {
        QRCodeMultiReader reader = new QRCodeMultiReader();
        Result result = reader.decode(getBinaryBitmap(bitmap), tmpHintsMap);
        return CompressionUtils.decompress(result.getText());
    } catch (DataFormatException | IOException | IllegalArgumentException e) {
        throw new InvalidException();
    } catch (FormatException | com.google.zxing.NotFoundException | ChecksumException e) {
        throw new NotFoundException();
    }
}
Also used : QRCodeMultiReader(com.google.zxing.multi.qrcode.QRCodeMultiReader) DecodeHintType(com.google.zxing.DecodeHintType) ChecksumException(com.google.zxing.ChecksumException) IOException(java.io.IOException) DataFormatException(java.util.zip.DataFormatException) FormatException(com.google.zxing.FormatException) Result(com.google.zxing.Result) DataFormatException(java.util.zip.DataFormatException) EnumMap(java.util.EnumMap)

Example 13 with ChecksumException

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

the class QRCodeUtils method decode.

private String decode(Bitmap bitmap) throws InvalidException, NotFoundException {
    Map<DecodeHintType, Object> tmpHintsMap = new EnumMap<>(DecodeHintType.class);
    tmpHintsMap.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
    tmpHintsMap.put(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE);
    tmpHintsMap.put(DecodeHintType.PURE_BARCODE, Boolean.FALSE);
    try {
        QRCodeMultiReader reader = new QRCodeMultiReader();
        Result result = reader.decode(getBinaryBitmap(bitmap), tmpHintsMap);
        return CompressionUtils.decompress(result.getText());
    } catch (DataFormatException | IOException | IllegalArgumentException e) {
        throw new InvalidException();
    } catch (FormatException | com.google.zxing.NotFoundException | ChecksumException e) {
        throw new NotFoundException();
    }
}
Also used : QRCodeMultiReader(com.google.zxing.multi.qrcode.QRCodeMultiReader) DecodeHintType(com.google.zxing.DecodeHintType) ChecksumException(com.google.zxing.ChecksumException) IOException(java.io.IOException) DataFormatException(java.util.zip.DataFormatException) FormatException(com.google.zxing.FormatException) Result(com.google.zxing.Result) DataFormatException(java.util.zip.DataFormatException) EnumMap(java.util.EnumMap)

Example 14 with ChecksumException

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

the class MainActivity method decodeQrCodeImage.

private void decodeQrCodeImage(Uri inputFile) {
    Bitmap bitmap;
    try {
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        try (InputStream inputStream = getContentResolver().openInputStream(inputFile)) {
            bitmap = BitmapFactory.decodeStream(inputStream, null, bmOptions);
            bitmap = BitmapHelper.resize(bitmap, QrCodeAnalyzer.RESOLUTION.getWidth(), QrCodeAnalyzer.RESOLUTION.getHeight());
        }
        int[] intArray = new int[bitmap.getWidth() * bitmap.getHeight()];
        bitmap.getPixels(intArray, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
        LuminanceSource source = new RGBLuminanceSource(bitmap.getWidth(), bitmap.getHeight(), intArray);
        BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source));
        Reader reader = new QRCodeReader();
        Result result = reader.decode(binaryBitmap);
        GoogleAuthInfo info = GoogleAuthInfo.parseUri(result.getText());
        VaultEntry entry = new VaultEntry(info);
        startEditEntryActivityForNew(CODE_ADD_ENTRY, entry);
    } catch (NotFoundException | IOException | ChecksumException | FormatException | GoogleAuthInfoException e) {
        e.printStackTrace();
        Dialogs.showErrorDialog(this, R.string.unable_to_read_qrcode, e);
    }
}
Also used : QRCodeReader(com.google.zxing.qrcode.QRCodeReader) GoogleAuthInfo(com.beemdevelopment.aegis.otp.GoogleAuthInfo) InputStream(java.io.InputStream) ChecksumException(com.google.zxing.ChecksumException) QRCodeReader(com.google.zxing.qrcode.QRCodeReader) Reader(com.google.zxing.Reader) NotFoundException(com.google.zxing.NotFoundException) IOException(java.io.IOException) HybridBinarizer(com.google.zxing.common.HybridBinarizer) FormatException(com.google.zxing.FormatException) Result(com.google.zxing.Result) Bitmap(android.graphics.Bitmap) BinaryBitmap(com.google.zxing.BinaryBitmap) LuminanceSource(com.google.zxing.LuminanceSource) RGBLuminanceSource(com.google.zxing.RGBLuminanceSource) VaultEntry(com.beemdevelopment.aegis.vault.VaultEntry) GoogleAuthInfoException(com.beemdevelopment.aegis.otp.GoogleAuthInfoException) BitmapFactory(android.graphics.BitmapFactory) BinaryBitmap(com.google.zxing.BinaryBitmap) RGBLuminanceSource(com.google.zxing.RGBLuminanceSource)

Example 15 with ChecksumException

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

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)

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