After spending countless hours searching Google and SO, I still haven't found a solution on how to scan a QR code in my Java based Selenium tests. I've tried various methods but encountered errors along the way.
Attempted to use the ZXing library with no success when dealing with blob URLs.
private String decodeQRCode(URL qrCodeImage) throws IOException, NotFoundException { BufferedImage bufferedImage = ImageIO.read(qrCodeImage); LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Result result = new MultiFormatReader().decode(bitmap); return result.getText(); }
Error received during this attempt:
unknown protocol: blob
java.net.MalformedURLException: unknown protocol: blob
at java.net.URL.<init>(URL.java:617)
at java.net.URL.<init>(URL.java:507)
at java.net.URL.<init>(URL.java:456)
Tried using the ZXing library with Base64 instead of URL.
private String decodeQRCode(String qrCodeImage) throws IOException, NotFoundException { byte[] decoded = Base64.decodeBase64(qrCodeImage); BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(decoded)); LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Result result = new MultiFormatReader().decode(bitmap); return result.getText(); }
Error encountered with this method:
null
java.lang.NullPointerException
at com.google.zxing.client.j2se.BufferedImageLuminanceSource.<init>(BufferedImageLuminanceSource.java:42)
Repeated the previous two methods by removing 'blob:' from the URL and still ended up with a NullPointerException.
Also attempted Javascript injection using the executeAsyncScript() function.
private String getBytesBase64FromBlobURI(String uri) { // Javascript script here... JavascriptExecutor jsExecutor = (JavascriptExecutor) driver; String result = (jsExecutor.executeAsyncScript(script, uri)).toString(); return result; }
No error occurred with this approach, but the retrieved value was incorrect.
Expected value after scanning QR code from mobile device:
ecfe09ff-ca31-4e16-9550-82da38a45966
Value obtained from code execution:
iVBORw0KGgoAAAANSUhEUgAAAMgAAADIAQAAAACFI5MzAAABfElEQVR4Xu2XQWrDQAxFFbyYpY/gm9gXCziQi9U3mSPMchb...
I'm struggling to find a working solution for reading QR codes. Any assistance would be greatly appreciated. Thank you!