springboot二维码示例

pom.xml依赖

     <dependency>
            <groupId>cn.hutoolgroupId>
            <artifactId>hutool-allartifactId>
            <version>5.8.16version>
        dependency>
        <dependency>
            <groupId>com.google.zxinggroupId>
            <artifactId>coreartifactId>
            <version>3.3.3version>
        dependency>

controller

import cn.hutool.extra.qrcode.QrCodeUtil;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;

@RestController
public class QrCodeController {
    @GetMapping(value = "/img", produces = MediaType.IMAGE_PNG_VALUE)
    public byte[] getImage() throws Exception {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        // 生成指定url对应的二维码到文件,宽和高都是300像素
        QrCodeUtil.generate("https://hutool.cn/", 300, 300,"", out);
        return out.toByteArray();
    }
}

查看效果

访问
http://localhost:8080/img
查看二维码

你可能感兴趣的:(java,spring,boot,后端,java)