java生成二维码技术实现

java生成二维码技术实现

一.第一种

maven依赖

<dependency>
<groupId>com.google.zxing</groupId>
     <artifactId>core</artifactId>
     <version>3.4.0</version>
</dependency>
<dependency>
   <groupId>com.google.zxing</groupId>
   <artifactId>javase</artifactId>
   <version>3.4.0</version>
</dependency>

java 代码

/**
     * 生成二维码  生成一个新的页面
     * @param request
     * @param response
     */
    @ApiOperation(value = "生成二维码,生成一个新的页面")
    @RequestMapping("/createQRcode")
    public void createQRcode(HttpServletRequest request, HttpServletResponse response) {
        String contents = "https://www.baidu.com/";
        int width = 500; int height = 500; int margin = 2;
        try {
            BufferedImage QRcode = QrCodeCreateUtil.createQRImage(contents, width, height, margin);

            String logoPath = request.getSession().getServletContext().getRealPath("") + "QR" + File.separator;
//            String logoPath = "C:\\Users\\zy962\\Desktop\\临时备份\\u=1792043407,969529386&fm=26&gp=0.jpg";
            File file2 = new File(logoPath);
            if(!file2.exists()  && !file2.isDirectory()){
                file2.mkdirs();
            }
            logoPath = logoPath + System.currentTimeMillis()+"_"+new Random().nextInt(1000) + ".jpg";
            file2 = new File(logoPath);
            if (!file2.exists()) {
                file2.createNewFile();
            }
            System.out.println("生成图片:" + logoPath);
            int logoSize = 4;
            BufferedImage qRImageWithLogo = QrCodeCreateUtil.addQRImagelogo(QRcode, width, height, logoPath, logoSize);

            // 写入返回
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(qRImageWithLogo, "jpg", baos);

            byte[] QRJPG = baos.toByteArray();
            response.setHeader("Cache-Control", "no-store");
            response.setHeader("Pragma", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setContentType("image/jpeg");

            ServletOutputStream os = response.getOutputStream();
            os.write(QRJPG); // 自此完成一套,图片读入,写入流,转为字节数组,写入输出流
            os.flush();
            os.close();
            baos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

第二种

maven依赖

    <dependency>
        <groupId>net.glxn.qrgen</groupId>
          <artifactId>javase</artifactId>
          <version>2.0</version>
      </dependency>

java 代码

  /**
     * 生成二维码  在列表中显示
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping("/getPng")
    @ApiOperation(value = "生成二维码  在列表中显示")
    public void getPng(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String url = "https://www.baidu.com/";
        ByteArrayOutputStream out = QRCode.from(url).to(ImageType.PNG).stream();
        response.setContentType("image/png");
        response.setContentLength(out.size());
        OutputStream os = response.getOutputStream();
        os.write(out.toByteArray());
        os.flush();
        os.close();
    }
    
  // 生成二维码图片
  public void code() {
        try {
            String content = "https://www.baidu.com";
            String path = "D:\\upload\\img\\livecode";// 二维码保存的路径
            String codeName = UUID.randomUUID().toString();// 二维码的图片名
            String imageType = "jpg";// 图片类型
            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
            Map<EncodeHintType, String> hints = new HashMap<EncodeHintType, String>();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 400, 400, hints);
            File file1 = new File(path, codeName + "." + imageType);
            MatrixToImageWriter.writeToFile(bitMatrix, imageType, file1);
        } catch (WriterException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

使用img标签调用getPng方法

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值