Skip to content

Instantly share code, notes, and snippets.

@eliasnogueira
Created February 11, 2021 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eliasnogueira/e1f233f6fc550fb1241bcd5056c82bee to your computer and use it in GitHub Desktop.
Save eliasnogueira/e1f233f6fc550fb1241bcd5056c82bee to your computer and use it in GitHub Desktop.
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
Copy link
Author

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