Googleが提供するzxingを使用してQRコードを生成します(WeChat支払い中に生成されたQRコードに使用できます)

1.依存関係を追加します

Googleが提供する方法を使用するため、最初に依存関係を追加する必要があります

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

ちなみにここにコンパイルプラグインを追加してください

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

ほんの数行のコード

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

おすすめ

転載: blog.csdn.net/qq_36905956/article/details/105137462