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;
    }

我是將生成的二維碼保存到本地,把地址返回給前端

--------------------------------請多多指教

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM