java创建二维码并赋予url链接的功能实现

首先在pom文件中导入有关依赖

com.google.zxingcore3.3.0 com.google.zxingjavase3.3.0

【java创建二维码并赋予url链接的功能实现】工具类
public class YmtUtil {public static byte[] getQRCodeImage(String text, int width, int height) throws WriterException, IOException {QRCodeWriter qrCodeWriter = new QRCodeWriter(); BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height); ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream(); MatrixToImageWriter.writeToStream(bitMatrix, "PNG", pngOutputStream); byte[] pngData = https://www.it610.com/article/pngOutputStream.toByteArray(); return pngData; }

功能实现
此处是写在service中的代码,调用过后就可以在指定的存储位置中找到对应的二维码
//获取要赋值给二维码的链接后缀如 192.168.0.21/erweimaString url = erweima; //获取本机ip地址,也可以找一指定ip地址写死InetAddress localhost = StrUtil.getLocalHostExactAddress(); //设置二维码访问路径String URL= "http://localhost"+ url; //设置二维码流qrcode = YmtUtil.getQRCodeImage(URL, 360, 360); final HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.IMAGE_PNG); //设置生成的二维码存储地址 linux路径:(/root/D:/opt/upFiles)window路径(D:/opt/upFiles)此处使用的是linux路径File path = new File("/root/D:/opt/upFiles", 二维码名称 + ".jpg"); //将二进制数组转为文件ByteArrayInputStream inputStream = new ByteArrayInputStream(qrcode); MockMultipartFile file = new MockMultipartFile(ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream); file.transferTo(path);

以上就是java创建二维码并赋予url链接的详细内容,更多关于java创建二维码的资料请关注脚本之家其它相关文章!

    推荐阅读