Use the zxing provided by Google to generate a QR code (which can be used for the QR code generated during WeChat payment)

1. Add dependencies

Because the method provided by Google is used, the dependency must be added first

<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>

Add a compilation plugin here by the way

<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>

Just a few lines of code

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("生成二维码成功");

Guess you like

Origin blog.csdn.net/qq_36905956/article/details/105137462
Recommended