java生成二维码代码

 /**
     * 生成二维码链接
     *
     * @param text //二维码里面的内容或者跳转的地址
     * @param orderid
     * @param request
     * @return
     */
    @ApiOperation("生成二维码")
    @GetMapping("/erwei")
//    @SuppressWarnings("deprecation")
    public static String erwei(String text,  int orderid, HttpServletRequest request)throws Exception {
        int width = 300; // 二维码图片宽度
        int height = 300; // 二维码图片高度
        String format = "png";// 二维码的图片格式

        Hashtable hints = new Hashtable();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");   // 内容所使用字符集编码

        BitMatrix bitMatrix = null;
        try {
            bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
        } catch (WriterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        ClassUtils.getDefaultClassLoader().getResource("").getPath();
//        System.out.println(ClassUtils.getDefaultClassLoader().getResource("").getPath());
        String path = System.getProperty("user.dir") + "\\ajaxupload";
        System.out.println(path);
        File filepath = new File(path);
        //判断路径是否存在
        if (!filepath.exists()){
            filepath.mkdirs();
        }
        String rootPath = request.getSession().getServletContext().getRealPath("/");
        String contextPath = request.getSession().getServletContext().getContextPath();
        //存放路径
        String url = filepath + File.separator+ "static" + File.separator + "erwei" + File.separator + orderid + ".png";
        //显示路径
        File filepath1 = new File(url);

        if (!filepath1.exists()){
            filepath1.mkdirs();
        }
        String imgUrl = filepath + "/static/erwei/" + orderid + ".png";
        System.out.println(imgUrl);
        // 生成二维码
        File outputFile = new File(url);
        try {
            MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return imgUrl;
    }

你可能感兴趣的:(java生成二维码代码)