java 生成带参数的二维码

直接上代码:

1.先引入jar包

<!--二维码生成jar包-->
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.3.0</version>
        </dependency>

2.生成二维码的方法

private static void generateQRCodeImage(String text, int width, int height, String filePath) throws WriterException, IOException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
        Path path = FileSystems.getDefault().getPath(filePath);
        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
    }


    public String getCode(String appointmentNumber) {
        String ctxPath = "D://file";
        String fileName="twoCode.png";
        String bizPath = "files";
        String qrCode = "files";
        try {
            String nowday = new SimpleDateFormat("yyyyMMdd").format(new Date());
            String ppath =ctxPath + File.separator + bizPath + File.separator + nowday;
            File file = new File(ctxPath + File.separator + bizPath + File.separator + nowday);
            if (!file.exists()) {
                file.mkdirs();// 创建文件根目录
            }
            String savePath = file.getPath() + File.separator + fileName;
            qrCode = bizPath + File.separator + nowday+ File.separator + fileName;
        if (savePath.contains("\\")) {
            savePath = savePath.replace("\\", "/");
        }
            if (qrCode.contains("\\")) {
                qrCode = qrCode.replace("\\", "/");
            }
//        String codeUrl=ppath+"/twoCode.png";
            System.out.print(qrCode);
            System.out.print(savePath);
            generateQRCodeImage(appointmentNumber, 350, 350, savePath);
            return qrCode;
        } catch (WriterException e) {
            System.out.println("Could not generate QR Code, WriterException :: " + e.getMessage());
        } catch (IOException e) {
            System.out.println("Could not generate QR Code, IOException :: " + e.getMessage());
        }
        return qrCode;
    }

我是将生成的二维码保存到本地,把地址返回给前端

--------------------------------请多多指教

 

上一篇:MyBatis传入参数为list、数组、map写法


下一篇:字符串翻转