Unable to read a QR code from a blob link

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.

  1. 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)
  1. 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)
  1. Repeated the previous two methods by removing 'blob:' from the URL and still ended up with a NullPointerException.

  2. 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!

Answer №1

After much trial and error, I have finally cracked the code on how to scan a QR code when the image src attribute is a blob URL. For anyone facing the same dilemma as I was, here's the solution that worked for me.

The key is to merge two methods that I previously attempted.

Firstly, you need to extract the byte array from your URL using a Javascript injection.

private String getBytesBase64FromBlobURI(String uri) {
    // JavaScript logic goes here
}

This method will give you a byte array which then needs to be decoded using the ZXing library to unveil the content of the QR code.

private String decodeQRCode(byte[] qrCodeImage) throws IOException, NotFoundException {
    // Decoding process explained here
}

With these steps in place, you can now successfully retrieve the information embedded within the QR code.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Avoid form submission when the 'enter' key is pressed in Edge, but not in Chrome or Firefox

I'm dealing with an issue in HTML where a 'details' tag is set to open and close when the user presses enter. However, on Edge browser, pressing enter on the 'details' tag actually submits the form. I've been tasked with preve ...

What is the method for configuring environment variables in the Lumber framework?

Installing Lumber CLI npm install -g lumber-cli -s Next, lumber generate "adminpanel_test" --connection-url "mysql://root@localhost:3306/admin-dev" --ssl "false" --application-host "localhost" --application-port "3310" Error: lumber is not recognized a ...

Extract information from a website generated using JavaScript

I'm having trouble extracting the phone number (623) 337-**** from a dynamically generated JS site. Here's my current code: from selenium import webdriver import re browser = webdriver.Firefox() browser.get('http://www.spokeo.com/search?q=J ...

Revamp local route variables in Express.js without the need for a page refresh

Currently, I am working on a project using node.js along with the express.js framework and EJS for template handling. Below is my routing code: app.get('/',function(req,res) { res.render('index', { matches: [], status_messag ...

What is the best way to include the parameter set in the interceptor when making a post request?

-> Initially, I attempt to handle this scenario in the axios request interceptor; if the parameter is uber, then utilize a token. If the parameter is not uber, then do not use a token. -> Afterward, how can I specify uber as a parameter in the custo ...

How to handle and display validation errors in an AJAX post request using express-validator in Node.js/Express

I am in the learning phase with Node and attempting to display validation errors (using express-validator and express 4) when a user submits a form. The validator appears to be functioning properly because when I log the data to the console, everything lo ...

Show a condensed version of a lengthy string in a div using React TS

I've been tackling a React component that takes in a lengthy string and a number as props. The goal of the component is to show a truncated version of the string based on the specified number, while also featuring "show more" and "show less" buttons. ...

What causes a horizontal line to form when a user enters white space?

I have written a piece of code which seems to be causing an issue when running it. Whenever I input empty spaces, it results in creating a horizontal line. import React, { Component } from 'react'; export default class App extends Component { co ...

What is the best way to add hidden columns in Telerik Grid MVC3?

I'm currently working with a grid where I need to hide certain columns using the following code: foreach (var attr in grid.Attr) .Columns(columns => { columns.Bound(attr.key) .Width(attr.width) .Visible(attr.isVisi ...

Ways to access properties beyond the top-level object

I'm encountering issues with JSON parsing using the Jackson library { "userName": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a5c7c9c4c7c9c4c7c9c4e5c2c8c4ccc98bc6cac8">[email protected]</a ...

I am looking to create a for loop that will automate the process of clicking on multiple links and navigating back to the previous page

Below is the code I have written to achieve a specific task: my_list = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//table[@border='1']//a"))) for option in my_list: option.click() WebDriver ...

Transitioning from using lerna to adopting pnpm

We are in the process of transitioning our project from Lerna to PNPM and we currently have a script that we run. Here are the commands: "postinstall": "npm run bootstrap" "bootstrap": "lerna bootstrap --hoist", &quo ...

Divide the sentence using unique symbols to break it into individual words, while also maintaining

Is there a way to split a sentence with special characters into words while keeping the spaces? For example: "la sílaba tónica es la penúltima".split(...regex...) to: ["la ", "sílaba ", "tónica ", "es ", "la ", "penúltima"] ↑ ...

Preload-webpack-plugin does not support pre-fetching files

I have a query about prefetching and preloading content. In my vue app, I noticed that after building, I have duplicate files loaded in my dist/index.html file. Here is an example: Additionally, the "scripts" are not being preloaded/prefetched as expec ...

Exploring VueJS Router History Mode with NGinx web server

After conducting research, I discovered several issues similar to the one I am facing. Currently, I have a docker-compose setup on Digital Ocean with NGinx, VueJS, and a Static Landing Page. Everything was working fine until I added a new route with a fo ...

Next.js components do not alter the attributes of the div element

I am encountering a problem with nextjs/reactjs. I have two tsx files: index.tsx and customAlert.tsx. The issue that I am facing is that the alert does not change color even though the CSS classes are being added to the alert HTML element. Tailwind is my c ...

Troubleshooting a Custom Pipe Problem in Angular Material Drag and Drop

Currently, I am working on a project involving Angular Material Drag And Drop functionality. I have created a simplified example on StackBlitz which you can access through this link: here The project involves two lists - one containing pets and the other ...

AngularJS enables the loading of directives dynamically, but sometimes encounters errors such as "Restricted URI access denied."

Presently, I am in the process of developing a small educational project using HTML5, CSS, JS and AngularJS. Hiccup: Loading an AngularJS Directive in my index.html file Error code [1] - Local browser Error: Access to restricted URI denied Various resp ...

What is causing my array of objects to constantly accumulate undefined elements?

My quick sort function implementation for the object users_total_likes is behaving unexpectedly. When compiled and run in the terminal or browser, it adds undefined values causing a TypeError: if(users[i][key] >= users[hi][key] && users[j][key] ...

When a login attempt is unsuccessful, I am redirected to /api/auth/error using next-auth

Currently, I am utilizing next-auth version 4.18.8 on my login page for the final project of my Fullstack JS course. It's worth noting that a more recent version is being used compared to what was taught in the course (next-auth v. 3). Upon entering ...