zoukankan      html  css  js  c++  java
  • 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;
        }

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

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

  • 相关阅读:
    web.xml配置详解
    oracle按时间创建分区表
    cron表达式详解
    临时表
    配置非安装版tomcat服务
    CodeForces 785 D Anton and School
    CodeForces 601B Lipshitz Sequence
    CodeForces 590C Three States BFS
    CodeForces 592D Super M DP
    CodeForces 507E Breaking Good 2维权重dij
  • 原文地址:https://www.cnblogs.com/livedian/p/11804745.html
Copyright © 2011-2022 走看看