SpringBoot 二维码生成

一、基于Google开发工具包ZXing生成二维码

1、引入需要的依赖

<!-- zxing生成二维码 -->
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.3.3</version>
</dependency>
 
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.3.3</version>
</dependency>

2.具体代码实现

import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import java.io.IOException;
import java.nio.file.Path;
import static java.nio.file.FileSystems.getDefault;

/**
 * @project 
 * @Description
 * @Author songwp
 * @Date 2022/8/25 11:07
 * @Version 1.0.0
 **/
public class QRCodeGenerator {

    /*
     * text - 内容
     * width - 二维码宽度
     * height - 二维码高度
     * filePath - 二维码存放位置
     */
    public static void generateQRCodeImage(String text, int width, int height, String filePath)throws WriterException, IOException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
        Path path = getDefault().getPath(filePath);
        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
    }

    /*
     * 测试方法
     * */
    public static void main(String[] args) {
        try {
            generateQRCodeImage("https://www.cnblogs.com/songweipeng'", 350, 350, "D:\\photo\\QRTest.png");
        } catch (WriterException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

3.效果显示

 

 二、基于开源的Hutool工具生成二维码

1、引入需要的依赖

 <!--二维码开发工具集-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>4.6.10</version>
        </dependency>
 
 
        <!-- zxing生成二维码 -->
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.3.3</version>
        </dependency>
 
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.3.3</version>
        </dependency>

2.利用Bean的形式显示配置并注入QrConfig,如下代码所示:

import cn.hutool.extra.qrcode.QrConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.awt.*;

/**
 * @project 
 * @Description
 * @Author songwp
 * @Date 2022/8/25 11:21
 * @Version 1.0.0
 **/
@Configuration
public class QRCode {
    @Bean
    public QrConfig qrConfig(){
        QrConfig qrConfig=new QrConfig();
        qrConfig.setBackColor(Color.white.getRGB());
        qrConfig.setForeColor(Color.black.getRGB());
        return qrConfig;
    }
}

3.编写业务实现类

import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

/**
 * @project 
 * @Description
 * @Author songwp
 * @Date 2022/8/25 11:22
 * @Version 1.0.0
 **/
@Service
public class QRService {
    @Resource
    private QrConfig qrconig;
public void generateFile(String content, File file){ //生成到本地文件 QrCodeUtil.generate(content, qrconig, file); } //输出到流 public void generateStream(String content, HttpServletResponse response) throws IOException { QrCodeUtil.generate(content,qrconig,"png",response.getOutputStream()); } }

4.编写QRController控制器类中进行调用,如下代码所示:

import com.songwp.service.impl.QRService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
* @project
* @Description
* @Author songwp
* @Date 2022/8/24 16:11
* @Version 1.0.0
**/
@RestController
@RequestMapping("/user")
public class UserController{

@Autowired
private QRService qrService;

@RequestMapping("/qrCode")
public void generateV3(String content, HttpServletResponse servletResponse) throws IOException {
qrService.generateStream(content,servletResponse);
}

5.postman调用测试:

posted @ 2022-08-25 11:42  [奋斗]  阅读(781)  评论(0编辑  收藏  举报