Created
February 11, 2021 21:00
-
-
Save eliasnogueira/e1f233f6fc550fb1241bcd5056c82bee to your computer and use it in GitHub Desktop.
Decode an image based either on a full path or base64 content
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The full code example can be found at https://github.com/eliasnogueira/selenium-read-qrcode