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

这篇文章给大家分享java创建二维码并赋予url链接的功能实现,需要获取要赋值给二维码的链接后缀,通过设置二维码的访问路径等一系列操作,具体实现代码跟随小编一起看看吧

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

      com.google.zxing     core     3.3.0        com.google.zxing     javase     3.3.0

工具类

 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 = pngOutputStream.toByteArray(); return pngData; }

功能实现

此处是写在service中的代码,调用过后就可以在指定的存储位置中找到对应的二维码

 //获取要赋值给二维码的链接后缀如 192.168.0.21/erweima String 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-600"); //将二进制数组转为文件 ByteArrayInputStream inputStream = new ByteArrayInputStream(qrcode); MockMultipartFile file = new MockMultipartFile(ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream); file.transferTo(path);

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

以上就是java创建二维码并赋予url链接的功能实现的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java