java 解析二维码工具类

mavn pom.xml

复制代码
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.5.3</version>
</dependency>

java 测试类

java 复制代码
import com.google.zxing.BinaryBitmap;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class QRCodeReader {

    public static String decodeQRCode(String filePath) {
        try {
            BufferedImage bufferedImage = ImageIO.read(new File(filePath));
            BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(bufferedImage)));
            Result result = new MultiFormatReader().decode(binaryBitmap);
            return result.getText();
        } catch (NotFoundException e) {
            System.err.println("No QR code found in the image.");
        } catch (IOException e) {
            System.err.println("Error reading the image file: " + e.getMessage());
        }
        return null;
    }

    public static void main(String[] args) {
        String filePath = QRCodeReader.class.getResource("/861393067237070.png").getPath();
        String qrCodeText = decodeQRCode(filePath);
        if (qrCodeText != null) {
            System.out.println("Decoded QR Code: " + qrCodeText);
        } else {
            System.out.println("Failed to decode QR code.");
        }
    }
}

重点关注:

main方法

相关推荐
水银嘻嘻8 分钟前
web 自动化之 Selenium 元素定位和浏览器操作
前端·selenium·自动化
一切皆有迹可循10 分钟前
Spring Boot 基于 CAS 实现单点登录:原理、实践与优化全解析
java·spring boot·后端
Clf丶忆笙15 分钟前
Spring Boot配置文件详解:从入门到精通
java·spring boot
上官箫羽19 分钟前
Spring Boot 单元测试使用教程(仅供参考)
java·spring boot·单元测试
余辉zmh20 分钟前
【Linux系统篇】:Linux线程控制基础---线程的创建,等待与终止
linux·运维·服务器
从零开始学习人工智能27 分钟前
Feign 重试策略调整:优化微服务通信的稳定性
java·微服务·架构
GanGuaGua33 分钟前
CSS:元素显示模式与背景
前端·javascript·css·html
一个会的不多的人35 分钟前
C# NX二次开发:判断两个体是否干涉和获取系统日志的UFUN函数
前端·javascript·html
小离a_a40 分钟前
uniapp tabBar 中设置“custom“: true 在H5和app中无效解决办法
前端·uni-app
coding侠客1 小时前
使用Jmeter进行核心API压力测试
java·jmeter·apache·压力测试