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

相关推荐
用户5508492902562 分钟前
CSS布局实战:从Flex到Grid的完整避坑指南
前端
用户5508492902562 分钟前
前端接口请求层怎么封装?一个可落地的请求方案
前端
沫璃染墨9 分钟前
《从零入门Linux系统篇(五十七):线程篇·十——生产者消费者模型进阶:从环形缓冲区到POSIX信号量》
linux·运维·服务器·开发语言·c++·系统架构·信号处理
MetWeave16 分钟前
跟着一条 SPECI 走流水线:从电报到界面
前端
专业程序开发源17 分钟前
SSM校园拍摄交流服务平台36936-计算机课程设计、毕业设计
java·spring boot·后端·python·elasticsearch·php·课程设计
用户0942485680318 分钟前
第23章:OpenJDK逃逸分析、标量替换与锁优化
java·jvm
wuminyu20 分钟前
LockStack在虚拟线程Mount和Unmount拷贝过程剖析
java·linux·c语言·jvm·c++
樱花落木兰31 分钟前
分布式登录实战:Session 会话共享改造,Redis 存储用户登录状态
java·javascript·数据库·redis·分布式·缓存
Helix25033 分钟前
Chrome 开发者工具进阶:长截图、网络调试与性能分析
前端·chrome·chrome devtools·使用技巧·性能分析·开发者工具·长截图
HAHAXX838 分钟前
电商RPA批量上架通用方案:一套流程如何同时跑通拼多多、抖店、淘宝和跨境平台
java·运维·rpa