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

相关推荐
琹箐2 分钟前
js文字两端对齐
前端·javascript·css
摆烂工程师4 分钟前
炸裂了~兄弟们,GPT4o出图效果太好了
前端·后端·程序员
开心小老虎5 分钟前
用HTML和CSS生成炫光动画卡片
前端·css·html
逍遥德9 分钟前
pom.xml与.yml,java配置参数传递
xml·java·spring boot·后端·系统架构
米粒宝的爸爸11 分钟前
vue3 vue-router 传递路由参数
前端·javascript·vue.js
YJlio14 分钟前
SQL Server 常见问题解析
运维·服务器·网络
前端同学17 分钟前
react版本主要区别
前端·react.js·前端框架
c无序23 分钟前
【Linux网络-poll与epoll】epollserver设计(两个版本 Reactor)+epoll理论补充(LT ET)
运维·服务器
音视频牛哥30 分钟前
Nginx RTMP 服务器开发设计与实现
运维·服务器·nginx·大牛直播sdk·nginx rtmp代码·nginx rtmp·nginx流媒体服务器
如意.75936 分钟前
从 0 到 1,吃透 Linux 权限管理的底层逻辑
linux·运维·服务器