二维码生成(JAVA)

1.所需依赖                                                       

<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.代码                                                              

public void testQRCode(){
        //二维码高度和宽度
        int width = 200;
        int height = 200;

        //创建map集合
        Map<EncodeHintType,Object> hint = new HashMap<EncodeHintType,Object>();
        hint.put(EncodeHintType.CHARACTER_SET,"UTF-8");
        BitMatrix matrix;
        try {
            //核心代码
            matrix = new MultiFormatWriter().encode("二维码需要展示的内容", BarcodeFormat.QR_CODE, width,height, hint );

            //二维码保存路径
            String filePath = "D://";
            String fileName = "a.jpg";
            Path path = FileSystems.getDefault().getPath(filePath,fileName);

            //保存二维码
            MatrixToImageWriter.writeToPath(matrix,"jpg",path);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

posted @ 2022-01-30 22:08  zddsl  阅读(76)  评论(0)    收藏  举报