Java读取QR code

首先导入jar包

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

实现代码如下:

import com.google.zxing.BinaryBitmap;

import com.google.zxing.MultiFormatReader;

import com.google.zxing.ReaderException;

import com.google.zxing.Result;

import com.google.zxing.client.j2se.BufferedImageLuminanceSource;

import com.google.zxing.common.HybridBinarizer;

import javax.imageio.ImageIO;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

public class QRCodeReader {

public static void main(String[] args) {

String filePath = "path/to/qrcode.png"; // 替换为你的二维码图片路径

try {

BufferedImage image = ImageIO.read(new File(filePath));

BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));

Result result = new MultiFormatReader().decode(bitmap);

String qrCodeData = result.getText();

System.out.println("读取到的二维码数据:" + qrCodeData);

} catch (IOException e) {

e.printStackTrace();

} catch (ReaderException e) {

e.printStackTrace();

}

}

}

package com.welab.automation.projects.demo;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import io.appium.java_client.MobileElement;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class readQrCode {

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

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

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

public static void main(String[] args) {

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值