java 生成二维码 qr

https://www.cnblogs.com/liuys635/p/15398427.html

maven 依赖


        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.3.0</version>
        </dependency>

demo

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

    private static final String QR_CODE_IMAGE_PATH = "C:\\Users\\188\\Desktop\\test\\tetsqr.png";

    private 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("This is my first QR Code", 350, 350, QR_CODE_IMAGE_PATH);
        } catch (WriterException e) {
            System.out.println("Could not generate QR Code, WriterException :: " + e.getMessage());
        } catch (IOException e) {
            System.out.println("Could not generate QR Code, IOException :: " + e.getMessage());
        }
    }
}

生成的二维码图片如下:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Java生成二维码,可以使用第三方库Zxing。首先需要在项目中引入该库,可以通过 Maven 或 Gradle 进行引入。 生成二维码的代码如下: ```java import java.io.File; import java.nio.file.Path; import java.nio.file.Paths; import java.util.HashMap; import java.util.Map; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; public class QRCodeGenerator { public static void main(String[] args) { String qrCodeData = "R2022121200000027"; String filePath = "qr_code.png"; int size = 250; String fileType = "png"; File qrFile = new File(filePath); try { createQRCode(qrFile, qrCodeData, size, fileType); System.out.println("QR Code generated successfully!"); } catch (WriterException e) { System.out.println("Error generating QR Code: " + e.getMessage()); } catch (Exception e) { System.out.println("Error: " + e.getMessage()); } } private static void createQRCode(File qrFile, String qrCodeData, int size, String fileType) throws WriterException, Exception { // Set QR code parameters Map<EncodeHintType, Object> hintMap = new HashMap<EncodeHintType, Object>(); hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8"); hintMap.put(EncodeHintType.MARGIN, 2); hintMap.put(EncodeHintType.QR_VERSION, 4); // Create QR code writer and encode the data QRCodeWriter qrCodeWriter = new QRCodeWriter(); BitMatrix byteMatrix = qrCodeWriter.encode(qrCodeData, BarcodeFormat.QR_CODE, size, size, hintMap); // Create QR code image Path path = Paths.get(qrFile.getAbsolutePath()); MatrixToImageWriter.writeToPath(byteMatrix, fileType, path); } } ``` 在上面的代码中,我们首先定义了要生成的二维码的内容、文件路径、大小和文件类型。然后使用 QRCodeWriter 对象将内容编码为二维码,再使用 MatrixToImageWriter 将其写入文件中。运行该代码,会在项目根目录下生成一个名为 qr_code.png 的二维码文件,包含订单号 R2022121200000027。 你也可以根据自己的需求修改代码中的参数,比如大小、文件类型等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值