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

相关推荐
面朝大海,春不暖,花不开9 分钟前
自定义Spring Boot Starter的全面指南
java·spring boot·后端
得过且过的勇者y10 分钟前
Java安全点safepoint
java
小小小小宇24 分钟前
前端小tips
前端
码农101号25 分钟前
Linux中shell编程表达式和数组讲解
linux·运维·服务器
小小小小宇33 分钟前
二维数组按顺时针螺旋顺序
前端
夜晚回家1 小时前
「Java基本语法」代码格式与注释规范
java·开发语言
是小满满满满吗1 小时前
传输层:udp与tcp协议
linux·服务器·网络
安木夕1 小时前
C#-Visual Studio宇宙第一IDE使用实践
前端·c#·.net
努力敲代码呀~1 小时前
前端高频面试题2:浏览器/计算机网络
前端·计算机网络·html
斯普信云原生组1 小时前
Docker构建自定义的镜像
java·spring cloud·docker