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

Adjusting the Size of an HTML Slideshow

While designing a homepage for my band, I encountered an issue when trying to integrate a slideshow element from an external source. The size of the slideshow is fixed at 600 x 300px and cannot be adjusted. Additionally, I found it challenging to position ...

Angular: Preserve the URL even when encountering a 404 page

Creating a custom 404 page in Angular 4 is something I have recently done, and I am looking for a way to preserve the incorrect URL that was input by the user. To see an example of this behavior, you can visit sites like GitHub. They show a 404 page when a ...

Instructions on how to implement a readmore button for texts that exceed a specific character length

I am attempting to display a "Read more" button if the length of a comment exceeds 80 characters. This is how I am checking it: <tr repeat.for="m of comments"> <td if.bind="showLess">${m.comment.length < 80 ? m.comment : ...

JavaScript code to make titles slide in as the user scrolls

Looking for a better way to make all titles slide in upon scrolling instead of coding individually? Consider using a forEach loop! Here's how you can modify the code below: function slideInLeft() { document.querySelectorAll('.subtitle-left ...

Python Selenium WebDriverWait not functioning properly when checking visibility of element

I am facing an issue while trying to retrieve an element using its xpath with WebDriverWait. The element is not being picked up as expected - Initially, when I execute - abc = driver.find_element_by_xpath('//div[2]/ag-grid-angular/div/div[2]/div/div[ ...

What steps should I take to fix the error message "Uncaught TypeError: Class constructor m must be called with 'new'" that occurs while attempting to access a polymer v2.0 application?

Is there a way to resolve this error that occurs when attempting to open a Polymer v2.0 app on Safari in iOS? Uncaught TypeError: Class constructor m cannot be invoked without 'new' from custom-elements-es5-adaptor. The Polymer v2.0 starter k ...

Using Bootstrap multiselect, you can easily control the display of a second menu based on the selection in the first menu

On my website, I am working with two menus. When I select Abon from the first menu, it should display all li elements under the Abon optgroup (Abon-1, Abon-2). If I uncheck block in the second menu, those elements should disappear. The code consists of Sel ...

Verify if the expression can be found in the DIV element's style

Recently, I encountered a situation where a DIV contains a background image that is either set directly or generated as an SVG. I needed to figure out how to determine when the DIV contains the SVG-string. Here are my two DIVs: <div class="cover m ...

Error due to PlatformLocation's location dependency issue

My AppComponent relies on Location (from angular2/router) as a dependency. Within the AppComponent, I am using Location.path(). However, when running my Jasmine test, I encountered an error. Can you help me identify the issue with my Jasmine test and guide ...

What could be the reason for my manifest file not being found in the Jar anymore?

I have been working on compiling a runnable jar file, and as part of the process, I created a manifest file. However, after Eclipse finishes compiling, I am encountering a warning that states 'Project/src/META-INF/MANIFEST.MF was replaced by the gener ...

The placement of the Vuetify tooltip is incorrectly aligned when located in the footer section

Having trouble fixing the issue with the Vuetify tooltip. After scrolling on the page, the tooltip moves up despite using fixed="true". Here is the code snippet causing the problem: <v-footer app inset fixed> <v-row align="center ...

What is the best method to fetch a specific post from supabase for showcasing in a dynamic Route with Nextjs14?

I'm currently working on a Next.js application where I am fetching posts from a Supabase database. Everything works fine when retrieving all posts, but when trying to retrieve a single post dynamically using the ID, the screen displays null. Here&apos ...

Ways to adjust timestamps (DayJs) by increments of 1 minute, 5 minutes, 15 minutes, 30 minutes, and more

Currently, I am exploring time functionality within React Native by utilizing DayJs. I have noticed a slight inconsistency when comparing 2 different points in time to calculate the time difference. Typically, everything works smoothly such as with 10:00 ...

Using TypeScript with Watermelondb

I'm currently developing a React App and I want to implement Watermelondb for Offline Storage, but I'm unsure about using it with TypeScript. I have already set up the database and created Course and Lesson model files from the Watermelondb libra ...

Checking connectivity in an Ionic application

Within my Ionic application, I am faced with the task of executing specific actions depending on whether the user is currently connected to the internet or not. I plan on utilizing the $cordovaNetwork plugin to determine the connectivity status within the ...

TextWatcher allows for adding multiple listeners using the addTextChangedListener method

How can I implement a text update feature for an edit text field after user input? One idea I had was to add another TextWatcher to the existing ones. Is there a way to ensure that my TextWatcher is triggered before any other ones on the same field (if m ...

The width and height properties in the element's style are not functioning as expected

let divElement = document.createElement("div"); divElement.style.width = 400; divElement.style.height = 400; divElement.style.backgroundColor = "red"; // num : 1 divElement.innerText = "Hello World "; // num : 2 document.body.append(divElement); // Af ...

Leveraging oEmbed - JSON data (within client-side JavaScript)

Is it feasible to utilize an oembed provider that solely produces json and xml outputs on the client side using javascript (through a $.ajax request to the provider)? Do we need to rely on oembed providers that enable a jsonp callback in javascript for th ...

Stopping Amazon Web Services Lambda functions from running on their own

I've implemented a Lambda function that is triggered whenever a new folder object is created in the root bucket. A unique identifier is generated for each folder object, such as 67459e53-20cb-4e7d-8b7a-10e4cd165a44 Within the root bucket, there is a ...

Is there a way to dynamically update a game's highscore from a database without having to refresh the page?

I developed a JS snake game using HTML5 canvas. In the event that the user loses, the score is transmitted to the database through an AJAX call in my PHP script. The PHP code then compares this score to the current highscore and updates it if needed. Howev ...