二维码生成(JAVA)


1.所需依赖                                                       

<dependency>
  <groupId>com.google.zxinggroupId>
  <artifactId>coreartifactId>
  <version>3.0.0version>
dependency>
<dependency>
  <groupId>com.google.zxinggroupId>
  <artifactId>javaseartifactId>
  <version>3.0.0version>
dependency>

2.代码                                                              

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

        //创建map集合
        Map hint = new HashMap();
        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();
        }
    }