Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Decode an image based either on a full path or base64 content
private static String decodeQRCode(Object qrCodeImage) {
Result result = null;
try {
BufferedImage bufferedImage;
// if not (probably it is a URL
if (((String) qrCodeImage).contains("http")) {
bufferedImage = ImageIO.read((new URL((String)qrCodeImage)));
// if is a Base64 String
} else {
byte[] decoded = Base64.decodeBase64((String)qrCodeImage);
bufferedImage = ImageIO.read(new ByteArrayInputStream(decoded));
}
LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
result = new MultiFormatReader().decode(bitmap);
} catch (NotFoundException | IOException e) {
log.error("Error reading the QR Code", e);
}
return result.getText();
}
@eliasnogueira

This comment has been minimized.

Copy link
Owner Author

@eliasnogueira eliasnogueira commented Feb 11, 2021

The full code example can be found at https://github.com/eliasnogueira/selenium-read-qrcode

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment