Pages

Wednesday, December 15, 2021

 Read and Verify QR code with Zxing and Appium

In every day life, we are witnessed about new technologies update. This is good for users as it helps to get every process easy and faster. QR code is now very common. Appium does not provide any support to handle QR code. For Automation, its not easy to handle such type of feature. Generally we should avoid to automate features that interact with hardware until we don't have a stable solution.

Here I am sharing solution for Read and Validating QR code with Appium and Zxing which is quite stable.

Approach: 

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 Zxing to read the QR code content.

Few Words about Zxing

Zxing 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.

Steps by step process

Here are all steps  


1. Add Zxing maven dependency to Pom.xml

<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>

2. Get QR code image from App using Appium

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. We can crop image with the help of X,Y coordinate of QR code with help of function getX() and getY(). 

Here is the code snippet for capture a image and sub image

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;

}

This function return buffered image of QR code

Now we have done with prerequisites i.e. cropped QR code's image.


3. Decode QR code from Cropped QR code using below function

Now we will pass cropped QR code's image and decodeQRCode function will return 

decoded informationbehind QR code in string format.

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();
}

4. Validate decoded information

Now we have actual decoded information and we also have expected result (Expected Decoded

 information of QR Code) so we can validate it. We have to invoke all above function and validate it.

 Here it is

public void readQRCode(String expectedDecodeInfo) throws
 IOException, NotFoundException {

MobileElement qrCodeElement = driver.findElement(By.id(
    "com.eliasnogueira.qr_code:id/qrcode"));

File screenshot = driver.getScreenshotAs(OutputType.FILE);



String actualDecodeInfo = decodeQRCode(generateImage(
                               qrCodeElement, screenshot));

System.out.println("content = " + actualDecodeInfo
);
Assert.assertTrue(actualDecodeInfo.equals(expectedDecodeInfo));
}

Now we are done :)

No comments:

Post a Comment