DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How to Test QR Codes in Your Applications
  • Web Push Provisioning: Advancements for Digital Wallet Developers
  • Using Barcodes in iText 7
  • React Native QR Code Scanner Using Expo-Barcode-Scanner

Trending

  • Unlocking AI Coding Assistants Part 2: Generating Code
  • The 4 R’s of Pipeline Reliability: Designing Data Systems That Last
  • The Modern Data Stack Is Overrated — Here’s What Works
  • Building Scalable and Resilient Data Pipelines With Apache Airflow

Read QR Code Content With Appium and ZXing

Learn how to read QR code content in a mobile app in automated testing using Appium and the ZXing library.

By 
Elias Nogueira user avatar
Elias Nogueira
·
Jun. 18, 18 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
17.8K Views

Join the DZone community and get the full member experience.

Join For Free

The Problem

Having a QR code to scan from an app is becoming common, as to add your contact info in another phone, distribute your contact info, or even validate a financial transaction.

To guarantee end-to-end (e2e) testing through an application with a QR code you need to read, an approach to get the image is necessary.

This is different from a web page, where you can save it or even convert it to a Base64 string. You cannot get the image resource (in an easy way) from any platform, Android or iOS.

The Solution

An easy solution is to take a screenshot from the device screen, get the points (width and height) from the element on the device, and crop the image to the element size, so you have an image with just the QR code. Now, you can use some a or API to read the QR code content.

How to Read the QRCode Content

ZXing (“zebra crossing”) is an open-source, multi-format 1D/2D barcode image processing library implemented in Java, with ports to other languages. One supported 2D format is the QR code.

The first thing we need to do is add the ZXing library to our project classpath. Below is the snippet in a pom.xml file (Maven):

<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.3.0</version>
</dependency>
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.3.0</version>
</dependency>

With ZXing, we can read the QR code content in different ways. In this solution, we will do this with a BufferedImage.

  1. Pass the BufferedImage to the BufferedImageLuminanceSource Zxing class/
  2. Instantiate a MultiFormatReader calling the decode method passing the BinaryBitmap. The MultiFormatReader is a convenience class and will attempt to decode all barcode formats that the library supports. The result is a Result class that encapsulates the result of decoding a barcode within an image
private static String decodeQRCode(BufferedImage qrCodeImage) throws NotFoundException {
    LuminanceSource source = new BufferedImageLuminanceSource(qrCodeImage);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

    Result result = new MultiFormatReader().decode(bitmap);
    return result.getText();
}

How About Appium?

With Appium (a mobile test automation framework) you can apply the solution: take a device screenshot, get the element, use some code to crop the image and read the QR Code content.

Image title

To do this, you can create an auxiliary method that receives a MobileElement (the element that contains the QR code) and a File (the device screenshot) to get the points and crop the device screenshot to the element size.

The code below does that and returns a BufferedImage.

private BufferedImage generateImage( MobileElement element, File screenshot) throws IOException {
    BufferedImage fullImage = ImageIO.read(screenshot);
    Point imageLocation = element.getLocation();

    int qrCodeImageWidth = element.getSize().getWidth();
    int qrCodeImageHeight = element.getSize().getHeight();

    int pointXPosition = imageLocation.getX();
    int pointYPosition = imageLocation.getY();

    BufferedImage qrCodeImage = fullImage.getSubimage(pointXPosition, pointYPosition, qrCodeImageWidth, qrCodeImageHeight);
    ImageIO.write(qrCodeImage, "png", screenshot);

    return qrCodeImage;
}

How to Implement This Solution

We have almost everything: the code to read a QR code's content and a code to crop the QR code inside an app.

Now, we just want to find the element on the device and call the auxiliary methods.

public void readQRCode() throws IOException, NotFoundException {
   MobileElement qrCodeElement = driver.findElement(By.id("com.eliasnogueira.qr_code:id/qrcode"));
   File screenshot = driver.getScreenshotAs(OutputType.FILE);

   String content = decodeQRCode(generateImage(qrCodeElement, screenshot));
   System.out.println("content = " + content);
}

Full Code Example

You can access the GitHub repo for

  • An app to run the example
  • The full functional code

You can do this approach with any other mobile testing framework using Java.

There is a post called "Read a QR Code With Selenium and zxing" if you want to do the same for web automation.

QR code

Published at DZone with permission of Elias Nogueira. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How to Test QR Codes in Your Applications
  • Web Push Provisioning: Advancements for Digital Wallet Developers
  • Using Barcodes in iText 7
  • React Native QR Code Scanner Using Expo-Barcode-Scanner

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!