java实现生成二维码

添加依赖:

<!--        生成二维码-->
    <dependency>
        <groupId>com.google.zxing</groupId>
        <artifactId>javase</artifactId>
        <version>3.3.0</version>
    </dependency>

代码实现:

package 二维码;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;

/**
 * 描述:     生成二维码工具
 */
public class QRCodeGeneratorUtil {


public static void generateQRCodeImage(String text, int width, int height, String filePath)
        throws WriterException, IOException {
    QRCodeWriter qrCodeWriter = new QRCodeWriter();
    BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
    Path path = FileSystems.getDefault().getPath(filePath);
    MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
}

public static void main(String[] args) {
    try {
        generateQRCodeImage("http://39.97.115.254", 150, 150, "C:\\Users\\wb\\IdeaProjects\\study\\src\\main\\java\\二维码\\QRTest.png");
    } catch (WriterException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

已有 0 条评论

    欢迎您,新朋友,感谢参与互动!