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;
}
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();
}
}
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();
}
}
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);
}
}
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;
}
Aggregations