springboot二维码示例
pom.xml依赖
<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.16</version></dependency><dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.3.3</version></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
查看二维码