无论如何,Appium 是否可以自动扫描 IOS 和 android 的二维码?

is there anyway to automate scan QR code by Appium for IOS and android?

我有一个应用程序在 android 和 IOS 中运行,但是主要功能主义者之一依赖于通过移动摄像头扫描 QR 码所以无论如何都可以使用 Appium 来做到这一点?

我不认为 appium 具有执行此操作的本机功能。我们可以使用Zxing外部库来实现它。

Zxing 是在 Java 中实现的开源、多格式一维/二维条码图像处理库,具有其他语言的端口。一种支持的二维格式是二维码。

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.

1.添加 Zxing maven 依赖到 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。使用Appium

从App获取二维码图片
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;

}

3。从使用上述函数生成的图像解码二维码

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.如何利用 generateImage() 和 readQRCode()

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

}

所以content中的信息是从二维码中提取的。

参考:分步指南是here and sample code is here