java生成和解析qrcode二维码

1、创建springboot工程

2、引入maven依赖,这里使用谷歌的zxing实现二维码生成

<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.3.1</version>
</dependency>
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.3.1</version>
</dependency>

3、编写qrcode工具类

package com.llg.mybatis.utils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;

import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;

public class QRCodeUtils {

    private static final int BLACK = 0xFF000000;
    //二维码颜色
    private static final int WHITE = 0xFFFFFFFF;

    public static BufferedImage codeCreate(String text){
        BufferedImage image = null;
        try {
            Map<EncodeHintType, String> his = new HashMap<>();
            his.put(EncodeHintType.CHARACTER_SET, "utf-8");
            BitMatrix encode = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, 400, 400, his);
            int codeWidth = encode.getWidth();
            int codeHeight = encode.getHeight();
            image = new BufferedImage(codeWidth, codeHeight, BufferedImage.TYPE_INT_RGB);
            for (int i = 0; i < codeWidth; i++) {
                for (int j = 0; j < codeHeight; j++) {
                    image.setRGB(i, j, encode.get(i, j) ? BLACK : WHITE);
                }
            }
        } catch (WriterException e) {
            e.printStackTrace();
        }

        return image;
    }
}

4、编写controller接口,用于浏览器访问,我们直接将二维码输出到浏览器页面上去

@GetMapping("codeCreate")
public void codeCreate(@RequestParam("text") String text, HttpServletResponse response){
    try {
        BufferedImage image = QRCodeUtils.codeCreate(text);
        ImageIO.write(image, "jpg", response.getOutputStream());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

5、启动项目,我们试一下,生成百度网址的二维码,访问http://localhost:8080/codeCreate?text=https://www.baidu.com

使用微信扫一扫,可以直接跳到百度首页

以上完成了二维码的生成,非常简单,下面我们做一个二维码的解析接口

6、继续在二维码工具类中新增解析方法

public static String codeAnalyze(InputStream is) throws Exception{
    MultiFormatReader formatReader = new MultiFormatReader();
    try {
        BufferedImage image = ImageIO.read(is);
        LuminanceSource source = new BufferedImageLuminanceSource(image);
        Binarizer binarizer = new HybridBinarizer(source);
        BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
        Map hints = new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        Result result = formatReader.decode(binaryBitmap, hints);
        return result.getText();
    } catch (NotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

7、编写controller接口,接受前端传递过来的二维码图片,并调用刚才编写的解析方法对二维码进行解析,最后将结果返回页面

@PostMapping("uploadQRCode")
public String uploadQRCode(@RequestParam("file") MultipartFile file){
    if (file.isEmpty()) {
        return "请选择文件";
    }
    try(InputStream is = file.getInputStream()) {
        return QRCodeUtils.codeAnalyze(is);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "解析失败!";
}

8、编写上传图片页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

    <h1>上传二维码</h1>
    <form action="/uploadQRCode" enctype="multipart/form-data">
        <input type="file" name="file">
        <input type="submit">
    </form>

</body>
</html>

9、启动项目,访问:http://localhost:8080/index.html

10、我们先生成一个包含“I love you” 的二维码,然后截图保存在电脑上,然后使用刚才创建的页面上传,可以看到返回结果确实是 I love you!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值