用com.google.zxing创建二维码

来源:互联网 发布:哈工大电力大数据 编辑:程序博客网 时间:2024/05/16 06:33

1.jar 包  maven:

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

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


2.代码

int width=300;
    int height=300;
    String format="png";
    String content="傻逼";
   
    //定义二维码参数
    HashMap code = new HashMap();
    code.put(EncodeHintType.CHARACTER_SET, "utf-8");
    code.put(EncodeHintType.MARGIN, 2);
   
    try{
    BitMatrix encode = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height,code);
    Path file = new File("D:/test/img.png").toPath();
    MatrixToImageWriter.writeToPath(encode,format,file);
    }catch(Exception e){
    e.printStackTrace();
    }



原创粉丝点击