Java读取QR code

2023-12-26 15:29:27

首先导入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) {

}

}

文章来源:https://blog.csdn.net/qq_30273575/article/details/135222379
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。