Utilice el zxing proporcionado por Google para generar un código QR (que se puede utilizar para el código QR generado durante el pago de WeChat)

1. Agregar dependencias

Debido a que se utiliza el método proporcionado por Google, la dependencia debe agregarse primero

<dependencies>
        <!-- google生成二维码依赖 -->
        <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>
    </dependencies>

Agregue un complemento de compilación aquí por cierto

<build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <target>1.8</target>
                    <source>1.8</source>
                </configuration>
            </plugin>
        </plugins>
    </build>

Solo unas pocas líneas de código

Map<EncodeHintType, Object> encodeHintTypeObjectMap = new HashMap<EncodeHintType, Object>();
        encodeHintTypeObjectMap.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        //创建一个矩阵对象
        BitMatrix bitMatrix = new MultiFormatWriter().encode("Hello world", BarcodeFormat.QR_CODE, 200, 200, encodeHintTypeObjectMap);
		//指定生成图片的位置 与格式
        Path path = FileSystems.getDefault().getPath("D://","grcode.png");

        //将矩阵对象转换为图片
        MatrixToImageWriter.writeToPath(bitMatrix, "png",path);
        System.out.println("生成二维码成功");

Supongo que te gusta

Origin blog.csdn.net/qq_36905956/article/details/105137462
Recomendado
Clasificación