use of com.google.zxing.ChecksumException in project SmartMesh_Android by SmartMeshFoundation.
the class CaptureActivity method scanningImage.
/**
* Scan the qr code image method
*/
public Result scanningImage(String path) {
if (TextUtils.isEmpty(path)) {
return null;
}
Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();
// Set the qr code coding content
hints.put(DecodeHintType.CHARACTER_SET, "UTF8");
BitmapFactory.Options options = new BitmapFactory.Options();
// To obtain the original size
options.inJustDecodeBounds = true;
scanBitmap = BitmapFactory.decodeFile(path, options);
// To get the new size
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;
}
use of com.google.zxing.ChecksumException in project guard-android by Authing.
the class CaptureActivity method scanningImage.
/**
* 扫描二维码图片的方法
* @param uri
* @return
*/
public Result scanningImage(Uri uri) {
if (uri == null) {
return null;
}
Hashtable<DecodeHintType, String> hints = new Hashtable<>();
// 设置二维码内容的编码
hints.put(DecodeHintType.CHARACTER_SET, "UTF8");
scanBitmap = BitmapUtil.decodeUri(this, uri, 500, 500);
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;
}
use of com.google.zxing.ChecksumException in project ofbiz-framework by apache.
the class QRCodeServices method generateQRCodeImage.
/**
* Streams QR Code to the result.
*/
public static Map<String, Object> generateQRCodeImage(DispatchContext ctx, Map<String, Object> context) {
Locale locale = (Locale) context.get("locale");
String message = (String) context.get("message");
Integer width = (Integer) context.get("width");
Integer height = (Integer) context.get("height");
String format = (String) context.get("format");
String encoding = (String) context.get("encoding");
Boolean verifyOutput = (Boolean) context.get("verifyOutput");
String logoImage = (String) context.get("logoImage");
Integer logoImageMaxWidth = (Integer) context.get("logoImageMaxWidth");
Integer logoImageMaxHeight = (Integer) context.get("logoImageMaxHeight");
Delegator delegator = ctx.getDelegator();
if (UtilValidate.isEmpty(message)) {
return ServiceUtil.returnError(UtilProperties.getMessage("QRCodeUiLabels", "ParameterCannotEmpty", new Object[] { "message" }, locale));
}
if (width == null) {
width = Integer.parseInt(EntityUtilProperties.getPropertyValue("qrcode", "qrcode.default.width", "200", delegator));
}
if (width < MIN_SIZE || width > MAX_SIZE) {
return ServiceUtil.returnError(UtilProperties.getMessage("QRCodeUiLabels", "SizeOutOfBorderError", new Object[] { "width", String.valueOf(width), String.valueOf(MIN_SIZE), String.valueOf(MAX_SIZE) }, locale));
}
if (height == null) {
height = Integer.parseInt(EntityUtilProperties.getPropertyValue("qrcode", "qrcode.default.height", "200", delegator));
}
if (height < MIN_SIZE || height > MAX_SIZE) {
return ServiceUtil.returnError(UtilProperties.getMessage("QRCodeUiLabels", "SizeOutOfBorderError", new Object[] { "height", String.valueOf(height), String.valueOf(MIN_SIZE), String.valueOf(MAX_SIZE) }, locale));
}
if (UtilValidate.isEmpty(format)) {
format = EntityUtilProperties.getPropertyValue("qrcode", "qrcode.default.format", "jpg", delegator);
}
String qrCodeFormatSupported = EntityUtilProperties.getPropertyValue("qrcode", "qrcode.format.supported", "jpg|png|bmp", delegator);
String[] formatNames = StringUtil.split(qrCodeFormatSupported, '|');
List<String> formatsSupported = Arrays.asList(formatNames);
if (!formatsSupported.contains(format)) {
return ServiceUtil.returnError(UtilProperties.getMessage("QRCodeUiLabels", "ErrorFormatNotSupported", new Object[] { format }, locale));
}
Map<EncodeHintType, Object> encodeHints = null;
if (UtilValidate.isNotEmpty(encoding)) {
encodeHints = new EnumMap<>(EncodeHintType.class);
encodeHints.put(EncodeHintType.CHARACTER_SET, encoding);
}
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(message, BarcodeFormat.QR_CODE, width, height, encodeHints);
BufferedImage bufferedImage = toBufferedImage(bitMatrix, format, locale);
BufferedImage logoBufferedImage = null;
if (UtilValidate.isNotEmpty(logoImage)) {
Map<String, Object> logoImageResult;
try {
logoImageResult = ImageTransform.getBufferedImage(FileUtil.getFile(logoImage).getAbsolutePath(), locale);
logoBufferedImage = (BufferedImage) logoImageResult.get("bufferedImage");
} catch (IllegalArgumentException | IOException e) {
Debug.logError(e, MODULE);
}
}
if (UtilValidate.isEmpty(logoBufferedImage)) {
String qrCodeDefaultLogoImage = EntityUtilProperties.getPropertyValue("qrcode", "qrcode.default.logoimage", delegator);
BufferedImage defaultLogoImage = null;
if (UtilValidate.isNotEmpty(qrCodeDefaultLogoImage)) {
try {
Map<String, Object> logoImageResult = ImageTransform.getBufferedImage(FileUtil.getFile(qrCodeDefaultLogoImage).getAbsolutePath(), locale);
defaultLogoImage = (BufferedImage) logoImageResult.get("bufferedImage");
if (UtilValidate.isEmpty(defaultLogoImage)) {
Debug.logError("Your logo image file(" + qrCodeDefaultLogoImage + ") cannot be read by javax.imageio.ImageIO. Please use png, jpeg formats instead of ico and etc.", MODULE);
}
} catch (IllegalArgumentException | IOException e) {
defaultLogoImage = null;
}
}
logoBufferedImage = defaultLogoImage;
}
BufferedImage newBufferedImage = null;
if (UtilValidate.isNotEmpty(logoBufferedImage)) {
if (UtilValidate.isNotEmpty(logoImageMaxWidth) && UtilValidate.isNotEmpty(logoImageMaxHeight) && (logoBufferedImage.getWidth() > logoImageMaxWidth || logoBufferedImage.getHeight() > logoImageMaxHeight)) {
Map<String, String> typeMap = new HashMap<>();
typeMap.put("width", logoImageMaxWidth.toString());
typeMap.put("height", logoImageMaxHeight.toString());
Map<String, Map<String, String>> dimensionMap = new HashMap<>();
dimensionMap.put("QRCode", typeMap);
Map<String, Object> logoImageResult = ImageTransform.scaleImage(logoBufferedImage, logoBufferedImage.getWidth(), logoBufferedImage.getHeight(), dimensionMap, "QRCode", locale);
logoBufferedImage = (BufferedImage) logoImageResult.get("bufferedImage");
}
BitMatrix newBitMatrix = bitMatrix.clone();
newBufferedImage = toBufferedImage(newBitMatrix, format, locale);
Graphics2D graphics = newBufferedImage.createGraphics();
graphics.drawImage(logoBufferedImage, new AffineTransformOp(AffineTransform.getTranslateInstance(1, 1), null), (newBufferedImage.getWidth() - logoBufferedImage.getWidth()) / 2, (newBufferedImage.getHeight() - logoBufferedImage.getHeight()) / 2);
graphics.dispose();
}
if (UtilValidate.isNotEmpty(verifyOutput) && verifyOutput) {
Decoder decoder = new Decoder();
Map<DecodeHintType, Object> decodeHints = new EnumMap<>(DecodeHintType.class);
decodeHints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
if (UtilValidate.isNotEmpty(encoding)) {
decodeHints.put(DecodeHintType.CHARACTER_SET, encoding);
}
DetectorResult detectorResult = null;
if (UtilValidate.isNotEmpty(newBufferedImage)) {
BitMatrix newBitMatrix = createMatrixFromImage(newBufferedImage);
DecoderResult result = null;
try {
detectorResult = new Detector(newBitMatrix).detect(decodeHints);
result = decoder.decode(detectorResult.getBits(), decodeHints);
} catch (ChecksumException | FormatException | NotFoundException e) {
Debug.logError(e, MODULE);
}
if (UtilValidate.isNotEmpty(result) && !result.getText().equals(message)) {
detectorResult = new Detector(bitMatrix).detect(decodeHints);
result = decoder.decode(detectorResult.getBits(), decodeHints);
if (!result.getText().equals(message)) {
return ServiceUtil.returnError(UtilProperties.getMessage("QRCodeUiLabels", "GeneratedTextNotMatchOriginal", new Object[] { result.getText(), message }, locale));
}
} else {
bufferedImage = newBufferedImage;
}
} else {
detectorResult = new Detector(bitMatrix).detect(decodeHints);
DecoderResult result = decoder.decode(detectorResult.getBits(), decodeHints);
if (!result.getText().equals(message)) {
return ServiceUtil.returnError(UtilProperties.getMessage("QRCodeUiLabels", "GeneratedTextNotMatchOriginal", new Object[] { result.getText(), message }, locale));
}
}
} else if (UtilValidate.isNotEmpty(newBufferedImage)) {
bufferedImage = newBufferedImage;
}
Map<String, Object> result = ServiceUtil.returnSuccess();
result.put("bufferedImage", bufferedImage);
return result;
} catch (WriterException e) {
return ServiceUtil.returnError(UtilProperties.getMessage("QRCodeUiLabels", "ErrorGenerateQRCode", new Object[] { e.toString() }, locale));
} catch (ChecksumException | FormatException | NotFoundException e) {
return ServiceUtil.returnError(UtilProperties.getMessage("QRCodeUiLabels", "ErrorVerifyQRCode", new Object[] { e.toString() }, locale));
}
}
use of com.google.zxing.ChecksumException in project Telegram-FOSS by Telegram-FOSS-Team.
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 fe is null, this can't be
throw ce;
}
}
use of com.google.zxing.ChecksumException in project mollyim-android by mollyim.
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